videoeditorengine/h263decoder/src/viddemux_mpeg.cpp
changeset 0 951a5db380a0
equal deleted inserted replaced
-1:000000000000 0:951a5db380a0
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 * MPEG-4 bitstream parsing.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 /*
       
    22  * Includes
       
    23  */
       
    24 
       
    25 #include "h263dConfig.h"
       
    26 #include "viddemux.h"
       
    27 #include "vdxint.h"
       
    28 #include "mpegcons.h"
       
    29 #include "sync.h"
       
    30 #include "vdcaic.h"
       
    31 #include "zigzag.h"
       
    32 #include "debug.h"
       
    33 #include "biblin.h"
       
    34 /* MVE */
       
    35 #include "MPEG4Transcoder.h"
       
    36 // <--
       
    37 
       
    38 /*
       
    39  * Local function prototypes
       
    40  */
       
    41 
       
    42 /* Macroblock Layer */
       
    43 
       
    44 static int vdxGetIntraDCSize(bibBuffer_t *inBuffer, int compnum, int *IntraDCSize, 
       
    45    int *bitErrorIndication);
       
    46 
       
    47 static int vdxGetRVLCIndex(u_int32 bits, u_int32 *index, int *length, int intra_luma,
       
    48    int *bitErrorIndication);
       
    49 
       
    50 
       
    51 /*
       
    52  * Picture Layer Global Functions
       
    53  */
       
    54 
       
    55 
       
    56 /*
       
    57  *
       
    58  * vdxGetVolHeader
       
    59  *
       
    60  * Parameters:
       
    61  *    inBuffer                   pointer to bit buffer instance
       
    62  *    header                     output parameters: VOL header
       
    63  *    bitErrorIndication         non-zero if a bit error has been detected
       
    64  *                               within the bits accessed in this function,
       
    65  *                               see biterr.h for possible values
       
    66  *
       
    67  * Function:
       
    68  *    This function reads the VO and VOL header from inBuffer.
       
    69  *
       
    70  * Returns:
       
    71  *    VDX_OK                     the function was successful
       
    72  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
    73  *                               occured
       
    74  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
    75  *    VDX_ERR_NO_START_CODE      if start code is not found
       
    76  *    VDX_ERR_NOT_SUPPORTED      if VOL header 
       
    77  *
       
    78  *    
       
    79  */
       
    80 
       
    81 int vdxGetVolHeader(
       
    82    bibBuffer_t *inBuffer,
       
    83    vdxVolHeader_t *header,
       
    84    int *bitErrorIndication,
       
    85    int getInfo, int *aByteIndex, int *aBitIndex, CMPEG4Transcoder *hTranscoder)
       
    86 {
       
    87    int bitsGot, sncCode, fUseDefaultVBVParams = 0, num_bits;
       
    88    int16 bibError = 0;
       
    89    u_int32 bits;
       
    90 
       
    91    memset(header, 0, sizeof(vdxVolHeader_t));
       
    92    *bitErrorIndication = 0;
       
    93 
       
    94    /* if Visual Object Sequence Start Code is present */
       
    95    bits = bibShowBits(MP4_VOS_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
    96    if (bits == MP4_VOS_START_CODE) {
       
    97 
       
    98       /* vos_start_code */
       
    99       bibFlushBits(MP4_VOS_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   100 
       
   101       /* profile_and_level_indication (8 bits) */
       
   102       bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   103       /* If fatal bit error occurred (lost segment/packet etc.) */
       
   104       /*   
       
   105            1: Simple Profile Level 1   (from ISO/IEC 14496-2:1999/FPDAM4 [N4350] July 2001)
       
   106            2: Simple Profile Level 2   (from ISO/IEC 14496-2:1999/FPDAM4 [N4350] July 2001) 
       
   107            3: Simple Profile Level 3   (from ISO/IEC 14496-2:1999/FPDAM4 [N4350] July 2001) 
       
   108            8: Simple Profile Level 0   (from ISO/IEC 14496-2:1999/FPDAM4 [N5743] July 2003)
       
   109            9: Simple Profile Level 0b  (from ISO/IEC 14496-2:1999/FPDAM4 [N5743] July 2003)
       
   110       */
       
   111 #if 0
       
   112       // Disabled since some clips have this set incorrectly, and this is not used for anything important.
       
   113       // Only simple profile clips seem to be available so this should not cause any harm.
       
   114       // Further, it is still checked in vedVolReader which is always used before editing. It is not checked only when creating thumbnails
       
   115       
       
   116       
       
   117       if (bits != 1 && bits != 2 && bits != 3 && bits != 8 && bits != 9) {
       
   118          /* this is not a supported simple profile stream */
       
   119          deb("vdxGetMPEGVolHeader: ERROR - This is not a supported simple profile stream\n");
       
   120          goto notSupported;
       
   121       }
       
   122 #endif
       
   123       if (bits != 8 && bits != 9) 
       
   124         header->profile_level = (int) bits;
       
   125       else
       
   126         header->profile_level = 0;
       
   127 
       
   128       /* User data if available */
       
   129       bits = bibShowBits(32, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   130       if (bits == MP4_USER_DATA_START_CODE)
       
   131       {
       
   132            if (!header->user_data) {
       
   133                header->user_data = (char *) malloc(MAX_USER_DATA_LENGTH);
       
   134                header->user_data_length = 0;
       
   135            }
       
   136 
       
   137            if (vdxGetUserData(inBuffer, header->user_data, &(header->user_data_length), bitErrorIndication) != VDX_OK)
       
   138                /* also bibError will be handled in exitAfterBitError */
       
   139                goto exitAfterBitError;               
       
   140        }
       
   141    }
       
   142 
       
   143    /* if Visual Object Start Code is present */
       
   144    bits = bibShowBits(MP4_VO_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   145    if (bits == MP4_VO_START_CODE) {
       
   146 
       
   147       /* visual_object_start_code */
       
   148       bibFlushBits(MP4_VO_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   149 
       
   150       /* is_visual_object_identifier (1 bit) */
       
   151       bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   152       if (bits) {
       
   153 
       
   154          /* visual_object_ver_id (4 bits) */
       
   155          bits = bibGetBits(4, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   156          if (bits != 1) {
       
   157             /* this is not an MPEG-4 version 1 stream */
       
   158             deb("vdxGetMPEGVolHeader: ERROR - This is not an MPEG-4 version 1 stream\n");
       
   159             goto notSupported;
       
   160          }
       
   161 
       
   162          /* visual_object_priority (3 bits) */
       
   163          bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   164          header->vo_priority = (int) bits;
       
   165 
       
   166       } else {
       
   167          header->vo_priority = 0;
       
   168       }
       
   169 
       
   170       /* visual_object_type (4 bits) */
       
   171       bits = bibGetBits(4, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   172       if (bits != 1) {
       
   173          /* this is not a video object */
       
   174          deb("vdxGetMPEGVolHeader: ERROR - This is not a video object\n");
       
   175          goto notSupported;
       
   176       }
       
   177 
       
   178       /* is_video_signal_type (1 bit) */
       
   179       bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   180       if (bits) {
       
   181 
       
   182            /* Note: The following fields in the bitstream give information about the 
       
   183               video signal type before encoding. These parameters don't influence the 
       
   184               decoding algorithm, but the composition at the output of the video decoder.
       
   185               There is no normative requirement however to utilize this information 
       
   186               during composition, therefore until a way to utilize them is found in
       
   187               MoViDe, these fields are just dummyly read, but not interpreted.
       
   188               For interpretation see the MPEG-4 Visual standard */
       
   189 
       
   190          /* video_format (3 bits) */
       
   191          bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   192          header->video_format = (int) bits;
       
   193 
       
   194          /* video_range (1 bit) */
       
   195          bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   196          header->video_range = (int) bits;
       
   197 
       
   198          /* colour_description (1 bit) */
       
   199          bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   200          if (bits) {
       
   201              
       
   202             /* colour_primaries (8 bits) */
       
   203             bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   204             header->colour_primaries = (int) bits;
       
   205 
       
   206             /* transfer_characteristics (8 bits) */
       
   207             bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   208             header->transfer_characteristics = (int) bits;
       
   209 
       
   210             /* matrix_coefficients (8 bits) */
       
   211             bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   212             header->matrix_coefficients = (int) bits;
       
   213          } else {
       
   214 
       
   215             header->colour_primaries = 1; /* default: ITU-R BT.709 */
       
   216             header->transfer_characteristics = 1; /* default: ITU-R BT.709 */
       
   217             header->matrix_coefficients = 1; /* default: ITU-R BT.709 */
       
   218          }
       
   219 
       
   220       } else {
       
   221 
       
   222          /* default values */
       
   223          header->video_format = 5; /* Unspecified video format */
       
   224          header->video_range = 0; /* Y range 16-235 pixel values */
       
   225          header->colour_primaries = 1; /* ITU-R BT.709 */
       
   226          header->transfer_characteristics = 1; /* ITU-R BT.709 */
       
   227          header->matrix_coefficients = 1; /* ITU-R BT.709 */
       
   228       }
       
   229 
       
   230       /* check the next start code */
       
   231       sncCode = sncCheckMpegSync(inBuffer, 0, &bibError);
       
   232        
       
   233       /* If User data is available */
       
   234       if (sncCode == SNC_USERDATA)
       
   235       {
       
   236          if (!header->user_data) {
       
   237             header->user_data = (char *) malloc(MAX_USER_DATA_LENGTH);
       
   238             header->user_data_length = 0;
       
   239          }
       
   240 
       
   241          if (vdxGetUserData(inBuffer, header->user_data, &(header->user_data_length), bitErrorIndication) != VDX_OK)
       
   242             /* also bibError will be handled in exitAfterBitError */
       
   243             goto exitAfterBitError;      
       
   244            
       
   245       } else if (sncCode != SNC_VID) {
       
   246 
       
   247          deb("vdxGetMPEGVolHeader: ERROR. No Start code after VO header\n");
       
   248          goto exitAfterBitError;
       
   249       }
       
   250    }
       
   251    
       
   252    /* if Video Object Start Code is present */
       
   253    bits = bibShowBits(MP4_VID_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   254    if (bits == MP4_VID_START_CODE) {
       
   255        /* video_object_start_code */
       
   256       bibFlushBits(MP4_VID_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   257 
       
   258       /* video_object_id */
       
   259       bits = bibGetBits(MP4_VID_ID_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   260       header->vo_id = (int) bits;
       
   261    }
       
   262 
       
   263    /* MVE */
       
   264    /* dummy indication of the position before VOL start code, only for recoginition of shortheader */
       
   265    if (!getInfo)
       
   266    {
       
   267        hTranscoder->ErrorResilienceInfo(NULL, inBuffer->getIndex, inBuffer->bitIndex);
       
   268    }
       
   269 
       
   270    /* vol_start_code */
       
   271    bits = bibShowBits(MP4_VOL_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   272    if(bits != MP4_VOL_START_CODE)
       
   273    {
       
   274       /* If H.263 PSC, this is a short header stream, i.e. H.263 baseline */
       
   275       if ( (bits >> 6) == 32 ) {
       
   276          return VDX_OK;
       
   277       } else {      
       
   278          deb("vdxGetMPEGVolHeader: ERROR - Bitstream does not start with MP4_VOL_START_CODE\n");
       
   279          goto exitAfterBitError;
       
   280       }  
       
   281    }
       
   282 
       
   283    bibFlushBits(MP4_VOL_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   284 
       
   285    /* vol_id */
       
   286    bits = bibGetBits(MP4_VOL_ID_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   287    header->vol_id = (int) bits;
       
   288 
       
   289    /* random_accessible_vol (1 bit) */
       
   290    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   291    header->random_accessible_vol = (u_char) bits;
       
   292 
       
   293    /* video_object_type_indication (8 bits) */
       
   294    bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   295    if (bits != 1) {
       
   296        /* this is not a simple video object stream */
       
   297        deb("vdxGetMPEGVolHeader: ERROR - This is not a simple video object stream\n");
       
   298        goto notSupported;
       
   299    }
       
   300 
       
   301    /* is_object_layer_identifier (1 bit) */
       
   302    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   303    if (bits) {
       
   304       /* video_object_layer_verid (4 bits) */
       
   305       bits = bibGetBits(4, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   306       if (bits != 1) {
       
   307          /* this is not an MPEG-4 version 1 stream */
       
   308          deb("vdxGetMPEGVolHeader: ERROR - This is not an MPEG-4 version 1 stream\n");
       
   309          goto notSupported;
       
   310       }
       
   311 
       
   312       /* video_object_layer_priority (3 bits) */
       
   313       bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   314       header->vo_priority = (int) bits;
       
   315    } 
       
   316 
       
   317    /* aspect_ratio_info: `0010`- 12:11 (625-type for 4:3 picture) */
       
   318    bits = bibGetBits(4, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   319    header->pixel_aspect_ratio = (int) bits;
       
   320 
       
   321    /* extended par */
       
   322    if (bits == 15) { 
       
   323       /* par_width */
       
   324       bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   325       /* par_height */
       
   326       bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   327    }
       
   328    
       
   329    /* vol_control_parameters flag */
       
   330    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   331    if (bits) {
       
   332 
       
   333       /* chroma_format (2 bits) */
       
   334       bits = bibGetBits(2, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   335       if (bits != 1) {
       
   336          goto exitAfterBitError;
       
   337       }
       
   338 
       
   339       /* low_delay (1 bits) */
       
   340       bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   341 
       
   342       /* vbv_parameters (1 bits) */
       
   343       bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   344       if (bits) {
       
   345 
       
   346          /* first_half_bitrate (15 bits) */
       
   347          bits = bibGetBits(15, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   348          header->bit_rate = (bits << 15);
       
   349           
       
   350          /* marker_bit */
       
   351          if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   352             goto exitAfterBitError;
       
   353            
       
   354          /* latter_half_bitrate (15 bits) */
       
   355          bits = bibGetBits(15, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   356          header->bit_rate += bits;
       
   357          header->bit_rate *= 400;
       
   358            
       
   359          /* marker_bit */
       
   360          if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   361             goto exitAfterBitError;
       
   362 
       
   363          /* first_half_vbv_buffer_size (15 bits) */
       
   364          bits = bibGetBits(15, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   365          header->vbv_buffer_size = (bits << 3);
       
   366            
       
   367          /* marker_bit */
       
   368          if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   369             goto exitAfterBitError;
       
   370            
       
   371          /* latter_half_vbv_buffer_size (3 bits) */
       
   372          bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   373          header->vbv_buffer_size += bits;
       
   374          header->vbv_buffer_size <<= 14 /**= 16384*/;
       
   375            
       
   376          /* first_half_vbv_occupancy (11 bits) */
       
   377          bits = bibGetBits(11, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   378          header->vbv_occupancy = (bits << 15);
       
   379            
       
   380          /* marker_bit */
       
   381          if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   382             goto exitAfterBitError;
       
   383            
       
   384          /* latter_half_vbv_occupancy (15 bits) */
       
   385          bits = bibGetBits(15, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   386          header->vbv_occupancy += bits;
       
   387          header->vbv_occupancy <<= 6 /**= 64*/;
       
   388            
       
   389          /* marker_bit */
       
   390          if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   391             goto exitAfterBitError;
       
   392 
       
   393       } else {
       
   394          fUseDefaultVBVParams = 1;
       
   395       }
       
   396    } else {
       
   397       fUseDefaultVBVParams = 1;
       
   398    }
       
   399 
       
   400    if (fUseDefaultVBVParams) {
       
   401 
       
   402       /* default values */
       
   403       header->vbv_buffer_size = 
       
   404         ((header->profile_level == 0 || header->profile_level == 1) ? 5 : 20);
       
   405       header->vbv_occupancy = header->vbv_buffer_size*170;
       
   406       header->bit_rate = 
       
   407         ((header->profile_level == 0 || header->profile_level == 1) ? 64 : 
       
   408         ((header->profile_level == 2) ? 128 : 384));
       
   409        
       
   410       header->vbv_occupancy <<= 6 /**= 64*/;
       
   411       header->vbv_buffer_size <<= 14 /**= 16384*/;
       
   412       header->bit_rate <<= 10 /**= 1024*/;
       
   413    }
       
   414 
       
   415    /* vol_shape (2 bits) */
       
   416    bits = bibGetBits(2, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   417    /* rectangular_shape = '00' */
       
   418    if (bits != 0) {
       
   419       deb("vdxGetMPEGVolHeader: ERROR - Not rectangular shape is not supported\n");
       
   420       goto notSupported;
       
   421    }
       
   422 
       
   423    /* marker_bit */
       
   424    if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   425       goto exitAfterBitError;
       
   426     
       
   427    /* MVE */
       
   428    if (!getInfo)
       
   429    {
       
   430        hTranscoder->MPEG4TimerResolution(inBuffer->getIndex, inBuffer->bitIndex);
       
   431    }
       
   432    
       
   433    /* time_increment_resolution */
       
   434    bits = bibGetBits(16, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   435    header->time_increment_resolution = (int) bits;
       
   436  
       
   437    /* marker_bit */
       
   438    if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   439       goto exitAfterBitError;
       
   440     
       
   441    /* fixed_vop_rate */
       
   442    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   443 
       
   444    /* MVE */
       
   445    header->fixed_vop_rate = (u_char)bits;
       
   446 
       
   447    /* fixed_vop_time_increment (1-15 bits) */
       
   448    if (bits) {
       
   449       for (num_bits = 1; ((header->time_increment_resolution-1) >> num_bits) != 0; num_bits++)
       
   450         {
       
   451         }
       
   452  
       
   453       bits = bibGetBits(num_bits, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   454    }
       
   455 
       
   456    /* if rectangular_shape !always! */
       
   457 
       
   458    /* marker_bit */
       
   459    if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   460       goto exitAfterBitError;
       
   461     
       
   462    /* vol_width (13 bits) */
       
   463    bits = bibGetBits(13, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   464    header->lumWidth = (int) bits;
       
   465 
       
   466    /* marker_bit */
       
   467    if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   468       goto exitAfterBitError;
       
   469     
       
   470    /* vol_height (13 bits) */
       
   471    bits = bibGetBits(13, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   472    header->lumHeight = (int) bits;
       
   473 
       
   474    /* endif rectangular_shape */
       
   475 
       
   476    /* marker_bit */
       
   477    if (!bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError))
       
   478       goto exitAfterBitError;
       
   479     
       
   480    /* interlaced (1 bit) */
       
   481    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   482    if (bits)
       
   483    {
       
   484       deb("vdxGetMPEGVolHeader: ERROR - Interlaced VOP not supported\n");
       
   485       goto notSupported;
       
   486    }
       
   487 
       
   488    /* OBMC_disable */
       
   489    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   490    if (!bits) {
       
   491      deb("vdxGetMPEGVolHeader: ERROR - Overlapped motion compensation not supported\n");
       
   492      goto notSupported;
       
   493    }
       
   494 
       
   495    /* sprite_enable (1 bit) */
       
   496    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   497    if (bits) {
       
   498      deb("vdxGetMPEGVolHeader: ERROR - Sprites not supported\n");
       
   499      goto notSupported;
       
   500    }
       
   501 
       
   502    /* not_8_bit (1 bit) */
       
   503    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   504    if (bits) {
       
   505      deb("vdxGetMPEGVolHeader: ERROR - Not 8 bits/pixel not supported\n");
       
   506      goto notSupported;
       
   507    }
       
   508 
       
   509    /* quant_type (1 bit) */
       
   510    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   511    if (bits) {
       
   512      deb("vdxGetMPEGVolHeader: ERROR - H.263/MPEG-2 Quant Table switch not supported\n");
       
   513      goto notSupported;
       
   514    }
       
   515 
       
   516    /* complexity_estimation_disable (1 bit) */
       
   517    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   518    if (!bits) {
       
   519      deb("vdxGetMPEGVolHeader: ERROR - Complexity estimation header not supported\n");
       
   520      goto notSupported;
       
   521    }
       
   522   
       
   523    /* MVE */
       
   524    if (!getInfo)
       
   525    {
       
   526        hTranscoder->ErrorResilienceInfo(NULL, inBuffer->getIndex, inBuffer->bitIndex);
       
   527    }
       
   528    else
       
   529    {
       
   530         *aByteIndex = inBuffer->getIndex;
       
   531         *aBitIndex = inBuffer->bitIndex;
       
   532    }
       
   533 
       
   534    /* resync_marker_disable (1 bit) */
       
   535    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   536    header->error_res_disable = (u_char) bits;
       
   537 
       
   538    /* data_partitioned (1 bit) */
       
   539    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   540    header->data_partitioned = (u_char) bits;
       
   541 
       
   542    if (header->data_partitioned) {
       
   543       /* reversible_vlc (1 bit) */
       
   544       bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   545       header->reversible_vlc = (u_char) bits;
       
   546    }
       
   547 
       
   548    /* MVE */
       
   549    if (!getInfo)
       
   550    {
       
   551        hTranscoder->ErrorResilienceInfo(header, 0, 0);
       
   552    }
       
   553 
       
   554    /* scalability (1 bit) */
       
   555    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   556    if (bits) {
       
   557      deb("vdxGetMPEGVolHeader: ERROR - Scalability not supported\n");
       
   558      goto notSupported;
       
   559    }
       
   560 
       
   561    /* check the next start code */
       
   562    sncCode = sncCheckMpegSync(inBuffer, 0, &bibError);
       
   563 
       
   564    /* Removed since the VOL header may be followed by another header (H.245 & MPEG-4 signaling)
       
   565       and sncCheckMpegSync does not recognize VOS start code
       
   566    if (sncCode == SNC_NO_SYNC || bibError)
       
   567    {
       
   568       deb("vdxGetMPEGVolHeader: ERROR. No Start code after VOL header\n");
       
   569       goto exitAfterBitError;
       
   570    } 
       
   571    */
       
   572    /* If User data is available */
       
   573    if (sncCode == SNC_USERDATA && !bibError)
       
   574    {
       
   575       if (!header->user_data) {
       
   576          header->user_data = (char *) malloc(MAX_USER_DATA_LENGTH);
       
   577          header->user_data_length = 0;
       
   578       }
       
   579        
       
   580       if (vdxGetUserData(inBuffer, header->user_data, &(header->user_data_length), bitErrorIndication) != VDX_OK)
       
   581          /* also bibError will be handled in exitAfterBitError */
       
   582          goto exitAfterBitError;       
       
   583    }
       
   584 
       
   585    /* If no error in bit buffer functions */
       
   586    if (!bibError)
       
   587       return VDX_OK;
       
   588 
       
   589    /* Else if ran out of data (i.e. decoding out of sync) */
       
   590    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
   591       return VDX_OK_BUT_BIT_ERROR;
       
   592    }
       
   593 
       
   594    /* Else other error in bit buffer functions */
       
   595    else
       
   596       return VDX_ERR_BIB;
       
   597 
       
   598    exitAfterBitError:
       
   599    if (bibError && bibError != ERR_BIB_NOT_ENOUGH_DATA)
       
   600        return VDX_ERR_BIB;
       
   601 
       
   602    return VDX_OK_BUT_BIT_ERROR;
       
   603 
       
   604    notSupported:
       
   605    return VDX_ERR_NOT_SUPPORTED;
       
   606 }
       
   607 
       
   608 /* {{-output"vdxGetGovHeader.txt"}} */
       
   609 /*
       
   610  *
       
   611  * vdxGetGovHeader
       
   612  *
       
   613  * Parameters:
       
   614  *    inBuffer                   pointer to bit buffer instance
       
   615  *    header                     output parameters: picture header
       
   616  *    bitErrorIndication         non-zero if a bit error has been detected
       
   617  *                               within the bits accessed in this function,
       
   618  *                               see biterr.h for possible values
       
   619  *
       
   620  * Function:
       
   621  *    This function reads the GOV header from inBuffer.
       
   622  *
       
   623  * Returns:
       
   624  *    VDX_OK                     the function was successful
       
   625  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
   626  *                               occured
       
   627  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
   628  *    VDX_ERR_NO_START_CODE      if start code is not found
       
   629  *    VDX_ERR_NOT_SUPPORTED      if broken_link and closed_gov conflict
       
   630  *
       
   631  */
       
   632 
       
   633 int vdxGetGovHeader(
       
   634    bibBuffer_t *inBuffer,
       
   635    vdxGovHeader_t *header,
       
   636    int *bitErrorIndication)
       
   637 /* {{-output"vdxGetGovHeader.txt"}} */
       
   638 {
       
   639    int tmpvar;
       
   640    u_int32 bits;
       
   641    int time_s;
       
   642    int bitsGot, sncCode;
       
   643    int16 bibError = 0;
       
   644 
       
   645    vdxAssert(inBuffer != NULL);
       
   646    vdxAssert(header != NULL);
       
   647    vdxAssert(bitErrorIndication != NULL);
       
   648 
       
   649    memset(header, 0, sizeof(vdxGovHeader_t));
       
   650    *bitErrorIndication = 0;
       
   651 
       
   652    /* group_start_code (32 bits) */
       
   653    bits = bibGetBits(32, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   654 
       
   655    if ( bibError )
       
   656        goto exitAfterBitError;
       
   657    if( bits != MP4_GROUP_START_CODE )
       
   658    {
       
   659       deb0p("ERROR. Bitstream does not start with MP4_GROUP_START_CODE\n");
       
   660       goto exitAfterBitError;
       
   661    }
       
   662 
       
   663    /* time_code_hours (5 bits) */
       
   664    tmpvar = (int) bibGetBits(5, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   665 
       
   666    if ( bibError )
       
   667        goto exitAfterBitError;
       
   668 
       
   669    time_s= tmpvar*3600;
       
   670  
       
   671    /* time_code_minutes (6 bits) */
       
   672    tmpvar = (int) bibGetBits(6, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   673 
       
   674    if ( bibError )
       
   675        goto exitAfterBitError;
       
   676 
       
   677    time_s += tmpvar*60;
       
   678 
       
   679    /* marker_bit (1 bit) */
       
   680    tmpvar = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   681    if ( bibError )
       
   682        goto exitAfterBitError;
       
   683    if ( !tmpvar )
       
   684       goto exitAfterBitError;
       
   685 
       
   686    /* time_code_seconds (6 bits) */
       
   687    tmpvar = (int) bibGetBits(6, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   688     
       
   689    time_s += tmpvar;
       
   690 
       
   691    /* closed_gov (1 bit) */
       
   692    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   693    if ( bibError )
       
   694        goto exitAfterBitError;
       
   695 
       
   696    header->closed_gov= (u_char) bits;
       
   697 
       
   698    /* broken_link (1 bit) */
       
   699    bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   700    if ( bibError )
       
   701        goto exitAfterBitError;
       
   702    
       
   703    header->broken_link = (u_char) bits;   
       
   704 
       
   705    if ( (header->closed_gov == 0)&&(header->broken_link == 1) )
       
   706    {
       
   707       deb0p("ERROR. GOVHeader: closed_gov = 0\tbroken_link = 1\n");
       
   708       goto exitAfterBitError;
       
   709    }
       
   710 
       
   711    /* Stuff the bits before the next start code */
       
   712    sncCode = sncCheckMpegSync(inBuffer, 1, &bibError);
       
   713    if ((sncCode == SNC_NO_SYNC) || bibError)
       
   714    {
       
   715       deb0p("ERROR. No VOP Start code after GOV header\n");
       
   716       /* also bibError will be handled in exitAfterBitError */
       
   717       goto exitAfterBitError;
       
   718    }
       
   719 
       
   720     /* If User data is available */
       
   721    if (sncCode == SNC_USERDATA)
       
   722    {
       
   723       if (!header->user_data) {
       
   724          header->user_data = (char *) malloc(MAX_USER_DATA_LENGTH);
       
   725          header->user_data_length = 0;
       
   726       }
       
   727        
       
   728       if (vdxGetUserData(inBuffer, header->user_data, &(header->user_data_length), bitErrorIndication) != VDX_OK)
       
   729           /* also bibError will be handled in exitAfterBitError */
       
   730           goto exitAfterBitError;       
       
   731    }
       
   732 
       
   733    header->time_stamp = time_s;
       
   734 
       
   735    /* If no error in bit buffer functions */
       
   736    if (!bibError)
       
   737       return VDX_OK;
       
   738 
       
   739    /* Else if ran out of data (i.e. decoding out of sync) */
       
   740    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
   741       return VDX_OK_BUT_BIT_ERROR;
       
   742    }
       
   743 
       
   744    /* Else other error in bit buffer functions */
       
   745    else
       
   746       return VDX_ERR_BIB;
       
   747 
       
   748    exitAfterBitError:
       
   749       if (bibError && bibError != ERR_BIB_NOT_ENOUGH_DATA)
       
   750          return VDX_ERR_BIB;
       
   751 
       
   752    return VDX_OK_BUT_BIT_ERROR;
       
   753 
       
   754 } 
       
   755 
       
   756 /* {{-output"vdxGetVopHeader.txt"}} */
       
   757 /*
       
   758  *
       
   759  * vdxGetVopHeader
       
   760  *
       
   761  * Parameters:
       
   762  *    inBuffer                   pointer to bit buffer instance
       
   763  *    inParam                    input parameters
       
   764  *    header                     output parameters: picture header
       
   765  *    bitErrorIndication         non-zero if a bit error has been detected
       
   766  *                               within the bits accessed in this function,
       
   767  *                               see biterr.h for possible values
       
   768  *
       
   769  * Function:
       
   770  *    This function reads the VOP header from inBuffer.
       
   771  *
       
   772  * Returns:
       
   773  *    VDX_OK                     the function was successful
       
   774  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
   775  *                               occured
       
   776  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
   777  *    VDX_ERR_NO_START_CODE      if start code is not found
       
   778  *    VDX_ERR_NOT_SUPPORTED      if broken_link and closed_gov conflict
       
   779  *
       
   780  *    
       
   781  */
       
   782 
       
   783 int vdxGetVopHeader(
       
   784    bibBuffer_t *inBuffer,
       
   785    const vdxGetVopHeaderInputParam_t *inpParam,
       
   786    vdxVopHeader_t *header,
       
   787    int * ModuloByteIndex,
       
   788    int * ModuloBitIndex,
       
   789    int * ByteIndex,
       
   790    int * BitIndex,
       
   791    int *bitErrorIndication)
       
   792 /* {{-output"vdxGetVopHeader.txt"}} */
       
   793 {
       
   794 
       
   795    int tmpvar;
       
   796    u_int32 bits;
       
   797    int bitsGot;
       
   798    int time_base_incr = 0,
       
   799       num_bits;
       
   800    int16
       
   801       bibError = 0;
       
   802 
       
   803    vdxAssert(inBuffer != NULL);
       
   804    vdxAssert(header != NULL);
       
   805    vdxAssert(bitErrorIndication != NULL);
       
   806 
       
   807    memset(header, 0, sizeof(vdxVopHeader_t));
       
   808    *bitErrorIndication = 0;
       
   809 
       
   810    /* vop_start_code (32 bits) */
       
   811    bits = bibGetBits(MP4_VOP_START_CODE_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   812    if ( bibError )
       
   813       goto exitFunction;
       
   814  
       
   815    if( bits != MP4_VOP_START_CODE  )
       
   816    {
       
   817       deb0p("ERROR. Bitstream does not start with MP4_VOP_START_CODE\n");
       
   818       goto exitAfterBitError;
       
   819    }
       
   820 
       
   821    *bitErrorIndication = 0;   
       
   822 
       
   823    /* vop_prediction_type (2 bits) */
       
   824    bits = bibGetBits(2, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   825    if ( bibError )
       
   826        goto exitAfterBitError;
       
   827 
       
   828    if ( (bits != VDX_VOP_TYPE_P && bits != VDX_VOP_TYPE_I) )
       
   829    {
       
   830       deb("ERROR. Not supported VOP prediction type\n");
       
   831       goto exitAfterBitError;
       
   832    }
       
   833   
       
   834    header->coding_type = (u_char) bits;
       
   835 
       
   836    /* MVE */
       
   837    *ModuloByteIndex = inBuffer->numBytesRead;
       
   838    *ModuloBitIndex = inBuffer->bitIndex;
       
   839 
       
   840    /* modulo_time_base (? bits) */
       
   841    tmpvar = (int) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   842    if ( bibError )
       
   843        goto exitAfterBitError;
       
   844 
       
   845    while( tmpvar == 1 && !bibError )
       
   846    {
       
   847       tmpvar = (int) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   848       time_base_incr++;
       
   849    }
       
   850    if ( bibError )
       
   851        goto exitAfterBitError;
       
   852 
       
   853    header->time_base_incr = time_base_incr;
       
   854 
       
   855    /* marker_bit (1 bit) */
       
   856    tmpvar = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   857    if ( bibError )
       
   858        goto exitAfterBitError;
       
   859    if ( !tmpvar )
       
   860       goto exitAfterBitError;
       
   861 
       
   862    /* MVE */
       
   863    *ByteIndex = inBuffer->numBytesRead;
       
   864    *BitIndex = inBuffer->bitIndex;
       
   865 
       
   866    /* vop_time_increment (1-16 bits) */
       
   867    for (num_bits = 1; ((inpParam->time_increment_resolution-1) >> num_bits) != 0; num_bits++)
       
   868     {
       
   869     }
       
   870  
       
   871    tmpvar = (int) bibGetBits(num_bits, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   872    if ( bibError )
       
   873        goto exitAfterBitError;
       
   874 
       
   875    header->time_inc = tmpvar;
       
   876 
       
   877    /* marker_bit (1 bit) */
       
   878    tmpvar = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   879    if ( bibError )
       
   880        goto exitAfterBitError;
       
   881    if ( !tmpvar )
       
   882       goto exitAfterBitError;
       
   883 
       
   884    /* vop_coded (1 bit) */
       
   885    header->vop_coded = (u_char) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   886    if ( bibError )
       
   887        goto exitAfterBitError;
       
   888 
       
   889    if ( !header->vop_coded ) {
       
   890       goto exitAfterBitError;
       
   891    }
       
   892    
       
   893    /* vop_rounding_type (1 bit) */
       
   894    if (header->coding_type == VDX_VOP_TYPE_P)
       
   895    {
       
   896       bits = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   897       if ( bibError )
       
   898           goto exitAfterBitError;
       
   899       header->rounding_type = (int) bits;
       
   900    } else
       
   901       header->rounding_type = 0;
       
   902 
       
   903    /* intra_dc_vlc_thr (3 bits) */
       
   904    bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   905    if ( bibError )
       
   906        goto exitAfterBitError;
       
   907 
       
   908    header->intra_dc_vlc_thr = (int) bits;
       
   909 
       
   910    /* vop_quant (5 bits) */
       
   911    bits = bibGetBits(5, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   912    if ( bibError )
       
   913        goto exitAfterBitError;
       
   914 
       
   915    header->quant = (int) bits;
       
   916 
       
   917    /* vop_fcode_forward (3 bits) */
       
   918    if (header->coding_type == VDX_VOP_TYPE_P)
       
   919    {
       
   920       bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
   921       if ( bibError )
       
   922           goto exitAfterBitError;
       
   923       header->fcode_forward = (int) bits;
       
   924    } else {
       
   925       /* In case of an Intra Frame to calculate the length of the 
       
   926          VP resynchronization marker later on fcode_forward should be 
       
   927          assumed to have the value 1. */
       
   928       header->fcode_forward = 1;
       
   929    }
       
   930 
       
   931    exitFunction:
       
   932 
       
   933    /* If no error in bit buffer functions */
       
   934    if (!bibError)
       
   935       return VDX_OK;
       
   936 
       
   937    /* Else if ran out of data (i.e. decoding out of sync) */
       
   938    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
   939       return VDX_OK_BUT_BIT_ERROR;
       
   940    }
       
   941 
       
   942    /* Else other error in bit buffer functions */
       
   943    else
       
   944       return VDX_ERR_BIB;
       
   945 
       
   946    exitAfterBitError:
       
   947    
       
   948    if (bibError && bibError != ERR_BIB_NOT_ENOUGH_DATA)
       
   949       return VDX_ERR_BIB;
       
   950 
       
   951    return VDX_OK_BUT_BIT_ERROR;
       
   952 
       
   953 }
       
   954 
       
   955 /*
       
   956  * vdxGetUserData
       
   957  *    
       
   958  *
       
   959  * Parameters:
       
   960  *    inBuffer                   pointer to bit buffer instance
       
   961  *    user_data                  string of user data read byte-by-byte from
       
   962  *                               the stream
       
   963  *    user_data_length           number of bytes of user data
       
   964  *    bitErrorIndication         non-zero if a bit error has been detected
       
   965  *                               within the bits accessed in this function,
       
   966  *                               see biterr.h for possible values
       
   967  *
       
   968  * Function:
       
   969  *    This function reads a string of bytes as user data from the bitsream
       
   970  *    It is called from te VOL header or GOV header
       
   971  *
       
   972  * Returns:
       
   973  *    VDX_OK                     the function was successful
       
   974  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
   975  *                               occured
       
   976  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
   977  *
       
   978  *    
       
   979  */
       
   980 int vdxGetUserData(bibBuffer_t *inBuffer,
       
   981    char *user_data, int *user_data_length,
       
   982    int *bitErrorIndication)
       
   983 {
       
   984    int i;
       
   985    u_int32 bits;
       
   986    int bitsGot;
       
   987    int16 ownError=0;
       
   988 
       
   989    /* user_data_start_code (32 bits) */
       
   990    bits = bibGetBits(32, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
   991    if ( ownError )
       
   992       goto exitFunction;
       
   993  
       
   994    if (bits != MP4_USER_DATA_START_CODE)
       
   995    {
       
   996       deb0p("ERROR. Bitstream does not start with MP4_USER_DATA_START_CODE\n");
       
   997       goto exitFunction;
       
   998    }
       
   999 
       
  1000    /* read until start_code 0x000001 */
       
  1001    for ( i=(*user_data_length);
       
  1002          (((bits = bibShowBits(24, inBuffer, &bitsGot, bitErrorIndication, &ownError)) != 0x000001)&&(ownError==0));
       
  1003          i++ )
       
  1004    {
       
  1005       bits = bibGetBits(8, inBuffer, &bitsGot, bitErrorIndication, &ownError);
       
  1006       if (ownError)
       
  1007          goto exitFunction;
       
  1008 
       
  1009       if (i<MAX_USER_DATA_LENGTH) 
       
  1010       {
       
  1011           user_data[i] = (u_char) bits;
       
  1012       }
       
  1013       else
       
  1014       {
       
  1015           break;
       
  1016       }
       
  1017    }
       
  1018    
       
  1019 
       
  1020    *user_data_length = (i>=MAX_USER_DATA_LENGTH) ? MAX_USER_DATA_LENGTH : i++;
       
  1021 
       
  1022    exitFunction:
       
  1023 
       
  1024    /* If no error in bit buffer functions */
       
  1025    if (!ownError)
       
  1026       return VDX_OK;
       
  1027 
       
  1028    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1029    else if (ownError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1030       return VDX_OK_BUT_BIT_ERROR;
       
  1031    }
       
  1032 
       
  1033    /* Else other error in bit buffer functions */
       
  1034    else
       
  1035       return VDX_ERR_BIB;
       
  1036 
       
  1037 }
       
  1038 
       
  1039 /*
       
  1040  * Video Packet Layer Global Functions
       
  1041  */
       
  1042 
       
  1043 /* {{-output"vdxGetVideoPacketHeader.txt"}} */
       
  1044 /*
       
  1045  *
       
  1046  * vdxGetVideoPacketHeader
       
  1047  *
       
  1048  * Parameters:
       
  1049  *    inBuffer                   pointer to bit buffer instance
       
  1050  *    header                     output parameters: picture header
       
  1051  *    bitErrorIndication         non-zero if a bit error has been detected
       
  1052  *                               within the bits accessed in this function,
       
  1053  *                               see biterr.h for possible values
       
  1054  *
       
  1055  * Function:
       
  1056  *    This function reads the Video Packet header from inBuffer.
       
  1057  *
       
  1058  * Returns:
       
  1059  *    VDX_OK                     the function was successful
       
  1060  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1061  *                               occured
       
  1062  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1063  *    VDX_ERR_NO_START_CODE      if start code is not found
       
  1064  *    VDX_ERR_NOT_SUPPORTED      if broken_link and closed_gov conflict
       
  1065  *
       
  1066  *      
       
  1067  */
       
  1068 
       
  1069 int vdxGetVideoPacketHeader(
       
  1070    bibBuffer_t *inBuffer,
       
  1071    const vdxGetVideoPacketHeaderInputParam_t *inpParam,
       
  1072    vdxVideoPacketHeader_t *header,
       
  1073    int *bitErrorIndication)
       
  1074 /* {{-output"vdxGetVideoPacketHeader.txt"}} */
       
  1075 {
       
  1076    int tmpvar, num_bits, bitsGot;
       
  1077    u_int32 bits;
       
  1078    int MBNumLength,
       
  1079       time_base_incr = 0;
       
  1080    int16
       
  1081       bibError = 0;
       
  1082 
       
  1083    vdxAssert(inBuffer != NULL);
       
  1084    vdxAssert(header != NULL);
       
  1085    vdxAssert(bitErrorIndication != NULL);
       
  1086 
       
  1087    memset(header, 0, sizeof(vdxVideoPacketHeader_t));
       
  1088    *bitErrorIndication = 0;
       
  1089 
       
  1090    /* resync marker */
       
  1091    tmpvar = bibGetBits(16 + inpParam->fcode_forward, inBuffer, &bitsGot,
       
  1092                    bitErrorIndication, &bibError);
       
  1093    if ( bibError )
       
  1094        goto exitAfterBitError;
       
  1095    if ( (int) tmpvar != MP4_RESYNC_MARKER )
       
  1096    {
       
  1097       deb("ERROR. No Resync Marker found\n");
       
  1098       goto exitAfterBitError;
       
  1099    }
       
  1100 
       
  1101    /* Macroblock Number */
       
  1102    for (MBNumLength = 1; ((inpParam->numOfMBs-1) >> MBNumLength) != 0; MBNumLength++)
       
  1103     {
       
  1104     }
       
  1105 
       
  1106    header->currMBNum = (int) bibGetBits(MBNumLength, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1107    if ( bibError )
       
  1108        goto exitAfterBitError;
       
  1109 
       
  1110    /* quant_scale (5 bits) */
       
  1111    bits = bibGetBits(5, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1112    if ( bibError )
       
  1113        goto exitAfterBitError;
       
  1114 
       
  1115    header->quant = (int) bits;
       
  1116 
       
  1117    /* header_extension_code (1 bit) */
       
  1118    header->fHEC = (u_char) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1119    if ( bibError )
       
  1120        goto exitAfterBitError;
       
  1121 
       
  1122    if(header->fHEC) {
       
  1123       /* modulo_time_base (? bits) */
       
  1124       tmpvar = (int) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1125       if ( bibError )
       
  1126           goto exitAfterBitError;
       
  1127       
       
  1128       while (tmpvar == 1 && !bibError)
       
  1129       {
       
  1130          tmpvar = (int) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1131          time_base_incr++;
       
  1132       }
       
  1133       if ( bibError )
       
  1134           goto exitAfterBitError;
       
  1135 
       
  1136       header->time_base_incr = time_base_incr;
       
  1137 
       
  1138       /* marker_bit (1 bit) */
       
  1139       tmpvar = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1140       if ( bibError )
       
  1141           goto exitAfterBitError;
       
  1142       if ( !tmpvar )
       
  1143          goto exitAfterBitError;
       
  1144 
       
  1145       /* vop_time_increment (1-15 bits) */
       
  1146       for (num_bits = 1; ((inpParam->time_increment_resolution-1) >> num_bits) != 0; num_bits++)
       
  1147         {
       
  1148         }
       
  1149       
       
  1150       tmpvar = (int) bibGetBits(num_bits, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1151       if ( bibError )
       
  1152           goto exitAfterBitError;
       
  1153 
       
  1154       header->time_inc = tmpvar;
       
  1155       
       
  1156       /* marker_bit (1 bit) */
       
  1157       tmpvar = bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1158       if ( bibError )
       
  1159           goto exitAfterBitError;
       
  1160       if (!tmpvar)
       
  1161          goto exitAfterBitError;
       
  1162 
       
  1163       /* vop_prediction_type (2 bits) */
       
  1164       bits = bibGetBits(2, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1165       if ( bibError )
       
  1166           goto exitAfterBitError;
       
  1167 
       
  1168       if ((bits != VDX_VOP_TYPE_P && bits != VDX_VOP_TYPE_I))
       
  1169       {
       
  1170          deb("ERROR. Not supported VOP prediction type\n");
       
  1171          goto exitAfterBitError;
       
  1172       }
       
  1173       
       
  1174       header->coding_type = (u_char) bits;
       
  1175 
       
  1176       /* intra_dc_vlc_thr (3 bits) */
       
  1177       bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1178       if ( bibError )
       
  1179           goto exitAfterBitError;
       
  1180 
       
  1181       header->intra_dc_vlc_thr = (int) bits;
       
  1182 
       
  1183       /* vop_fcode_forward (3 bits) */
       
  1184       if (header->coding_type == VDX_VOP_TYPE_P)
       
  1185       {
       
  1186          bits = bibGetBits(3, inBuffer, &bitsGot, bitErrorIndication, &bibError);
       
  1187          if ( bibError )
       
  1188              goto exitAfterBitError;
       
  1189          header->fcode_forward = (int) bits;
       
  1190       } else
       
  1191          header->fcode_forward = 1;
       
  1192    }
       
  1193 
       
  1194    /* Check success and return */
       
  1195 
       
  1196    /* If no error in bit buffer functions */
       
  1197    if (!bibError)
       
  1198       return VDX_OK;
       
  1199 
       
  1200    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1201    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1202       return VDX_OK_BUT_BIT_ERROR;
       
  1203    }
       
  1204 
       
  1205    /* Else other error in bit buffer functions */
       
  1206    else
       
  1207       return VDX_ERR_BIB;
       
  1208 
       
  1209    exitAfterBitError:
       
  1210       if (bibError && bibError != ERR_BIB_NOT_ENOUGH_DATA)
       
  1211          return VDX_ERR_BIB;
       
  1212 
       
  1213       return VDX_OK_BUT_BIT_ERROR;
       
  1214 }
       
  1215 
       
  1216 /*
       
  1217  * Macroblock Layer Global Functions
       
  1218  */
       
  1219 
       
  1220 /*
       
  1221  *
       
  1222  * vdxGetDataPartitionedIMBLayer_Part1
       
  1223  *
       
  1224  * Parameters:
       
  1225  *    inBuffer     input buffer
       
  1226  *    inpParam     input parameters
       
  1227  *    MBList       a double-linked list for soring 
       
  1228  *                 MB parameters + DC values in the VP
       
  1229  *    bitErrorIndication
       
  1230  *                 non-zero if a bit error has been detected
       
  1231  *                 within the bits accessed in this function,
       
  1232  *                 see biterr.h for possible values
       
  1233  *
       
  1234  * Function:
       
  1235  *    This function gets the first (DC) partition of a data
       
  1236  *    partitioned encoded Video Packet in an Intra-VOP. 
       
  1237  *    The parameters MCBPC, DQUANT and DC values of all the MBs
       
  1238  *    in the VP are read and stored in the linked list.
       
  1239  *
       
  1240  * Returns:
       
  1241  *    VDX_OK                     the function was successful
       
  1242  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1243  *                               occured
       
  1244  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1245  *
       
  1246  * Error codes:
       
  1247  *    error codes returned by bibFlushBits/bibGetBits/bibShowBits
       
  1248  *
       
  1249  *
       
  1250  */
       
  1251 
       
  1252 int vdxGetDataPartitionedIMBLayer_Part1(
       
  1253    bibBuffer_t *inBuffer, bibBuffer_t *outBuffer, bibBufferEdit_t *bufEdit,
       
  1254      int aColorEffect, int *aStartByteIndex, int *aStartBitIndex, 
       
  1255      CMPEG4Transcoder *hTranscoder, 
       
  1256    const vdxGetDataPartitionedIMBLayerInputParam_t *inpParam,
       
  1257    dlst_t *MBList,
       
  1258    int *bitErrorIndication)
       
  1259 {
       
  1260 
       
  1261     int mcbpcIndex,
       
  1262         retValue = VDX_OK,
       
  1263         fDQUANT,
       
  1264         new_quant, previous_quant,
       
  1265         bitsGot,
       
  1266         i, 
       
  1267         IntraDC_size, 
       
  1268         IntraDC_delta;
       
  1269     
       
  1270     /* MVE */
       
  1271    int StartByteIndex;
       
  1272    int StartBitIndex;
       
  1273      
       
  1274    int16 error=0;
       
  1275    vdxIMBListItem_t *MBinstance = NULL;
       
  1276      
       
  1277      
       
  1278    vdxAssert(inpParam != NULL);
       
  1279    vdxAssert(inBuffer != NULL);
       
  1280    vdxAssert(bitErrorIndication != NULL);
       
  1281      
       
  1282    previous_quant = inpParam->quant;
       
  1283 
       
  1284    /* MVE */
       
  1285    int stuffingStartByteIndex, stuffingStartBitIndex, stuffingEndByteIndex, stuffingEndBitIndex;
       
  1286    stuffingStartByteIndex = stuffingEndByteIndex = inBuffer->getIndex;
       
  1287    stuffingStartBitIndex  = stuffingEndBitIndex = inBuffer->bitIndex;
       
  1288 
       
  1289    while (bibShowBits(MP4_DC_MARKER_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &error) != MP4_DC_MARKER && !error)
       
  1290    {
       
  1291          /* MVE */
       
  1292          StartByteIndex = inBuffer->getIndex; 
       
  1293          StartBitIndex = inBuffer->bitIndex;
       
  1294          
       
  1295          retValue = vdxGetMCBPCIntra(inBuffer, &mcbpcIndex, bitErrorIndication);
       
  1296          if (retValue != VDX_OK)
       
  1297              goto exitFunction;
       
  1298          
       
  1299      if (mcbpcIndex == 8)
       
  1300          {
       
  1301              /* MVE */
       
  1302              stuffingEndByteIndex = inBuffer->getIndex;
       
  1303              stuffingEndBitIndex = inBuffer->bitIndex;
       
  1304              
       
  1305              continue; /* skip stuffing */
       
  1306          }
       
  1307          
       
  1308          /* Create new MBInstance for the next MB */
       
  1309          MBinstance = (vdxIMBListItem_t *) malloc(sizeof(vdxIMBListItem_t));
       
  1310          if (!MBinstance)
       
  1311          {
       
  1312              deb("ERROR - MBinstance creation failed\n");
       
  1313              retValue = H263D_ERROR;
       
  1314              goto exitFunction;
       
  1315          }
       
  1316          memset(MBinstance, 0, sizeof(vdxIMBListItem_t));
       
  1317          
       
  1318          /* CBPC (2 LSBs of MCBPC) */
       
  1319          MBinstance->cbpc = mcbpcIndex & 3;
       
  1320          
       
  1321          /* MVE */
       
  1322          MBinstance->mcbpc = mcbpcIndex;
       
  1323          VDT_SET_START_POSITION(MBinstance,11,stuffingStartByteIndex,stuffingStartBitIndex); // 11: MB stuffing bits
       
  1324          VDT_SET_END_POSITION(MBinstance,11,stuffingEndByteIndex,stuffingEndBitIndex); // 11: MB stuffing bits
       
  1325          
       
  1326          VDT_SET_START_POSITION(MBinstance,0,StartByteIndex,StartBitIndex); // 0: mcbpc
       
  1327          VDT_SET_END_POSITION(MBinstance,0,inBuffer->getIndex,inBuffer->bitIndex); // 0: mcbpc
       
  1328          VDT_SET_START_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  1329          
       
  1330          /* DQUANT is given for MCBPC indexes 4..7 */
       
  1331          fDQUANT = mcbpcIndex & 4;
       
  1332          
       
  1333          if (fDQUANT) {
       
  1334              retValue = vdxUpdateQuant(inBuffer, 0, previous_quant,
       
  1335                  &new_quant, bitErrorIndication);
       
  1336              if (retValue != VDX_OK)
       
  1337                  goto exitFunction;
       
  1338          }
       
  1339          /* Else no DQUANT */
       
  1340          else
       
  1341              new_quant = previous_quant;
       
  1342          
       
  1343          MBinstance->dquant       = fDQUANT ?  new_quant - previous_quant : 0;
       
  1344          MBinstance->quant = previous_quant = new_quant;
       
  1345          
       
  1346          /* MVE */
       
  1347          VDT_SET_END_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  1348      VDT_SET_START_POSITION(MBinstance,4,inBuffer->getIndex,inBuffer->bitIndex); // 4: intraDC
       
  1349      VDT_SET_START_POSITION(MBinstance,5,inBuffer->getIndex,inBuffer->bitIndex); // 5: intraDC
       
  1350      VDT_SET_START_POSITION(MBinstance,6,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC
       
  1351      VDT_SET_START_POSITION(MBinstance,7,inBuffer->getIndex,inBuffer->bitIndex); // 7: intraDC
       
  1352      VDT_SET_START_POSITION(MBinstance,8,inBuffer->getIndex,inBuffer->bitIndex); // 8: intraDC
       
  1353      VDT_SET_START_POSITION(MBinstance,9,inBuffer->getIndex,inBuffer->bitIndex); // 9: intraDC
       
  1354          
       
  1355      VDT_SET_END_POSITION(MBinstance,4,inBuffer->getIndex,inBuffer->bitIndex); // 4: intraDC
       
  1356      VDT_SET_END_POSITION(MBinstance,5,inBuffer->getIndex,inBuffer->bitIndex); // 5: intraDC
       
  1357      VDT_SET_END_POSITION(MBinstance,6,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC
       
  1358      VDT_SET_END_POSITION(MBinstance,7,inBuffer->getIndex,inBuffer->bitIndex); // 7: intraDC
       
  1359      VDT_SET_END_POSITION(MBinstance,8,inBuffer->getIndex,inBuffer->bitIndex); // 8: intraDC
       
  1360      VDT_SET_END_POSITION(MBinstance,9,inBuffer->getIndex,inBuffer->bitIndex); // 9: intraDC
       
  1361 
       
  1362          /* Color Toning */
       
  1363      hTranscoder->AfterMBLayer(new_quant);
       
  1364  
       
  1365          
       
  1366          MBinstance->switched = aicIntraDCSwitch(inpParam->intra_dc_vlc_thr,new_quant);
       
  1367          
       
  1368          /* Intra_DC_Coeffs */
       
  1369          if(!MBinstance->switched) {
       
  1370              for (i=0; i<6; i++) {
       
  1371                  
       
  1372                  /* MVE */
       
  1373                  VDT_SET_START_POSITION(MBinstance,i+4,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC,chrominance
       
  1374                  
       
  1375                  retValue = vdxGetIntraDC(inBuffer, outBuffer, bufEdit, aColorEffect, aStartByteIndex, aStartBitIndex, 
       
  1376                      i, &IntraDC_size, &IntraDC_delta, bitErrorIndication);
       
  1377                  
       
  1378                  /* MVE */
       
  1379                  VDT_SET_END_POSITION(MBinstance,i+4,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC,chrominance
       
  1380                  
       
  1381                  if (retValue != VDX_OK)
       
  1382                      goto exitFunction;
       
  1383                  
       
  1384                  MBinstance->DC[i] = IntraDC_delta;
       
  1385              } 
       
  1386          } 
       
  1387          
       
  1388          
       
  1389          /* Put MBinstance into the queue */
       
  1390          dlstAddAfterCurr(MBList, MBinstance);
       
  1391          MBinstance = NULL;
       
  1392          
       
  1393      /* MVE */
       
  1394          // begin another MB, record the position of MB stuffing
       
  1395          stuffingStartByteIndex = stuffingEndByteIndex = inBuffer->getIndex;
       
  1396          stuffingStartBitIndex  = stuffingEndBitIndex = inBuffer->bitIndex;
       
  1397          
       
  1398    }
       
  1399      
       
  1400 exitFunction:
       
  1401      
       
  1402    if (MBinstance)
       
  1403       free(MBinstance);
       
  1404 
       
  1405 
       
  1406    /* If no error in bit buffer functions */
       
  1407    if (!error)
       
  1408       return retValue;
       
  1409 
       
  1410    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1411    else if (error == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1412       return VDX_OK_BUT_BIT_ERROR;
       
  1413    }
       
  1414 
       
  1415    /* Else other error in bit buffer functions */
       
  1416    else
       
  1417       return VDX_ERR_BIB;
       
  1418 }
       
  1419 
       
  1420 /*
       
  1421  *
       
  1422  * vdxGetDataPartitionedIMBLayer_Part2
       
  1423  *
       
  1424  * Parameters:
       
  1425  *    inBuffer     input buffer
       
  1426  *    MBList       a double-linked list for soring 
       
  1427  *                 MB parameters + DC values in the VP
       
  1428  *    numMBsInVP   number of MBs in this VP
       
  1429  *    bitErrorIndication
       
  1430  *                 non-zero if a bit error has been detected
       
  1431  *                 within the bits accessed in this function,
       
  1432  *                 see biterr.h for possible values
       
  1433  *
       
  1434  * Function:
       
  1435  *    This function gets the second (CBPY) partition of a data
       
  1436  *    partitioned encoded Video Packet in an Intra-VOP. 
       
  1437  *    The parameters CBPY and ac_pred_flag of all the MBs
       
  1438  *    in the VP are read and stored in the linked list.
       
  1439  *
       
  1440  * Returns:
       
  1441  *    VDX_OK                     the function was successful
       
  1442  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1443  *                               occured
       
  1444  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1445  *
       
  1446  * Error codes:
       
  1447  *      error codes returned by bibFlushBits/bibGetBits/bibShowBits
       
  1448  *
       
  1449  *      
       
  1450  *
       
  1451  */
       
  1452 
       
  1453 int vdxGetDataPartitionedIMBLayer_Part2(
       
  1454    bibBuffer_t *inBuffer, bibBuffer_t */*outBuffer*/, bibBufferEdit_t */*bufEdit*/, 
       
  1455    int /*aColorEffect*/, int */*aStartByteIndex*/, int */*aStartBitIndex*/, 
       
  1456    dlst_t *MBList,
       
  1457    int numMBsInVP,
       
  1458    int *bitErrorIndication)
       
  1459 {
       
  1460 
       
  1461    int cbpyIndex,
       
  1462       i,
       
  1463       retValue = VDX_OK,
       
  1464       bitsGot;
       
  1465 
       
  1466    int16 error=0;
       
  1467    u_char code;
       
  1468    vdxIMBListItem_t *MBinstance;
       
  1469 
       
  1470    vdxAssert(inBuffer != NULL);
       
  1471    vdxAssert(bitErrorIndication != NULL);
       
  1472 
       
  1473    /* get the first MB of the list */
       
  1474    dlstHead(MBList, (void **) &MBinstance);
       
  1475 
       
  1476    /* Get ac_pred_flag and cbpy of all MBs in VP */
       
  1477    for (i = 0; i < numMBsInVP; i++)
       
  1478    {    
       
  1479          if (!MBinstance)
       
  1480          {
       
  1481              retValue = H263D_ERROR;
       
  1482              goto exitFunction;
       
  1483          }
       
  1484 
       
  1485          /* MVE */
       
  1486          VDT_SET_START_POSITION(MBinstance,3,inBuffer->getIndex,inBuffer->bitIndex); // 3: ac_pred_flag
       
  1487          
       
  1488          /* ac_pred_flag (1 bit) */
       
  1489          code = (u_char) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &error);
       
  1490          if (error) 
       
  1491              goto exitFunction;
       
  1492          
       
  1493          /* MVE */
       
  1494          VDT_SET_END_POSITION(MBinstance,3,inBuffer->getIndex,inBuffer->bitIndex); // 3: ac_pred_flag
       
  1495          VDT_SET_START_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); //  2: cbpy
       
  1496          
       
  1497          /* CBPY */
       
  1498          retValue = vdxGetCBPY(inBuffer, &cbpyIndex, bitErrorIndication);
       
  1499          if (retValue != VDX_OK)
       
  1500              goto exitFunction;
       
  1501          
       
  1502          /* MVE */
       
  1503          VDT_SET_END_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); // 2: cbpy
       
  1504          
       
  1505                                                                                                                                                              /* add the information to the MB data item.
       
  1506                                                                                                                                                              If the Part1 of the VP was decoded without errors (all MBs were
       
  1507          attached to the list), MBinstance should always have a valid value. */ 
       
  1508          if (MBinstance != NULL) {
       
  1509              MBinstance->ac_pred_flag = code;
       
  1510              MBinstance->cbpy = cbpyIndex;
       
  1511              
       
  1512              dlstNext(MBList, (void **) &MBinstance);
       
  1513          }
       
  1514    }
       
  1515      
       
  1516 exitFunction:
       
  1517      
       
  1518      
       
  1519    /* If no error in bit buffer functions */
       
  1520    if (!error)
       
  1521       return retValue;
       
  1522 
       
  1523    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1524    else if (error == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1525       return VDX_OK_BUT_BIT_ERROR;
       
  1526    }
       
  1527 
       
  1528    /* Else other error in bit buffer functions */
       
  1529    else
       
  1530       return VDX_ERR_BIB;
       
  1531 }
       
  1532 
       
  1533 /*
       
  1534  *
       
  1535  * vdxGetDataPartitionedPMBLayer_Part1
       
  1536  *
       
  1537  * Parameters:
       
  1538  *    inBuffer     input buffer
       
  1539  *    inpParam     input parameters
       
  1540  *    MBList       a double-linked list for soring 
       
  1541  *                 MB parameters + DC values in the VP
       
  1542  *    bitErrorIndication
       
  1543  *                 non-zero if a bit error has been detected
       
  1544  *                 within the bits accessed in this function,
       
  1545  *                 see biterr.h for possible values
       
  1546  *
       
  1547  * Function:
       
  1548  *    This function gets the first (Motion) partition of a data
       
  1549  *    partitioned encoded Video Packet in an Inter-VOP. 
       
  1550  *    The parameters COD, MCBPC and motion vectors of all the MBs
       
  1551  *    in the VP are read and stored in the linked list.
       
  1552  *
       
  1553  * Returns:
       
  1554  *    VDX_OK                     the function was successful
       
  1555  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1556  *                               occured
       
  1557  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1558  *
       
  1559  * Error codes:
       
  1560  *      error codes returned by bibFlushBits/bibGetBits/bibShowBits
       
  1561  *
       
  1562  *      
       
  1563  *
       
  1564  */
       
  1565 
       
  1566  int vdxGetDataPartitionedPMBLayer_Part1(
       
  1567    bibBuffer_t *inBuffer,
       
  1568    bibBuffer_t */*outBuffer*/, bibBufferEdit_t */*bufEdit*/, 
       
  1569    int /*aColorEffect*/, int */*aStartByteIndex*/, int */*aStartBitIndex*/, 
       
  1570    const vdxGetDataPartitionedPMBLayerInputParam_t *inpParam,
       
  1571    dlst_t *MBList,
       
  1572    int *bitErrorIndication)
       
  1573  {
       
  1574      
       
  1575    static const int mbTypeToMBClass[6] = 
       
  1576      {VDX_MB_INTER, VDX_MB_INTER, VDX_MB_INTER, 
       
  1577      VDX_MB_INTRA, VDX_MB_INTRA, VDX_MB_INTER};
       
  1578      
       
  1579    int mvdx, mvdy,
       
  1580          numMVs,
       
  1581          mcbpcIndex,
       
  1582          retValue = VDX_OK,
       
  1583          bitsGot;
       
  1584      
       
  1585    int16 error=0;
       
  1586    u_char code;
       
  1587    vdxPMBListItem_t *MBinstance = NULL;
       
  1588      
       
  1589      /* MVE */
       
  1590    int StartByteIndex = 0;
       
  1591    int StartBitIndex = 0;
       
  1592    int stuffingStartByteIndex, stuffingStartBitIndex, stuffingEndByteIndex, stuffingEndBitIndex;
       
  1593    
       
  1594    stuffingStartByteIndex = stuffingEndByteIndex = inBuffer->getIndex;
       
  1595    stuffingStartBitIndex  = stuffingEndBitIndex = inBuffer->bitIndex;
       
  1596      
       
  1597    vdxAssert(inpParam != NULL);
       
  1598    vdxAssert(inBuffer != NULL);
       
  1599    vdxAssert(bitErrorIndication != NULL);
       
  1600      
       
  1601    while (bibShowBits(MP4_MOTION_MARKER_COMB_LENGTH, inBuffer, &bitsGot, bitErrorIndication, &error) 
       
  1602          != MP4_MOTION_MARKER_COMB && !error)
       
  1603    {
       
  1604          /* COD */
       
  1605          code = (u_char) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &error);
       
  1606          if (error) 
       
  1607              goto exitFunction;
       
  1608          
       
  1609          if (code == 0)
       
  1610          {
       
  1611              
       
  1612              /* MVE */
       
  1613              StartByteIndex = inBuffer->getIndex; 
       
  1614              StartBitIndex = inBuffer->bitIndex;
       
  1615              
       
  1616              /* MCBPC */
       
  1617              retValue = vdxGetMCBPCInter(
       
  1618                  inBuffer, 
       
  1619                  0, /* No PLUSPTYPE present in MPEG-4 */
       
  1620                  1, /* Four motion vectors always possible in MPEG-4 */
       
  1621             0, /* Flag to indicate if this is the first MB of a picture.
       
  1622                              Since this value is used only for checking if
       
  1623                              indices 21 - 24 are allowed in H.263, we don't have
       
  1624                              to set this correctly. (Indices 21 - 24 are not allowed
       
  1625                              in MPEG-4 at all.) */
       
  1626                              &mcbpcIndex, 
       
  1627                              bitErrorIndication);
       
  1628              
       
  1629              if (retValue != VDX_OK)
       
  1630                  goto exitFunction;
       
  1631              
       
  1632              if (mcbpcIndex == 20)
       
  1633              {
       
  1634                  /* MVE */
       
  1635                  stuffingEndByteIndex = inBuffer->getIndex;
       
  1636                  stuffingEndBitIndex = inBuffer->bitIndex;
       
  1637                  
       
  1638                  continue;  /* skip stuffing */
       
  1639              }
       
  1640              
       
  1641              /* Indices > 20 not allowed */
       
  1642              if (mcbpcIndex > 20) {
       
  1643                  deb0p("vdxGetDataPartitionedPMBLayer_Part1: ERROR - Illegal code.\n");
       
  1644                  retValue = VDX_OK_BUT_BIT_ERROR;              
       
  1645                  goto exitFunction;
       
  1646              }
       
  1647              
       
  1648              
       
  1649          }
       
  1650          
       
  1651          /* Create new MBInstance for the MB */
       
  1652          MBinstance = (vdxPMBListItem_t *) malloc(sizeof(vdxPMBListItem_t));
       
  1653          if (!MBinstance)
       
  1654          {
       
  1655              deb("ERROR - MBinstance creation failed\n");
       
  1656              goto exitFunction;
       
  1657          }
       
  1658          memset(MBinstance, 0, sizeof(vdxPMBListItem_t));
       
  1659          
       
  1660          MBinstance->fCodedMB = (u_char) (code ^ 1);      
       
  1661          
       
  1662          if (MBinstance->fCodedMB) 
       
  1663          {                   
       
  1664              /* MCBPC */
       
  1665              MBinstance->cbpc = mcbpcIndex;
       
  1666              
       
  1667              /* MCBPC --> MB type & included data elements */
       
  1668              MBinstance->mbType = mcbpcIndex / 4;
       
  1669              MBinstance->mbClass = mbTypeToMBClass[MBinstance->mbType];
       
  1670              
       
  1671              /* MVE */
       
  1672              MBinstance->mcbpc = mcbpcIndex;
       
  1673              VDT_SET_START_POSITION(MBinstance,11,stuffingStartByteIndex,stuffingStartBitIndex); // 11: MB stuffing bits
       
  1674              VDT_SET_END_POSITION(MBinstance,11,stuffingEndByteIndex,stuffingEndBitIndex); // 11: MB stuffing bits
       
  1675              VDT_SET_START_POSITION(MBinstance,0,StartByteIndex,StartBitIndex);  // MCBPC
       
  1676              VDT_SET_END_POSITION(MBinstance,0,inBuffer->getIndex,inBuffer->bitIndex);
       
  1677              
       
  1678              VDT_SET_START_POSITION(MBinstance,10,inBuffer->getIndex,inBuffer->bitIndex); // MVs
       
  1679              
       
  1680              /* MVD is included always for PB-frames and always if MB type is INTER */
       
  1681              numMVs = MBinstance->numMVs = 
       
  1682                  (MBinstance->mbClass == VDX_MB_INTER) ?
       
  1683                  ((MBinstance->mbType == 2 || MBinstance->mbType == 5) ? 4 : 1) : 0;
       
  1684              
       
  1685              if (numMVs) {
       
  1686                  int i;
       
  1687                  for (i = 0; i < numMVs; i++) {
       
  1688                      retValue = vdxGetScaledMVD(inBuffer,inpParam->f_code,&mvdx,bitErrorIndication);
       
  1689                      if (retValue != VDX_OK)
       
  1690                          goto exitFunction;
       
  1691                      retValue = vdxGetScaledMVD(inBuffer,inpParam->f_code,&mvdy,bitErrorIndication);
       
  1692                      if (retValue != VDX_OK)
       
  1693                          goto exitFunction;
       
  1694                      
       
  1695                      MBinstance->mvx[i] = mvdx;
       
  1696                      MBinstance->mvy[i] = mvdy;
       
  1697                  }
       
  1698              }
       
  1699              
       
  1700              /* MVE */
       
  1701              VDT_SET_END_POSITION(MBinstance,10,inBuffer->getIndex,inBuffer->bitIndex); // MVs
       
  1702              
       
  1703          }
       
  1704          
       
  1705        /* MVE */
       
  1706          // begin another MB, record the position of MB stuffing
       
  1707          stuffingStartByteIndex = stuffingEndByteIndex = inBuffer->getIndex;
       
  1708          stuffingStartBitIndex  = stuffingEndBitIndex = inBuffer->bitIndex;
       
  1709          
       
  1710          /* Put MBinstance into the queue */
       
  1711          dlstAddAfterCurr(MBList, MBinstance);
       
  1712          MBinstance = NULL;
       
  1713    }
       
  1714      
       
  1715 exitFunction:
       
  1716      
       
  1717    if (MBinstance)
       
  1718          free(MBinstance);
       
  1719    
       
  1720      
       
  1721    /* If no error in bit buffer functions */
       
  1722    if (!error)
       
  1723          return retValue;
       
  1724      
       
  1725    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1726    else if (error == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1727          return VDX_OK_BUT_BIT_ERROR;
       
  1728    }
       
  1729      
       
  1730    /* Else other error in bit buffer functions */
       
  1731    else
       
  1732          return VDX_ERR_BIB;
       
  1733      
       
  1734 }
       
  1735 
       
  1736 
       
  1737 /*
       
  1738  *
       
  1739  * vdxGetDataPartitionedPMBLayer_Part2
       
  1740  *
       
  1741  * Parameters:
       
  1742  *    inBuffer     input buffer
       
  1743  *    inpParam     input parameters
       
  1744  *    MBList       a double-linked list for soring 
       
  1745  *                 MB parameters + DC values in the VP
       
  1746  *    bitErrorIndication
       
  1747  *                 non-zero if a bit error has been detected
       
  1748  *                 within the bits accessed in this function,
       
  1749  *                 see biterr.h for possible values
       
  1750  *
       
  1751  * Function:
       
  1752  *    This function gets the second (CBPY) partition of a data
       
  1753  *    partitioned encoded Video Packet in an Inter-VOP. 
       
  1754  *    The parameters CBPY, DQUANT and if Intra MB its DC coeffs
       
  1755  *    of all the MBs in the VP are read and stored in the linked list.
       
  1756  *
       
  1757  * Returns:
       
  1758  *    VDX_OK                     the function was successful
       
  1759  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1760  *                               occured
       
  1761  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1762  *
       
  1763  * Error codes:
       
  1764  *      error codes returned by bibFlushBits/bibGetBits/bibShowBits
       
  1765  *
       
  1766  *      
       
  1767  *
       
  1768  */
       
  1769 
       
  1770  int vdxGetDataPartitionedPMBLayer_Part2(
       
  1771    bibBuffer_t *inBuffer,
       
  1772    bibBuffer_t *outBuffer, 
       
  1773    bibBufferEdit_t *bufEdit,
       
  1774    int aColorEffect, int *aStartByteIndex, int *aStartBitIndex, 
       
  1775    CMPEG4Transcoder *hTranscoder, 
       
  1776    const vdxGetDataPartitionedPMBLayerInputParam_t *inpParam,
       
  1777    dlst_t *MBList,
       
  1778    int *bitErrorIndication)
       
  1779  {
       
  1780      
       
  1781    static const int mbTypeToDQUANTI[6] =
       
  1782      {0, 1, 0, 0, 1, 1};
       
  1783      
       
  1784    int fDQUANT,
       
  1785          cbpyIndex,
       
  1786          retValue = VDX_OK,
       
  1787          new_quant, previous_quant,
       
  1788          bitsGot,
       
  1789          i, 
       
  1790          IntraDC_size, 
       
  1791          IntraDC_delta;
       
  1792      
       
  1793    int16 error=0;
       
  1794    u_char code;
       
  1795    vdxPMBListItem_t *MBinstance;
       
  1796      
       
  1797    vdxAssert(inpParam != NULL);
       
  1798    vdxAssert(inBuffer != NULL);
       
  1799    vdxAssert(bitErrorIndication != NULL);
       
  1800      
       
  1801    previous_quant = inpParam->quant;
       
  1802      
       
  1803    /* Get ac_pred_flag and cbpy of all MBs in VP */
       
  1804    for (dlstHead(MBList, (void **) &MBinstance); 
       
  1805      MBinstance != NULL; 
       
  1806      dlstNext(MBList, (void **) &MBinstance))
       
  1807    {
       
  1808          
       
  1809          if (!MBinstance->fCodedMB) continue;
       
  1810          
       
  1811          /* MVE */
       
  1812          VDT_SET_START_POSITION(MBinstance,3,inBuffer->getIndex,inBuffer->bitIndex); // 3 ac_pred_flag
       
  1813          
       
  1814          if (MBinstance->mbClass == VDX_MB_INTRA) {
       
  1815              
       
  1816              /* ac_pred_flag (1 bit) */
       
  1817              code = (u_char) bibGetBits(1, inBuffer, &bitsGot, bitErrorIndication, &error);
       
  1818              if (error) 
       
  1819                  goto exitFunction;
       
  1820              
       
  1821              MBinstance->ac_pred_flag = code;
       
  1822          }
       
  1823          
       
  1824          /* MVE */
       
  1825          VDT_SET_END_POSITION(MBinstance,3,inBuffer->getIndex,inBuffer->bitIndex); // 3 ac_pred_flag
       
  1826          VDT_SET_START_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); // 2: cbpy
       
  1827          
       
  1828          /* CBPY */
       
  1829          retValue = vdxGetCBPY(inBuffer, &cbpyIndex, bitErrorIndication);
       
  1830          if (retValue != VDX_OK)
       
  1831              goto exitFunction;
       
  1832          
       
  1833          if (MBinstance->mbClass == VDX_MB_INTER)
       
  1834              /* Convert index to INTER CBPY */
       
  1835              cbpyIndex = 15 - cbpyIndex;
       
  1836          
       
  1837          MBinstance->cbpy = cbpyIndex;
       
  1838          
       
  1839          /* MVE */
       
  1840          VDT_SET_END_POSITION(MBinstance,2,inBuffer->getIndex,inBuffer->bitIndex); // 
       
  1841          VDT_SET_START_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  1842          
       
  1843          /* DQUANT is given for MCBPC indexes 4..7 */
       
  1844          fDQUANT = mbTypeToDQUANTI[MBinstance->mbType];
       
  1845          
       
  1846          if (fDQUANT) {
       
  1847              retValue = vdxUpdateQuant(inBuffer, 0, previous_quant,
       
  1848                  &new_quant, bitErrorIndication);
       
  1849              if (retValue != VDX_OK)
       
  1850                  goto exitFunction;
       
  1851          }
       
  1852          /* Else no DQUANT */
       
  1853          else
       
  1854              new_quant = previous_quant;
       
  1855          
       
  1856          MBinstance->dquant       = fDQUANT ?  new_quant - previous_quant : 0;
       
  1857          MBinstance->quant = previous_quant = new_quant;
       
  1858          
       
  1859          /* MVE */
       
  1860          VDT_SET_END_POSITION(MBinstance,1,inBuffer->getIndex,inBuffer->bitIndex); // 1: dquant
       
  1861          VDT_SET_START_POSITION(MBinstance,4,inBuffer->getIndex,inBuffer->bitIndex); // 4: intraDC
       
  1862          VDT_SET_START_POSITION(MBinstance,5,inBuffer->getIndex,inBuffer->bitIndex); // 5: intraDC
       
  1863          VDT_SET_START_POSITION(MBinstance,6,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC
       
  1864          VDT_SET_START_POSITION(MBinstance,7,inBuffer->getIndex,inBuffer->bitIndex); // 7: intraDC
       
  1865          VDT_SET_START_POSITION(MBinstance,8,inBuffer->getIndex,inBuffer->bitIndex); // 8: intraDC
       
  1866          VDT_SET_START_POSITION(MBinstance,9,inBuffer->getIndex,inBuffer->bitIndex); // 9: intraDC
       
  1867          
       
  1868          VDT_SET_END_POSITION(MBinstance,4,inBuffer->getIndex,inBuffer->bitIndex); // 4: intraDC
       
  1869          VDT_SET_END_POSITION(MBinstance,5,inBuffer->getIndex,inBuffer->bitIndex); // 5: intraDC
       
  1870          VDT_SET_END_POSITION(MBinstance,6,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC
       
  1871          VDT_SET_END_POSITION(MBinstance,7,inBuffer->getIndex,inBuffer->bitIndex); // 7: intraDC
       
  1872          VDT_SET_END_POSITION(MBinstance,8,inBuffer->getIndex,inBuffer->bitIndex); // 8: intraDC
       
  1873          VDT_SET_END_POSITION(MBinstance,9,inBuffer->getIndex,inBuffer->bitIndex); // 9: intraDC
       
  1874          
       
  1875      /* Color Toning */
       
  1876      hTranscoder->AfterMBLayer(new_quant);
       
  1877      
       
  1878          if (MBinstance->mbClass == VDX_MB_INTRA) {
       
  1879              MBinstance->switched = aicIntraDCSwitch(inpParam->intra_dc_vlc_thr,new_quant);
       
  1880              
       
  1881              /* Intra_DC_Coeffs */
       
  1882              if(!MBinstance->switched) {
       
  1883                  for (i=0; i<6; i++) {
       
  1884                      
       
  1885                      /* MVE */
       
  1886                      VDT_SET_START_POSITION(MBinstance,i+4,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC,chrominance
       
  1887                      
       
  1888                      retValue = vdxGetIntraDC(inBuffer, outBuffer, bufEdit, aColorEffect, aStartByteIndex, aStartBitIndex, 
       
  1889                          i, &IntraDC_size, &IntraDC_delta, bitErrorIndication);
       
  1890                      
       
  1891                      /* MVE */
       
  1892                      VDT_SET_END_POSITION(MBinstance,i+4,inBuffer->getIndex,inBuffer->bitIndex); // 6: intraDC,chrominance
       
  1893                      
       
  1894                      if (retValue != VDX_OK)
       
  1895                          goto exitFunction;
       
  1896                      MBinstance->DC[i] = IntraDC_delta;
       
  1897                  } 
       
  1898              }
       
  1899          }
       
  1900    }
       
  1901      
       
  1902 exitFunction:
       
  1903    
       
  1904      
       
  1905    /* If no error in bit buffer functions */
       
  1906    if (!error)
       
  1907          return retValue;
       
  1908      
       
  1909    /* Else if ran out of data (i.e. decoding out of sync) */
       
  1910    else if (error == ERR_BIB_NOT_ENOUGH_DATA) {
       
  1911          return VDX_OK_BUT_BIT_ERROR;
       
  1912    }
       
  1913      
       
  1914    /* Else other error in bit buffer functions */
       
  1915    else
       
  1916          return VDX_ERR_BIB;
       
  1917      
       
  1918 }
       
  1919 
       
  1920 /*
       
  1921  * Block Layer Global Functions
       
  1922  */
       
  1923 
       
  1924 /* {{-output"vdxGetMPEGIntraDCTBlock.txt"}} */
       
  1925 /*
       
  1926  * vdxGetMPEGIntraDCTBlock
       
  1927  *    
       
  1928  *
       
  1929  * Parameters:
       
  1930  *    inBuffer                   pointer to bit buffer instance
       
  1931  *    startIndex                 the first index in block where to put data
       
  1932  *    block                      DCT coefficients of the block 
       
  1933  *                               in zigzag order 
       
  1934  *    bitErrorIndication         non-zero if a bit error has been detected
       
  1935  *                               within the bits accessed in this function,
       
  1936  *                               see biterr.h for possible values
       
  1937  *
       
  1938  * Function:
       
  1939  *    This function gets the DCT coefficients for one INTRA block.
       
  1940  *
       
  1941  * Returns:
       
  1942  *    VDX_OK                     the function was successful
       
  1943  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  1944  *                               occured
       
  1945  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  1946  *
       
  1947  *    
       
  1948  *    
       
  1949  */
       
  1950 
       
  1951 int vdxGetMPEGIntraDCTBlock(
       
  1952    bibBuffer_t *inBuffer, 
       
  1953    int startIndex,
       
  1954    int *block,
       
  1955    int *bitErrorIndication)
       
  1956 /* {{-output"vdxGetMPEGIntraDCTBlock.txt"}} */
       
  1957 {
       
  1958    int
       
  1959       numBitsGot,
       
  1960       retValue = VDX_OK,
       
  1961       tmpvar;
       
  1962    int16
       
  1963       bibError = 0;
       
  1964 
       
  1965    static const vdxVLCTable_t Intra_tcoefTab0[] = {
       
  1966     {0x10401, 7}, {0x10301, 7}, {0x00601, 7}, {0x10501, 7},
       
  1967     {0x00701, 7}, {0x00202, 7}, {0x00103, 7}, {0x00009, 7},
       
  1968     {0x10002, 6}, {0x10002, 6}, {0x00501, 6}, {0x00501, 6}, 
       
  1969     {0x10201, 6}, {0x10201, 6}, {0x10101, 6}, {0x10101, 6},
       
  1970     {0x00401, 6}, {0x00401, 6}, {0x00301, 6}, {0x00301, 6},
       
  1971     {0x00008, 6}, {0x00008, 6}, {0x00007, 6}, {0x00007, 6}, 
       
  1972     {0x00102, 6}, {0x00102, 6}, {0x00006, 6}, {0x00006, 6},
       
  1973     {0x00201, 5}, {0x00201, 5}, {0x00201, 5}, {0x00201, 5},
       
  1974     {0x00005, 5}, {0x00005, 5}, {0x00005, 5}, {0x00005, 5}, 
       
  1975     {0x00004, 5}, {0x00004, 5}, {0x00004, 5}, {0x00004, 5}, 
       
  1976     {0x10001, 4}, {0x10001, 4}, {0x10001, 4}, {0x10001, 4},
       
  1977     {0x10001, 4}, {0x10001, 4}, {0x10001, 4}, {0x10001, 4},
       
  1978     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2},
       
  1979     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2},
       
  1980     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, 
       
  1981     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2},
       
  1982     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2},
       
  1983     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, 
       
  1984     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2},
       
  1985     {0x00001, 2}, {0x00001, 2}, {0x00001, 2}, {0x00001, 2},
       
  1986     {0x00002, 3}, {0x00002, 3}, {0x00002, 3}, {0x00002, 3}, 
       
  1987     {0x00002, 3}, {0x00002, 3}, {0x00002, 3}, {0x00002, 3},
       
  1988     {0x00002, 3}, {0x00002, 3}, {0x00002, 3}, {0x00002, 3},
       
  1989     {0x00002, 3}, {0x00002, 3}, {0x00002, 3}, {0x00002, 3}, 
       
  1990     {0x00101, 4}, {0x00101, 4}, {0x00101, 4}, {0x00101, 4},
       
  1991     {0x00101, 4}, {0x00101, 4}, {0x00101, 4}, {0x00101, 4},
       
  1992     {0x00003, 4}, {0x00003, 4}, {0x00003, 4}, {0x00003, 4},
       
  1993     {0x00003, 4}, {0x00003, 4}, {0x00003, 4}, {0x00003, 4},
       
  1994    };
       
  1995 
       
  1996 
       
  1997    static const vdxVLCTable_t Intra_tcoefTab1[] = {
       
  1998     {0x00012,10}, {0x00011,10}, {0x10e01, 9}, {0x10e01, 9},
       
  1999     {0x10d01, 9}, {0x10d01, 9}, {0x10c01, 9}, {0x10c01, 9},
       
  2000     {0x10b01, 9}, {0x10b01, 9}, {0x10a01, 9}, {0x10a01, 9}, 
       
  2001     {0x10102, 9}, {0x10102, 9}, {0x10004, 9}, {0x10004, 9},
       
  2002     {0x00c01, 9}, {0x00c01, 9}, {0x00b01, 9}, {0x00b01, 9},
       
  2003     {0x00702, 9}, {0x00702, 9}, {0x00602, 9}, {0x00602, 9}, 
       
  2004     {0x00502, 9}, {0x00502, 9}, {0x00303, 9}, {0x00303, 9},
       
  2005     {0x00203, 9}, {0x00203, 9}, {0x00106, 9}, {0x00106, 9},
       
  2006     {0x00105, 9}, {0x00105, 9}, {0x00010, 9}, {0x00010, 9}, 
       
  2007     {0x00402, 9}, {0x00402, 9}, {0x0000f, 9}, {0x0000f, 9},
       
  2008     {0x0000e, 9}, {0x0000e, 9}, {0x0000d, 9}, {0x0000d, 9},
       
  2009     {0x10801, 8}, {0x10801, 8}, {0x10801, 8}, {0x10801, 8}, 
       
  2010     {0x10701, 8}, {0x10701, 8}, {0x10701, 8}, {0x10701, 8},
       
  2011     {0x10601, 8}, {0x10601, 8}, {0x10601, 8}, {0x10601, 8},
       
  2012     {0x10003, 8}, {0x10003, 8}, {0x10003, 8}, {0x10003, 8},  
       
  2013     {0x00a01, 8}, {0x00a01, 8}, {0x00a01, 8}, {0x00a01, 8},
       
  2014     {0x00901, 8}, {0x00901, 8}, {0x00901, 8}, {0x00901, 8},
       
  2015     {0x00801, 8}, {0x00801, 8}, {0x00801, 8}, {0x00801, 8},  
       
  2016     {0x10901, 8}, {0x10901, 8}, {0x10901, 8}, {0x10901, 8},
       
  2017     {0x00302, 8}, {0x00302, 8}, {0x00302, 8}, {0x00302, 8},
       
  2018     {0x00104, 8}, {0x00104, 8}, {0x00104, 8}, {0x00104, 8},  
       
  2019     {0x0000c, 8}, {0x0000c, 8}, {0x0000c, 8}, {0x0000c, 8},
       
  2020     {0x0000b, 8}, {0x0000b, 8}, {0x0000b, 8}, {0x0000b, 8},
       
  2021     {0x0000a, 8}, {0x0000a, 8}, {0x0000a, 8}, {0x0000a, 8}, 
       
  2022    };
       
  2023 
       
  2024    static const vdxVLCTable_t Intra_tcoefTab2[] = {
       
  2025     {0x10007,11}, {0x10007,11}, {0x10006,11}, {0x10006,11},
       
  2026     {0x00016,11}, {0x00016,11}, {0x00015,11}, {0x00015,11},
       
  2027     {0x10202,10}, {0x10202,10}, {0x10202,10}, {0x10202,10},  
       
  2028     {0x10103,10}, {0x10103,10}, {0x10103,10}, {0x10103,10},
       
  2029     {0x10005,10}, {0x10005,10}, {0x10005,10}, {0x10005,10},
       
  2030     {0x00d01,10}, {0x00d01,10}, {0x00d01,10}, {0x00d01,10},  
       
  2031     {0x00503,10}, {0x00503,10}, {0x00503,10}, {0x00503,10},
       
  2032     {0x00802,10}, {0x00802,10}, {0x00802,10}, {0x00802,10},
       
  2033     {0x00403,10}, {0x00403,10}, {0x00403,10}, {0x00403,10},  
       
  2034     {0x00304,10}, {0x00304,10}, {0x00304,10}, {0x00304,10},
       
  2035     {0x00204,10}, {0x00204,10}, {0x00204,10}, {0x00204,10},
       
  2036     {0x00107,10}, {0x00107,10}, {0x00107,10}, {0x00107,10}, 
       
  2037     {0x00014,10}, {0x00014,10}, {0x00014,10}, {0x00014,10},
       
  2038     {0x00013,10}, {0x00013,10}, {0x00013,10}, {0x00013,10},
       
  2039     {0x00017,11}, {0x00017,11}, {0x00018,11}, {0x00018,11}, 
       
  2040     {0x00108,11}, {0x00108,11}, {0x00902,11}, {0x00902,11},
       
  2041     {0x10302,11}, {0x10302,11}, {0x10402,11}, {0x10402,11},
       
  2042     {0x10f01,11}, {0x10f01,11}, {0x11001,11}, {0x11001,11}, 
       
  2043     {0x00019,12}, {0x0001a,12}, {0x0001b,12}, {0x00109,12},
       
  2044     {0x00603,12}, {0x0010a,12}, {0x00205,12}, {0x00703,12},
       
  2045     {0x00e01,12}, {0x10008,12}, {0x10502,12}, {0x10602,12}, 
       
  2046     {0x11101,12}, {0x11201,12}, {0x11301,12}, {0x11401,12},
       
  2047     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2048     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2049     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2050     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2051     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2052     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2053     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2054     {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7}, {0x01bff, 7},
       
  2055    };
       
  2056 
       
  2057    static const int intra_max_level[2][64] = {
       
  2058                                      {27, 10,  5,  4,  3,  3,  3,  3,
       
  2059                                        2,  2,  1,  1,  1,  1,  1,  0,
       
  2060                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2061                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2062                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2063                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2064                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2065                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2066                                      },
       
  2067  
       
  2068                                       {8,  3,  2,  2,  2,  2,  2,  1,
       
  2069                                        1,  1,  1,  1,  1,  1,  1,  1,
       
  2070                                        1,  1,  1,  1,  1,  0,  0,  0,
       
  2071                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2072                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2073                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2074                                        0,  0,  0,  0,  0,  0,  0,  0,
       
  2075                                        0,  0,  0,  0,  0,  0,  0,  0
       
  2076                                     }
       
  2077                                    };
       
  2078 
       
  2079    static const int intra_max_run0[28] = { 999, 14,  9,  7,  3,  2,  1,
       
  2080                                     1,  1,  1,  1,  0,  0,  0,
       
  2081                                     0,  0,  0,  0,  0,  0,  0,
       
  2082                                     0,  0,  0,  0,  0,  0,  0
       
  2083                                 };
       
  2084  
       
  2085    static const int intra_max_run1[9] = { 999, 20,  6,
       
  2086                                    1,  0,  0,
       
  2087                                    0,  0,  0
       
  2088                                };
       
  2089 
       
  2090    int
       
  2091       code,       /* bits got from bit buffer */
       
  2092       index,      /* index to zigzag table running from 1 to 63 */
       
  2093       run,        /* RUN code */
       
  2094       level;      /* LEVEL code */
       
  2095 
       
  2096    u_int32
       
  2097      last,       /* LAST code (see standard) */
       
  2098       sign;       /* sign for level */
       
  2099 
       
  2100    vdxVLCTable_t const *tab; /* pointer to lookup table */
       
  2101 
       
  2102    vdxAssert(inBuffer != NULL);
       
  2103    vdxAssert(startIndex == 0 || startIndex == 1);
       
  2104    vdxAssert(block != NULL);
       
  2105    vdxAssert(bitErrorIndication != NULL);
       
  2106 
       
  2107    index = startIndex;
       
  2108 
       
  2109    do {
       
  2110       code = (int) bibShowBits(12, inBuffer, &numBitsGot, bitErrorIndication,
       
  2111          &bibError);
       
  2112       if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  2113          deb("vdxGetDCTBlock: bibShowBits returned not enough data --> "
       
  2114             "try to use the data available.\n");
       
  2115          code <<= 12 - numBitsGot;
       
  2116          bibError = 0;
       
  2117       }
       
  2118       else if (bibError ) {
       
  2119          goto exitFunction;
       
  2120       }
       
  2121 
       
  2122       /* Select the right table and index for the codeword */
       
  2123       if (code >= 512)
       
  2124          tab = &Intra_tcoefTab0[(code >> 5) - 16];
       
  2125       else if (code >= 128)
       
  2126          tab = &Intra_tcoefTab1[(code >> 2) - 32];
       
  2127       else if (code >= 8)
       
  2128          tab = &Intra_tcoefTab2[code - 8];
       
  2129       else {
       
  2130          deb("ERROR - illegal TCOEF\n");
       
  2131          retValue = VDX_OK_BUT_BIT_ERROR;
       
  2132          goto exitFunction;
       
  2133       }
       
  2134 
       
  2135       /* Flush the codeword from the buffer */
       
  2136       bibFlushBits(tab->len, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2137       if (bibError)
       
  2138         goto exitFunction;
       
  2139     
       
  2140       /* the following is modified for 3-mode escape */
       
  2141       if (tab->val == 7167)     /* ESCAPE */
       
  2142       {
       
  2143       
       
  2144          int run_offset=0,
       
  2145          level_offset=0;
       
  2146 
       
  2147          code = (int) bibShowBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2148          if (bibError) {
       
  2149             goto exitFunction;
       
  2150          }
       
  2151 
       
  2152          if (code<=2) {
       
  2153 
       
  2154             /* escape modes: level or run is offset */
       
  2155             if (code==2) run_offset=1;
       
  2156             else level_offset=1;
       
  2157 
       
  2158             /* Flush the escape code from the buffer */
       
  2159             if (run_offset)
       
  2160                bibFlushBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2161             else
       
  2162                bibFlushBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2163 
       
  2164             if (bibError)
       
  2165                goto exitFunction;
       
  2166             /* Read next codeword */
       
  2167             code = (int) bibShowBits(12, inBuffer, &numBitsGot, bitErrorIndication,
       
  2168                      &bibError);
       
  2169             if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  2170                deb("vdxGetDCTBlock: bibShowBits returned not enough data --> "
       
  2171                   "try to use the data available.\n");
       
  2172                code <<= 12 - numBitsGot;
       
  2173                bibError = 0;
       
  2174             }
       
  2175             else if (bibError) {
       
  2176                goto exitFunction;
       
  2177             }
       
  2178 
       
  2179             /* Select the right table and index for the codeword */
       
  2180             if (code >= 512)
       
  2181                tab = &Intra_tcoefTab0[(code >> 5) - 16];
       
  2182             else if (code >= 128)
       
  2183                tab = &Intra_tcoefTab1[(code >> 2) - 32];
       
  2184             else if (code >= 8)
       
  2185                tab = &Intra_tcoefTab2[code - 8];
       
  2186             else {
       
  2187                deb("ERROR - illegal TCOEF\n");
       
  2188                retValue = VDX_OK_BUT_BIT_ERROR;
       
  2189                goto exitFunction;
       
  2190             }
       
  2191 
       
  2192             bibFlushBits(tab->len, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2193             if (bibError)
       
  2194                goto exitFunction;
       
  2195 
       
  2196             run = (tab->val >> 8) & 255;
       
  2197             level = tab->val & 255;
       
  2198             last = (tab->val >> 16) & 1;
       
  2199 
       
  2200             /* need to add back the max level */
       
  2201             if (level_offset)
       
  2202                level = level + intra_max_level[last][run];
       
  2203             else if (last)
       
  2204                run = run + intra_max_run1[level]+1;
       
  2205             else
       
  2206                run = run + intra_max_run0[level]+1;
       
  2207 
       
  2208             sign = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,
       
  2209                      &bibError);
       
  2210 
       
  2211             if ( bibError )
       
  2212                goto exitFunction;
       
  2213         
       
  2214             if (sign)
       
  2215                level = -level;
       
  2216 
       
  2217          } else {
       
  2218        
       
  2219             /* Flush the codeword from the buffer */
       
  2220             bibFlushBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2221             if (bibError)
       
  2222                goto exitFunction;
       
  2223 
       
  2224             /* LAST */
       
  2225             last = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2226                      &bibError);
       
  2227             if ( bibError )
       
  2228                goto exitFunction;
       
  2229             /* RUN */
       
  2230             run = (int) bibGetBits(6, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2231                      &bibError);
       
  2232             if ( bibError )
       
  2233                goto exitFunction;
       
  2234             /* MARKER BIT */
       
  2235             tmpvar = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError);
       
  2236             if ( bibError )
       
  2237                goto exitFunction;
       
  2238             if (!tmpvar) {
       
  2239 
       
  2240                retValue = VDX_OK_BUT_BIT_ERROR;
       
  2241                goto exitFunction;
       
  2242             }
       
  2243             /* LEVEL */
       
  2244             level = (int) bibGetBits(12, inBuffer, &numBitsGot, bitErrorIndication, 
       
  2245                         &bibError);
       
  2246             if ( bibError )
       
  2247                goto exitFunction;
       
  2248             /* MARKER BIT */
       
  2249             tmpvar = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError);
       
  2250             if ( bibError )
       
  2251                goto exitFunction;
       
  2252             if(!tmpvar) {
       
  2253 
       
  2254                retValue = VDX_OK_BUT_BIT_ERROR;
       
  2255                goto exitFunction;
       
  2256             }
       
  2257 
       
  2258             /* 0000 0000 0000 and 1000 0000 0000 is forbidden unless in MQ mode */
       
  2259             if (level == 0 || level == 2048) {
       
  2260                deb("ERROR - illegal level.\n");
       
  2261                retValue = VDX_OK_BUT_BIT_ERROR;
       
  2262                goto exitFunction;
       
  2263             }
       
  2264 
       
  2265             /* Codes 1000 0000 0001 .. 1111 1111 1111 */
       
  2266             if (level > 2048)
       
  2267                level -= 4096;
       
  2268       
       
  2269          } /* flc */
       
  2270       }
       
  2271       else {
       
  2272    
       
  2273          run = (tab->val >> 8) & 255;
       
  2274          level = tab->val & 255;
       
  2275          last = (tab->val >> 16) & 1;
       
  2276 
       
  2277          sign = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2278          if ( bibError )
       
  2279             goto exitFunction;
       
  2280 
       
  2281          if (sign)
       
  2282             level = -level;
       
  2283       }
       
  2284 
       
  2285       /* If too many coefficients */
       
  2286       if (index + run > 63) {
       
  2287          deb("ERROR - too many TCOEFs.\n");
       
  2288          retValue = VDX_OK_BUT_BIT_ERROR;
       
  2289          goto exitFunction;
       
  2290       }
       
  2291 
       
  2292       /* Do run-length decoding */
       
  2293          while (run--)
       
  2294             block[index++] = 0;
       
  2295 
       
  2296          block[index++] = level;
       
  2297 
       
  2298    } while (!last);
       
  2299 
       
  2300    exitFunction:
       
  2301 
       
  2302    /* Set the rest of the coefficients to zero */
       
  2303    while (index <= 63) {
       
  2304       block[index++] = 0;
       
  2305    }
       
  2306 
       
  2307    if (!bibError)
       
  2308       return retValue;
       
  2309 
       
  2310    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  2311       return VDX_OK_BUT_BIT_ERROR;
       
  2312    }
       
  2313 
       
  2314    else
       
  2315       return VDX_ERR_BIB;
       
  2316 }
       
  2317 
       
  2318 /*
       
  2319  * Macroblock Layer Local Functions
       
  2320  */
       
  2321 
       
  2322 /*
       
  2323  *
       
  2324  * vdxGetScaledMVD
       
  2325  *    
       
  2326  *
       
  2327  * Parameters:
       
  2328  *    inBuffer     input buffer
       
  2329  *    f_code       f_code for current Vop
       
  2330  *    error        error code
       
  2331  *    *mvd10       returned MV value (10x)
       
  2332  *    *bitErrorIndication
       
  2333  *
       
  2334  * Function:
       
  2335  *      Calculates a component of a block (or MB) vector by decoding 
       
  2336  *      the magnitude & residual of the diff. vector, making the prediction, 
       
  2337  *      and combining the decoded diff. and the predicted values
       
  2338  *
       
  2339  * Returns:
       
  2340  *    VDX_OK                     the function was successful
       
  2341  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  2342  *                               occured
       
  2343  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  2344  *
       
  2345  *    
       
  2346  */
       
  2347 
       
  2348 
       
  2349 int vdxGetScaledMVD(bibBuffer_t *inBuffer, int f_code,
       
  2350             int *mvd10, int *bitErrorIndication)
       
  2351 {
       
  2352 
       
  2353    int   residual=0, vlc_code_mag=0; 
       
  2354    int   diff_vector,
       
  2355          retValue = VDX_OK,
       
  2356          numBitsGot;
       
  2357    int16 bibError = 0;
       
  2358 
       
  2359    /* decode component */
       
  2360    retValue = vdxGetMVD(inBuffer,&vlc_code_mag,bitErrorIndication);
       
  2361    if (retValue < 0)
       
  2362       goto exitFunction;
       
  2363 
       
  2364    if ((f_code > 1) && (vlc_code_mag != 0))
       
  2365    {
       
  2366       vlc_code_mag /= 5;
       
  2367 
       
  2368       residual = (int) 
       
  2369          bibGetBits(f_code-1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2370       if (bibError)
       
  2371          goto exitFunction;
       
  2372 
       
  2373       diff_vector = ((abs(vlc_code_mag)-1)<<(f_code-1)) + residual + 1;
       
  2374       if (vlc_code_mag < 0)
       
  2375          diff_vector = -diff_vector;
       
  2376 
       
  2377       *mvd10 = diff_vector * 5;
       
  2378    } else 
       
  2379       *mvd10 = vlc_code_mag;
       
  2380 
       
  2381    exitFunction:
       
  2382    
       
  2383    if (bibError) {
       
  2384       if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  2385          return VDX_OK_BUT_BIT_ERROR;
       
  2386       }
       
  2387       return VDX_ERR_BIB;
       
  2388    }
       
  2389 
       
  2390    return retValue;
       
  2391 }
       
  2392 
       
  2393 /*
       
  2394  * vdxGetIntraDC
       
  2395  *    
       
  2396  *
       
  2397  * Parameters:
       
  2398  *    inBuffer                   pointer to bit buffer instance
       
  2399  *    compnum                    number of the block component in the
       
  2400  *                       4:2:2 YUV scheme 
       
  2401  *                       (0..3) luma, (4..5) chroma
       
  2402  *    IntraDCDelta               the read Intra DC value (quantized)
       
  2403  *    bitErrorIndication         non-zero if a bit error has been detected
       
  2404  *                               within the bits accessed in this function,
       
  2405  *                               see biterr.h for possible values
       
  2406  *
       
  2407  * Function:
       
  2408  *    This function gets the Intra DC value
       
  2409  *
       
  2410  * Returns:
       
  2411  *    VDX_OK                     the function was successful
       
  2412  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  2413  *                               occured
       
  2414  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  2415  *
       
  2416  *    
       
  2417  *
       
  2418  */
       
  2419 
       
  2420 int vdxGetIntraDC(bibBuffer_t *inBuffer, bibBuffer_t */*outBuffer*/, bibBufferEdit_t */*bufEdit*/, int /*aColorEffect*/, 
       
  2421                                     int */*aStartByteIndex*/, int */*aStartBitIndex*/, int compnum, int *IntraDC_size, int *IntraDCDelta, 
       
  2422                                     int *bitErrorIndication)
       
  2423 {
       
  2424    u_int32 code;
       
  2425    int16 bibError=0;
       
  2426    int first_bit,
       
  2427       numBitsGot,
       
  2428       retValue,
       
  2429       IntraDCSize=0,
       
  2430       tmpvar;
       
  2431   
       
  2432    vdxAssert(inBuffer != NULL);
       
  2433    vdxAssert(IntraDCDelta != NULL);
       
  2434    vdxAssert(bitErrorIndication != NULL);
       
  2435 
       
  2436    /* read DC size 2 - 8 bits */
       
  2437    retValue =  vdxGetIntraDCSize(inBuffer, compnum, &IntraDCSize, bitErrorIndication);
       
  2438    if (retValue != VDX_OK)
       
  2439       return retValue;
       
  2440   
       
  2441    *IntraDC_size = IntraDCSize;
       
  2442    if (IntraDCSize == 0) {
       
  2443 
       
  2444       *IntraDCDelta = 0;
       
  2445 
       
  2446    } else {
       
  2447 
       
  2448       /* read delta DC 0 - 8 bits */
       
  2449       code = bibGetBits(IntraDCSize,inBuffer,&numBitsGot,bitErrorIndication, &bibError);
       
  2450 
       
  2451       if (bibError) {
       
  2452          if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  2453             return VDX_OK_BUT_BIT_ERROR;
       
  2454          }
       
  2455          return VDX_ERR_BIB;
       
  2456       } 
       
  2457 
       
  2458       first_bit = code >> (IntraDCSize-1);
       
  2459 
       
  2460       if (first_bit == 0 )
       
  2461       { /* negative delta INTRA DC */
       
  2462          *IntraDCDelta = -1 * (int) (code ^ ((1 << IntraDCSize)-1));
       
  2463       }
       
  2464       else
       
  2465       { /* positive delta INTRA DC */
       
  2466          *IntraDCDelta = (int) code;
       
  2467       }
       
  2468 
       
  2469       if (IntraDCSize > 8) {
       
  2470          /* Marker Bit */
       
  2471          tmpvar = bibGetBits(1,inBuffer,&numBitsGot,bitErrorIndication, &bibError);
       
  2472          if( !tmpvar ) {
       
  2473             return VDX_OK_BUT_BIT_ERROR;
       
  2474          }
       
  2475       }
       
  2476    }
       
  2477                         
       
  2478    return VDX_OK;
       
  2479 }
       
  2480 
       
  2481 /*
       
  2482  * vdxGetIntraDCSize
       
  2483  *    
       
  2484  *
       
  2485  * Parameters:
       
  2486  *    inBuffer                   pointer to bit buffer instance
       
  2487  *    compnum                    number of the block component in the
       
  2488  *                       4:2:2 YUV scheme 
       
  2489  *                       (0..3) luma, (4..5) chroma
       
  2490  *    IntraDCSize                Size of the following Intra DC FLC
       
  2491  *    bitErrorIndication         non-zero if a bit error has been detected
       
  2492  *                               within the bits accessed in this function,
       
  2493  *                               see biterr.h for possible values
       
  2494  *
       
  2495  * Function:
       
  2496  *    This function gets the IntraDCSize VLC for luma or chroma
       
  2497  *
       
  2498  * Returns:
       
  2499  *    VDX_OK                     the function was successful
       
  2500  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  2501  *                               occured
       
  2502  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  2503  *
       
  2504  *    
       
  2505  *
       
  2506  */
       
  2507 
       
  2508 static int vdxGetIntraDCSize(bibBuffer_t *inBuffer, int compnum, int *IntraDCSize, 
       
  2509    int *bitErrorIndication)
       
  2510 {
       
  2511    u_int32  code;
       
  2512    int numBitsGot,
       
  2513       retValue = VDX_OK;
       
  2514    int16
       
  2515       bibError = 0;
       
  2516 
       
  2517    if( compnum >=0 && compnum < 4 ) /* luminance block */
       
  2518    {
       
  2519       code = bibShowBits(11, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2520       if (bibError && (bibError != ERR_BIB_NOT_ENOUGH_DATA)) {
       
  2521          return VDX_ERR_BIB;
       
  2522       }
       
  2523 
       
  2524       if ((bibError == ERR_BIB_NOT_ENOUGH_DATA) && (numBitsGot != 0)) {
       
  2525          code <<= (11-numBitsGot);
       
  2526          bibError = 0;
       
  2527       }
       
  2528 
       
  2529       if ( code == 1)
       
  2530       {
       
  2531          *IntraDCSize = 12;
       
  2532          bibFlushBits(11, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2533          goto exitFunction;
       
  2534       }
       
  2535       
       
  2536       code >>= 1;
       
  2537       if ( code == 1)
       
  2538       {
       
  2539          *IntraDCSize = 11;
       
  2540          bibFlushBits(10, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2541          goto exitFunction;
       
  2542       }
       
  2543 
       
  2544       code >>= 1;
       
  2545       if ( code == 1)
       
  2546       {
       
  2547          *IntraDCSize = 10;
       
  2548          bibFlushBits(9, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2549          goto exitFunction;
       
  2550       }
       
  2551 
       
  2552       code >>= 1;
       
  2553       if ( code == 1)
       
  2554       {
       
  2555          *IntraDCSize = 9;
       
  2556          bibFlushBits(8, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2557          goto exitFunction;
       
  2558       }
       
  2559 
       
  2560       code >>= 1;
       
  2561       if ( code == 1)
       
  2562       {
       
  2563          *IntraDCSize = 8;
       
  2564          bibFlushBits(7, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2565          goto exitFunction;
       
  2566       }
       
  2567       code >>= 1;
       
  2568       if ( code == 1)
       
  2569       {
       
  2570          *IntraDCSize = 7;
       
  2571          bibFlushBits(6, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2572          goto exitFunction;
       
  2573       }
       
  2574       code >>= 1;
       
  2575       if ( code == 1)
       
  2576       {
       
  2577          *IntraDCSize = 6;
       
  2578          bibFlushBits(5, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2579          goto exitFunction;
       
  2580       }
       
  2581       code >>= 1;
       
  2582       if ( code == 1)
       
  2583       {
       
  2584          *IntraDCSize = 5;
       
  2585          bibFlushBits(4, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2586          goto exitFunction;
       
  2587       }
       
  2588       code >>= 1;
       
  2589       if ( code == 1)
       
  2590       {
       
  2591          *IntraDCSize = 4;
       
  2592          bibFlushBits(3, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2593          goto exitFunction;
       
  2594       } else if (code == 2) {
       
  2595          *IntraDCSize = 3;
       
  2596          bibFlushBits(3, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2597          goto exitFunction;
       
  2598       } else if (code ==3) {
       
  2599          *IntraDCSize = 0;
       
  2600          bibFlushBits(3, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2601          goto exitFunction;
       
  2602       }
       
  2603       code >>= 1;
       
  2604       if ( code == 2)
       
  2605       {
       
  2606          *IntraDCSize = 2;
       
  2607          bibFlushBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2608          goto exitFunction;
       
  2609       } else if (code == 3) {
       
  2610          *IntraDCSize = 1;
       
  2611          bibFlushBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2612          goto exitFunction;
       
  2613       }     
       
  2614 
       
  2615    }
       
  2616    else /* chrominance block */
       
  2617    {
       
  2618       code = bibShowBits(12, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2619       if (bibError && (bibError != ERR_BIB_NOT_ENOUGH_DATA)) {
       
  2620          return VDX_ERR_BIB;
       
  2621       }
       
  2622 
       
  2623       if ((bibError == ERR_BIB_NOT_ENOUGH_DATA) && (numBitsGot != 0)) {
       
  2624          code <<= (12-numBitsGot);
       
  2625          bibError = 0;
       
  2626       }
       
  2627 
       
  2628       if ( code == 1)
       
  2629       {
       
  2630          *IntraDCSize = 12;
       
  2631          bibFlushBits(12, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2632          goto exitFunction;
       
  2633       }
       
  2634       
       
  2635       code >>= 1;
       
  2636       if ( code == 1)
       
  2637       {
       
  2638          *IntraDCSize = 11;
       
  2639          bibFlushBits(11, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2640          goto exitFunction;
       
  2641       }
       
  2642 
       
  2643       code >>= 1;
       
  2644       if ( code == 1)
       
  2645       {
       
  2646          *IntraDCSize = 10;
       
  2647          bibFlushBits(10, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2648          goto exitFunction;
       
  2649       }
       
  2650 
       
  2651       code >>= 1;
       
  2652       if ( code == 1)
       
  2653       {
       
  2654          *IntraDCSize = 9;
       
  2655          bibFlushBits(9, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2656          goto exitFunction;
       
  2657       }
       
  2658 
       
  2659       code >>= 1;
       
  2660       if ( code == 1)
       
  2661       {
       
  2662          *IntraDCSize = 8;
       
  2663          bibFlushBits(8, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2664          goto exitFunction;
       
  2665       }
       
  2666       code >>= 1;
       
  2667       if ( code == 1)
       
  2668       {
       
  2669          *IntraDCSize = 7;
       
  2670          bibFlushBits(7, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2671          goto exitFunction;
       
  2672       }
       
  2673       code >>= 1;
       
  2674       if ( code == 1)
       
  2675       {
       
  2676          *IntraDCSize = 6;
       
  2677          bibFlushBits(6, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2678          goto exitFunction;
       
  2679       }
       
  2680       code >>= 1;
       
  2681       if ( code == 1)
       
  2682       {
       
  2683          *IntraDCSize = 5;
       
  2684          bibFlushBits(5, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2685          goto exitFunction;
       
  2686       }
       
  2687       code >>= 1;
       
  2688       if ( code == 1)
       
  2689       {
       
  2690          *IntraDCSize = 4;
       
  2691          bibFlushBits(4, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2692          goto exitFunction;
       
  2693       } 
       
  2694       code >>= 1;
       
  2695       if ( code == 1)
       
  2696       {
       
  2697          *IntraDCSize = 3;
       
  2698          bibFlushBits(3, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2699          goto exitFunction;
       
  2700       } 
       
  2701       code >>= 1;
       
  2702       {
       
  2703          *IntraDCSize = 3-code;
       
  2704          bibFlushBits(2, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  2705          goto exitFunction;
       
  2706       } 
       
  2707    }
       
  2708 
       
  2709 exitFunction:
       
  2710 
       
  2711    /* If no error in bit buffer functions */
       
  2712    if (!bibError)
       
  2713       return retValue;
       
  2714 
       
  2715    /* Else if ran out of data (i.e. decoding out of sync) */
       
  2716    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  2717       return VDX_OK_BUT_BIT_ERROR;
       
  2718    }
       
  2719 
       
  2720    /* Else other error in bit buffer functions */
       
  2721    else
       
  2722       return VDX_ERR_BIB;
       
  2723 }
       
  2724 
       
  2725 /*
       
  2726  * vdxGetRVLCIndex
       
  2727  *    
       
  2728  *
       
  2729  * Parameters:
       
  2730  *   bits                   input: the bits read from the stream
       
  2731  *   index                  output: the RVLC table index corresponding
       
  2732  *                       to "bits"
       
  2733  *   length              output: length of the codeword
       
  2734  *   intra_luma             indicates an intra "1" or inter "0" Block
       
  2735  *    bitErrorIndication         non-zero if a bit error has been detected
       
  2736  *                               within the bits accessed in this function,
       
  2737  *                               see biterr.h for possible values
       
  2738  *
       
  2739  * Function:
       
  2740  *    This function finds the RVLC table index (LAST,RUN.LEVEL) and length
       
  2741  *   of the code belonging to the input codeword.
       
  2742  *
       
  2743  * Returns:
       
  2744  *    VDX_OK                     the function was successful
       
  2745  *    VDX_OK_BUT_BIT_ERROR       no such codeword exists in the table
       
  2746  *
       
  2747  *
       
  2748  */
       
  2749 
       
  2750 int vdxGetRVLCIndex(
       
  2751    u_int32 bits,
       
  2752    u_int32 *index,
       
  2753    int *length,
       
  2754    int intra_luma,
       
  2755    int *bitErrorIndication)
       
  2756 {
       
  2757 
       
  2758    /* The indexes in the RVLC tables are written in equal RUN groups with
       
  2759       LEVEL increasing */
       
  2760    
       
  2761    /* Intra RVLC table */
       
  2762    static const vdxVLCTable_t RvlcTcoefTab0[] = {
       
  2763   /* [0] --> e.g.: RUN = 0; LEVEL = 1..27 */
       
  2764       {1,3},   {2,3},   {3,4},   {4,5},   {5,6},   {6,6},   {7,7},   {8,8},
       
  2765       {9,8},  {10,9},  {11,9}, {12,10}, {13,10}, {14,10}, {15,11}, {16,11},
       
  2766     {17,11}, {18,12}, {19,12}, {20,13}, {21,13}, {22,12}, {23,13}, {24,14},
       
  2767     {25,14}, {26,14}, {27,15},
       
  2768   /* [27] */
       
  2769     {257,4}, {258,5}, {259,7}, {260,8}, {261,8}, {262,9},{263,10},{264,11},
       
  2770    {265,11},{266,12},{267,13},{268,14},{269,14},
       
  2771   /* [40] */
       
  2772     {513,5}, {514,7}, {515,9},{516,10},{517,11},{518,11},{519,13},{520,13},
       
  2773    {521,13},{522,14},{523,14},
       
  2774   /* [51] */   
       
  2775     {769,5}, {770,8}, {771,9},{772,11},{773,12},{774,13},{775,14},{776,14},
       
  2776    {777,15},
       
  2777   /* [60] */   
       
  2778    {1025,6},{1026,8},{1027,10},{1028,12},{1029,12},{1030,14},
       
  2779   /* [66] */   
       
  2780    {1281,6},{1282,9},{1283,11},{1284,12},{1285,14},{1286,14},
       
  2781   /* [72] */     
       
  2782    {1537,7},{1538,10},{1539,11},{1540,12},{1541,15},
       
  2783   /* [77] */   
       
  2784    {1793,7},{1794,10},{1795,11},{1796,13},{1797,15},
       
  2785   /* [82] */
       
  2786    {2049,8},{2050,10},{2051,13},{2052,14},
       
  2787   /* [86] */   
       
  2788    {2305,8},{2306,11},{2307,13},{2308,15},
       
  2789   /* [90] */
       
  2790    {2561,9},{2562,12},
       
  2791   /* [92] */      
       
  2792    {2817,10},{2818,13},
       
  2793   /* [94] */      
       
  2794    {3073,10},{3074,15},
       
  2795   /* [96] */
       
  2796    {3329,11},
       
  2797    {3585,13},
       
  2798    {3841,13},
       
  2799    {4097,14},
       
  2800    {4353,14},  
       
  2801    {4609,14},
       
  2802    {4865,15},
       
  2803 
       
  2804   /* [103] --> LAST = 1 */    
       
  2805    {65537,4},{65538,8},{65539,11},{65540,13},{65541,14},
       
  2806   /* [108] */     
       
  2807    {65793,5},{65794,9},{65795,12},{65796,14},{65797,15},
       
  2808   /* [113] */
       
  2809    {66049,5},{66050,11},{66051,15},
       
  2810   /* [116] */     
       
  2811    {66305,6},{66306,12},
       
  2812   /* [118] */
       
  2813    {66561,6},{66562,12},
       
  2814   /* [120] */
       
  2815    {66817,6},{66818,13},
       
  2816   /* [122] */
       
  2817    {67073,6},{67074,13},
       
  2818   /* [124] */
       
  2819    {67329,7},{67330,13},
       
  2820   /* [126] */
       
  2821    {67585,7},{67586,13},
       
  2822   /* [128] */
       
  2823    {67841,7},{67842,13},
       
  2824   /* [130] */
       
  2825    {68097,7},{68098,14},
       
  2826   /* [132] */
       
  2827    {68353,7},{68354,14},
       
  2828   /* [134] */
       
  2829    {68609,8},{68610,14},
       
  2830   /* [136] */
       
  2831    {68865,8},{68866,15},
       
  2832   /* [138] */
       
  2833    {69121,8},
       
  2834    {69377,9},
       
  2835    {69633,9},
       
  2836    {69889,9},
       
  2837    {70145,9},
       
  2838    {70401,9},
       
  2839    {70657,9},
       
  2840    {70913,10},
       
  2841    {71169,10},
       
  2842    {71425,10},
       
  2843    {71681,10},
       
  2844    {71937,10},
       
  2845    {72193,11},
       
  2846    {72449,11},
       
  2847    {72705,11},
       
  2848    {72961,12},
       
  2849    {73217,12},
       
  2850    {73473,12},
       
  2851    {73729,12},
       
  2852    {73985,12},
       
  2853    {74241,12},
       
  2854    {74497,12},
       
  2855    {74753,13},
       
  2856    {75009,13},
       
  2857    {75265,14},
       
  2858    {75521,14},
       
  2859    {75777,14},
       
  2860    {76033,15},
       
  2861    {76289,15},
       
  2862    {76545,15},
       
  2863    {76801,15},
       
  2864 
       
  2865   /* [169] */
       
  2866      {7167,4}   /* last entry: escape code */
       
  2867   
       
  2868 };
       
  2869 
       
  2870    /* Inter RVLC table */
       
  2871    static const vdxVLCTable_t RvlcTcoefTab1[] = {
       
  2872   /* [0] */
       
  2873       {1,3},   {2,4},   {3,5},   {4,7},   {5,8},   {6,8},   {7,9},  {8,10},
       
  2874      {9,10}, {10,11}, {11,11}, {12,12}, {13,13}, {14,13}, {15,13}, {16,13},
       
  2875     {17,14}, {18,14}, {19,15},
       
  2876   /* [19] */
       
  2877     {257,3}, {258,6}, {259,8}, {260,9},{261,10},{262,11},{263,12},{264,13},
       
  2878    {265,14},{266,14},
       
  2879   /* [29] */
       
  2880     {513,4}, {514,7}, {515,9},{516,11},{517,12},{518,14},{519,14},
       
  2881   /* [36] */
       
  2882     {769,5}, {770,8},{771,10},{772,12},{773,13},{774,14},{775,15},
       
  2883   /* [43] */
       
  2884    {1025,5},{1026,8},{1027,11},{1028,13},{1029,15},
       
  2885   /* [48] */      
       
  2886    {1281,5},{1282,9},{1283,11},{1284,13},
       
  2887   /* [52] */      
       
  2888    {1537,6},{1538,10},{1539,12},{1540,14},
       
  2889   /* [56] */
       
  2890    {1793,6},{1794,10},{1795,12},{1796,15},
       
  2891   /* [60] */
       
  2892    {2049,6},{2050,10},{2051,13},
       
  2893   /* [63] */      
       
  2894    {2305,7},{2306,10},{2307,14},
       
  2895   /* [66] */
       
  2896    {2561,7},{2562,11},
       
  2897   /* [68] */
       
  2898     {2817,7},{2818,12},
       
  2899   /* [70] */
       
  2900     {3073,8},{3074,13},
       
  2901   /* [72] */
       
  2902    {3329,8},{3330,14},
       
  2903   /* [74] */
       
  2904     {3585,8},{3586,14},
       
  2905   /* [76] */
       
  2906     {3841,9},{3842,14},
       
  2907   /* [78] */
       
  2908     {4097,9},{4098,14},
       
  2909   /* [80] */
       
  2910    {4353,9},{4354,15},
       
  2911   /* [82] */
       
  2912    {4609,10},
       
  2913    {4865,10},
       
  2914    {5121,10},
       
  2915    {5377,11},
       
  2916    {5633,11},
       
  2917    {5889,11},
       
  2918    {6145,11},
       
  2919    {6401,11},
       
  2920    {6657,11},
       
  2921    {6913,12},
       
  2922    {7169,12},
       
  2923    {7425,12},
       
  2924    {7681,13},
       
  2925    {7937,13},
       
  2926    {8193,13},
       
  2927    {8449,13},
       
  2928    {8705,14},
       
  2929    {8961,14},
       
  2930    {9217,14},
       
  2931    {9473,15},
       
  2932    {9729,15},
       
  2933 
       
  2934   /* [103] --> LAST = 1 */
       
  2935    {65537,4},{65538,8},{65539,11},{65540,13},{65541,14},
       
  2936   /* [108] */
       
  2937    {65793,5},{65794,9},{65795,12},{65796,14},{65797,15},
       
  2938   /* [113] */
       
  2939    {66049,5},{66050,11},{66051,15},
       
  2940   /* [116] */     
       
  2941    {66305,6},{66306,12},
       
  2942   /* [118] */
       
  2943    {66561,6},{66562,12},
       
  2944   /* [120] */
       
  2945    {66817,6},{66818,13},
       
  2946   /* [122] */
       
  2947    {67073,6},{67074,13},
       
  2948   /* [124] */
       
  2949    {67329,7},{67330,13},
       
  2950   /* [126] */
       
  2951    {67585,7},{67586,13},
       
  2952   /* [128] */
       
  2953    {67841,7},{67842,13},
       
  2954   /* [130] */
       
  2955    {68097,7},{68098,14},
       
  2956   /* [132] */
       
  2957    {68353,7},{68354,14},
       
  2958   /* [134] */
       
  2959    {68609,8},{68610,14},
       
  2960   /* [136] */
       
  2961    {68865,8},{68866,15},
       
  2962   /* [138] */
       
  2963    {69121,8},
       
  2964    {69377,9},
       
  2965    {69633,9},
       
  2966    {69889,9},
       
  2967    {70145,9},
       
  2968    {70401,9},
       
  2969    {70657,9},
       
  2970   {70913,10},
       
  2971   {71169,10},
       
  2972   {71425,10},
       
  2973   {71681,10},
       
  2974   {71937,10},
       
  2975   {72193,11},
       
  2976   {72449,11},
       
  2977   {72705,11},
       
  2978   {72961,12},
       
  2979   {73217,12},
       
  2980   {73473,12},
       
  2981   {73729,12},
       
  2982   {73985,12},
       
  2983   {74241,12},
       
  2984   {74497,12},
       
  2985   {74753,13},
       
  2986   {75009,13},
       
  2987   {75265,14},
       
  2988   {75521,14},
       
  2989   {75777,14},
       
  2990   {76033,15},
       
  2991   {76289,15},
       
  2992   {76545,15},
       
  2993   {76801,15},
       
  2994 
       
  2995   /* [169] */
       
  2996    {7167,4}          /* last entry: escape code */
       
  2997 };
       
  2998 
       
  2999    vdxVLCTable_t const *tab; /* pointer to lookup table */
       
  3000 
       
  3001    vdxAssert(bitErrorIndication != NULL);
       
  3002 
       
  3003     switch(bits) {
       
  3004  
       
  3005     case 0x0:
       
  3006       if (intra_luma)
       
  3007         tab = &RvlcTcoefTab0[169];
       
  3008       else
       
  3009         tab = &RvlcTcoefTab1[169];
       
  3010       break;
       
  3011       
       
  3012     case 0x1: 
       
  3013       if (intra_luma)
       
  3014         tab = &RvlcTcoefTab0[27];
       
  3015       else
       
  3016         tab = &RvlcTcoefTab1[1];
       
  3017       break;
       
  3018     
       
  3019     case 0x4: 
       
  3020       if (intra_luma)
       
  3021         tab = &RvlcTcoefTab0[40];
       
  3022       else
       
  3023         tab = &RvlcTcoefTab1[2];
       
  3024        break;
       
  3025     
       
  3026     case 0x5:
       
  3027       if (intra_luma)
       
  3028         tab = &RvlcTcoefTab0[51];
       
  3029       else
       
  3030         tab = &RvlcTcoefTab1[36];
       
  3031       break;
       
  3032       
       
  3033     case 0x6:
       
  3034       if (intra_luma)
       
  3035         tab = &RvlcTcoefTab0[0];
       
  3036       else
       
  3037         tab = &RvlcTcoefTab1[0];
       
  3038       break;
       
  3039       
       
  3040     case 0x7:
       
  3041       if (intra_luma)
       
  3042         tab = &RvlcTcoefTab0[1];
       
  3043       else
       
  3044         tab = &RvlcTcoefTab1[19];
       
  3045       break;
       
  3046       
       
  3047     case 0x8:
       
  3048       if (intra_luma)
       
  3049         tab = &RvlcTcoefTab0[28];
       
  3050       else
       
  3051         tab = &RvlcTcoefTab1[43];
       
  3052       break;
       
  3053       
       
  3054     case 0x9:
       
  3055       if (intra_luma)
       
  3056         tab = &RvlcTcoefTab0[3];
       
  3057       else
       
  3058         tab = &RvlcTcoefTab1[48];
       
  3059       break;
       
  3060       
       
  3061     case 0xa:
       
  3062       if (intra_luma)
       
  3063         tab = &RvlcTcoefTab0[2];
       
  3064       else
       
  3065         tab = &RvlcTcoefTab1[29];
       
  3066       break;
       
  3067       
       
  3068     case 0xb:
       
  3069       if (intra_luma)
       
  3070         tab = &RvlcTcoefTab0[103];
       
  3071       else
       
  3072         tab = &RvlcTcoefTab1[103];
       
  3073       break;
       
  3074       
       
  3075     case 0xc:
       
  3076       if (intra_luma)
       
  3077         tab = &RvlcTcoefTab0[60];
       
  3078       else
       
  3079         tab = &RvlcTcoefTab1[20];
       
  3080       break;
       
  3081       
       
  3082     case 0xd:
       
  3083       if (intra_luma)
       
  3084         tab = &RvlcTcoefTab0[66];
       
  3085       else
       
  3086         tab = &RvlcTcoefTab1[52];
       
  3087       break;
       
  3088       
       
  3089     case 0x12:
       
  3090       if (intra_luma)
       
  3091         tab = &RvlcTcoefTab0[108];
       
  3092       else
       
  3093         tab = &RvlcTcoefTab1[108];
       
  3094       break;
       
  3095       
       
  3096     case 0x13:
       
  3097       if (intra_luma)
       
  3098         tab = &RvlcTcoefTab0[113];
       
  3099       else
       
  3100         tab = &RvlcTcoefTab1[113];
       
  3101       break;
       
  3102       
       
  3103     case 0x14:
       
  3104       if (intra_luma)
       
  3105         tab = &RvlcTcoefTab0[4];
       
  3106       else
       
  3107         tab = &RvlcTcoefTab1[56];
       
  3108       break;
       
  3109       
       
  3110     case 0x15:
       
  3111       if (intra_luma)
       
  3112         tab = &RvlcTcoefTab0[5];
       
  3113       else
       
  3114         tab = &RvlcTcoefTab1[60];
       
  3115       break;
       
  3116       
       
  3117     case 0x18:
       
  3118       if (intra_luma)
       
  3119         tab = &RvlcTcoefTab0[116];
       
  3120       else
       
  3121         tab = &RvlcTcoefTab1[116];
       
  3122       break;
       
  3123       
       
  3124     case 0x19:
       
  3125       if (intra_luma)
       
  3126         tab = &RvlcTcoefTab0[118];
       
  3127       else
       
  3128         tab = &RvlcTcoefTab1[118];
       
  3129       break;
       
  3130       
       
  3131     case 0x1c:
       
  3132       if (intra_luma)
       
  3133         tab = &RvlcTcoefTab0[72];
       
  3134       else
       
  3135         tab = &RvlcTcoefTab1[3];
       
  3136       break;
       
  3137       
       
  3138     case 0x1d:
       
  3139       if (intra_luma)
       
  3140         tab = &RvlcTcoefTab0[77];
       
  3141       else
       
  3142         tab = &RvlcTcoefTab1[30];
       
  3143       break;
       
  3144       
       
  3145     case 0x22:
       
  3146       if (intra_luma)
       
  3147         tab = &RvlcTcoefTab0[120];
       
  3148       else
       
  3149         tab = &RvlcTcoefTab1[120];
       
  3150       break;
       
  3151       
       
  3152     case 0x23:
       
  3153       if (intra_luma)
       
  3154         tab = &RvlcTcoefTab0[122];
       
  3155       else
       
  3156         tab = &RvlcTcoefTab1[122];
       
  3157       break;
       
  3158       
       
  3159     case 0x2c:
       
  3160       if (intra_luma)
       
  3161         tab = &RvlcTcoefTab0[41];
       
  3162       else
       
  3163         tab = &RvlcTcoefTab1[63];
       
  3164       break;
       
  3165       
       
  3166     case 0x2d:
       
  3167       if (intra_luma)
       
  3168         tab = &RvlcTcoefTab0[29];
       
  3169       else
       
  3170         tab = &RvlcTcoefTab1[66];
       
  3171       break;
       
  3172       
       
  3173     case 0x34:
       
  3174       if (intra_luma)
       
  3175         tab = &RvlcTcoefTab0[6];
       
  3176       else
       
  3177         tab = &RvlcTcoefTab1[68];
       
  3178       break;
       
  3179       
       
  3180     case 0x35:
       
  3181       if (intra_luma)
       
  3182         tab = &RvlcTcoefTab0[124];
       
  3183       else
       
  3184         tab = &RvlcTcoefTab1[124];
       
  3185       break;
       
  3186       
       
  3187     case 0x38:
       
  3188       if (intra_luma)
       
  3189         tab = &RvlcTcoefTab0[126];
       
  3190       else
       
  3191         tab = &RvlcTcoefTab1[126];
       
  3192       break;
       
  3193       
       
  3194     case 0x39:
       
  3195       if (intra_luma)
       
  3196         tab = &RvlcTcoefTab0[128];
       
  3197       else
       
  3198         tab = &RvlcTcoefTab1[128];
       
  3199       break;
       
  3200       
       
  3201     case 0x3c:
       
  3202       if (intra_luma)
       
  3203         tab = &RvlcTcoefTab0[82];
       
  3204       else
       
  3205         tab = &RvlcTcoefTab1[4];
       
  3206       break;
       
  3207       
       
  3208     case 0x3d:
       
  3209       if (intra_luma)
       
  3210         tab = &RvlcTcoefTab0[86];
       
  3211       else
       
  3212         tab = &RvlcTcoefTab1[5];
       
  3213       break;
       
  3214       
       
  3215     case 0x42:
       
  3216       if (intra_luma)
       
  3217         tab = &RvlcTcoefTab0[130];
       
  3218       else
       
  3219         tab = &RvlcTcoefTab1[130];
       
  3220       break;
       
  3221       
       
  3222     case 0x43:
       
  3223       if (intra_luma)
       
  3224         tab = &RvlcTcoefTab0[132];
       
  3225       else
       
  3226         tab = &RvlcTcoefTab1[132];
       
  3227       break;
       
  3228       
       
  3229     case 0x5c:
       
  3230       if (intra_luma)
       
  3231         tab = &RvlcTcoefTab0[52];
       
  3232       else
       
  3233         tab = &RvlcTcoefTab1[21];
       
  3234       break;
       
  3235       
       
  3236     case 0x5d:
       
  3237       if (intra_luma)
       
  3238         tab = &RvlcTcoefTab0[61];
       
  3239       else
       
  3240         tab = &RvlcTcoefTab1[37];
       
  3241       break;
       
  3242       
       
  3243     case 0x6c:
       
  3244       if (intra_luma)
       
  3245         tab = &RvlcTcoefTab0[30];
       
  3246       else
       
  3247         tab = &RvlcTcoefTab1[44];
       
  3248       break;
       
  3249       
       
  3250     case 0x6d:
       
  3251       if (intra_luma)
       
  3252         tab = &RvlcTcoefTab0[31];
       
  3253       else
       
  3254         tab = &RvlcTcoefTab1[70];
       
  3255       break;
       
  3256       
       
  3257     case 0x74:
       
  3258       if (intra_luma)
       
  3259         tab = &RvlcTcoefTab0[7];
       
  3260       else
       
  3261         tab = &RvlcTcoefTab1[72];
       
  3262       break;
       
  3263       
       
  3264     case 0x75:
       
  3265       if (intra_luma)
       
  3266         tab = &RvlcTcoefTab0[8];
       
  3267       else
       
  3268         tab = &RvlcTcoefTab1[74];
       
  3269       break;
       
  3270       
       
  3271     case 0x78:
       
  3272       if (intra_luma)
       
  3273         tab = &RvlcTcoefTab0[104];
       
  3274       else
       
  3275         tab = &RvlcTcoefTab1[104];
       
  3276       break;
       
  3277       
       
  3278     case 0x79:
       
  3279       if (intra_luma)
       
  3280         tab = &RvlcTcoefTab0[134];
       
  3281       else
       
  3282         tab = &RvlcTcoefTab1[134];
       
  3283       break;
       
  3284       
       
  3285     case 0x7c:
       
  3286       if (intra_luma)
       
  3287         tab = &RvlcTcoefTab0[90];
       
  3288       else
       
  3289         tab = &RvlcTcoefTab1[6];
       
  3290       break;
       
  3291       
       
  3292     case 0x7d:
       
  3293       if (intra_luma)
       
  3294         tab = &RvlcTcoefTab0[67];
       
  3295       else
       
  3296         tab = &RvlcTcoefTab1[22];
       
  3297       break;
       
  3298       
       
  3299     case 0x82:
       
  3300       if (intra_luma)
       
  3301         tab = &RvlcTcoefTab0[136];
       
  3302       else
       
  3303         tab = &RvlcTcoefTab1[136];
       
  3304       break;
       
  3305       
       
  3306     case 0x83:
       
  3307       if (intra_luma)
       
  3308         tab = &RvlcTcoefTab0[138];
       
  3309       else
       
  3310         tab = &RvlcTcoefTab1[138];
       
  3311       break;
       
  3312       
       
  3313     case 0xbc:
       
  3314       if (intra_luma)
       
  3315         tab = &RvlcTcoefTab0[42];
       
  3316       else
       
  3317         tab = &RvlcTcoefTab1[31];
       
  3318       break;
       
  3319       
       
  3320     case 0xbd:
       
  3321       if (intra_luma)
       
  3322         tab = &RvlcTcoefTab0[53];
       
  3323       else
       
  3324         tab = &RvlcTcoefTab1[49];
       
  3325       break;
       
  3326       
       
  3327     case 0xdc:
       
  3328       if (intra_luma)
       
  3329         tab = &RvlcTcoefTab0[32];
       
  3330       else
       
  3331         tab = &RvlcTcoefTab1[76];
       
  3332       break;
       
  3333       
       
  3334     case 0xdd:
       
  3335       if (intra_luma)
       
  3336         tab = &RvlcTcoefTab0[9];
       
  3337       else
       
  3338         tab = &RvlcTcoefTab1[78];
       
  3339       break;
       
  3340       
       
  3341     case 0xec:
       
  3342       if (intra_luma)
       
  3343         tab = &RvlcTcoefTab0[10];
       
  3344       else
       
  3345         tab = &RvlcTcoefTab1[80];
       
  3346       break;
       
  3347       
       
  3348     case 0xed:
       
  3349       if (intra_luma)
       
  3350         tab = &RvlcTcoefTab0[109];
       
  3351       else
       
  3352         tab = &RvlcTcoefTab1[109];
       
  3353       break;
       
  3354       
       
  3355     case 0xf4:
       
  3356       if (intra_luma)
       
  3357         tab = &RvlcTcoefTab0[139];
       
  3358       else
       
  3359         tab = &RvlcTcoefTab1[139];
       
  3360       break;
       
  3361       
       
  3362     case 0xf5:
       
  3363       if (intra_luma)
       
  3364         tab = &RvlcTcoefTab0[140];
       
  3365       else
       
  3366         tab = &RvlcTcoefTab1[140];
       
  3367       break;
       
  3368       
       
  3369     case 0xf8:
       
  3370       if (intra_luma)
       
  3371         tab = &RvlcTcoefTab0[141];
       
  3372       else
       
  3373         tab = &RvlcTcoefTab1[141];
       
  3374       break;
       
  3375       
       
  3376     case 0xf9:
       
  3377       if (intra_luma)
       
  3378         tab = &RvlcTcoefTab0[142];
       
  3379       else
       
  3380         tab = &RvlcTcoefTab1[142];
       
  3381       break;
       
  3382       
       
  3383     case 0xfc:
       
  3384       if (intra_luma)
       
  3385         tab = &RvlcTcoefTab0[92];
       
  3386       else
       
  3387         tab = &RvlcTcoefTab1[7];
       
  3388       break;
       
  3389       
       
  3390     case 0xfd:
       
  3391       if (intra_luma)
       
  3392         tab = &RvlcTcoefTab0[94];
       
  3393       else
       
  3394         tab = &RvlcTcoefTab1[8];
       
  3395       break;
       
  3396       
       
  3397     case 0x102:
       
  3398       if (intra_luma)
       
  3399         tab = &RvlcTcoefTab0[143];
       
  3400       else
       
  3401         tab = &RvlcTcoefTab1[143];
       
  3402       break;
       
  3403       
       
  3404     case 0x103:
       
  3405       if (intra_luma)
       
  3406         tab = &RvlcTcoefTab0[144];
       
  3407       else
       
  3408         tab = &RvlcTcoefTab1[144];
       
  3409       break;
       
  3410       
       
  3411     case 0x17c:
       
  3412       if (intra_luma)
       
  3413         tab = &RvlcTcoefTab0[73];
       
  3414       else
       
  3415         tab = &RvlcTcoefTab1[23];
       
  3416       break;
       
  3417       
       
  3418     case 0x17d:
       
  3419       if (intra_luma)
       
  3420         tab = &RvlcTcoefTab0[78];
       
  3421       else
       
  3422         tab = &RvlcTcoefTab1[38];
       
  3423       break;
       
  3424       
       
  3425     case 0x1bc:
       
  3426       if (intra_luma)
       
  3427         tab = &RvlcTcoefTab0[83];
       
  3428       else
       
  3429         tab = &RvlcTcoefTab1[53];
       
  3430       break;
       
  3431       
       
  3432     case 0x1bd:
       
  3433       if (intra_luma)
       
  3434         tab = &RvlcTcoefTab0[62];
       
  3435       else
       
  3436         tab = &RvlcTcoefTab1[57];
       
  3437       break;
       
  3438       
       
  3439     case 0x1dc:
       
  3440       if (intra_luma)
       
  3441         tab = &RvlcTcoefTab0[43];
       
  3442       else
       
  3443         tab = &RvlcTcoefTab1[61];
       
  3444       break;
       
  3445       
       
  3446     case 0x1dd:
       
  3447       if (intra_luma)
       
  3448         tab = &RvlcTcoefTab0[33];
       
  3449       else
       
  3450         tab = &RvlcTcoefTab1[64];
       
  3451       break;
       
  3452       
       
  3453     case 0x1ec:
       
  3454       if (intra_luma)
       
  3455         tab = &RvlcTcoefTab0[11];
       
  3456       else
       
  3457         tab = &RvlcTcoefTab1[82];
       
  3458       break;
       
  3459       
       
  3460     case 0x1ed:
       
  3461       if (intra_luma)
       
  3462         tab = &RvlcTcoefTab0[12];
       
  3463       else
       
  3464         tab = &RvlcTcoefTab1[83];
       
  3465       break;
       
  3466       
       
  3467     case 0x1f4:
       
  3468       if (intra_luma)
       
  3469         tab = &RvlcTcoefTab0[13];
       
  3470       else
       
  3471         tab = &RvlcTcoefTab1[84];
       
  3472       break;
       
  3473       
       
  3474     case 0x1f5:
       
  3475       if (intra_luma)
       
  3476         tab = &RvlcTcoefTab0[145];
       
  3477       else
       
  3478         tab = &RvlcTcoefTab1[145];
       
  3479       break;
       
  3480       
       
  3481     case 0x1f8:
       
  3482       if (intra_luma)
       
  3483         tab = &RvlcTcoefTab0[146];
       
  3484       else
       
  3485         tab = &RvlcTcoefTab1[146];
       
  3486       break;
       
  3487       
       
  3488     case 0x1f9:
       
  3489       if (intra_luma)
       
  3490         tab = &RvlcTcoefTab0[147];
       
  3491       else
       
  3492         tab = &RvlcTcoefTab1[147];
       
  3493       break;
       
  3494       
       
  3495     case 0x1fc:
       
  3496       if (intra_luma)
       
  3497         tab = &RvlcTcoefTab0[96];
       
  3498       else
       
  3499         tab = &RvlcTcoefTab1[9];
       
  3500       break;
       
  3501       
       
  3502     case 0x1fd:
       
  3503       if (intra_luma)
       
  3504         tab = &RvlcTcoefTab0[87];
       
  3505       else
       
  3506         tab = &RvlcTcoefTab1[10];
       
  3507       break;
       
  3508       
       
  3509     case 0x202:
       
  3510       if (intra_luma)
       
  3511         tab = &RvlcTcoefTab0[148];
       
  3512       else
       
  3513         tab = &RvlcTcoefTab1[148];
       
  3514       break;
       
  3515       
       
  3516     case 0x203:
       
  3517       if (intra_luma)
       
  3518         tab = &RvlcTcoefTab0[149];
       
  3519       else
       
  3520         tab = &RvlcTcoefTab1[149];
       
  3521       break;
       
  3522       
       
  3523     case 0x2fc:
       
  3524       if (intra_luma)
       
  3525         tab = &RvlcTcoefTab0[68];
       
  3526       else
       
  3527         tab = &RvlcTcoefTab1[24];
       
  3528       break;
       
  3529       
       
  3530     case 0x2fd:
       
  3531       if (intra_luma)
       
  3532         tab = &RvlcTcoefTab0[74];
       
  3533       else
       
  3534         tab = &RvlcTcoefTab1[32];
       
  3535       break;
       
  3536       
       
  3537     case 0x37c:
       
  3538       if (intra_luma)
       
  3539         tab = &RvlcTcoefTab0[79];
       
  3540       else
       
  3541         tab = &RvlcTcoefTab1[45];
       
  3542       break;
       
  3543       
       
  3544     case 0x37d:
       
  3545       if (intra_luma)
       
  3546         tab = &RvlcTcoefTab0[54];
       
  3547       else
       
  3548         tab = &RvlcTcoefTab1[50];
       
  3549       break;
       
  3550       
       
  3551     case 0x3bc:
       
  3552       if (intra_luma)
       
  3553         tab = &RvlcTcoefTab0[44];
       
  3554       else
       
  3555         tab = &RvlcTcoefTab1[67];
       
  3556       break;
       
  3557       
       
  3558     case 0x3bd:
       
  3559       if (intra_luma)
       
  3560         tab = &RvlcTcoefTab0[45];
       
  3561       else
       
  3562         tab = &RvlcTcoefTab1[85];
       
  3563       break;
       
  3564       
       
  3565     case 0x3dc:
       
  3566       if (intra_luma)
       
  3567         tab = &RvlcTcoefTab0[34];
       
  3568       else
       
  3569         tab = &RvlcTcoefTab1[86];
       
  3570       break;
       
  3571       
       
  3572     case 0x3dd:
       
  3573       if (intra_luma)
       
  3574         tab = &RvlcTcoefTab0[35];
       
  3575       else
       
  3576         tab = &RvlcTcoefTab1[87];
       
  3577       break;
       
  3578       
       
  3579     case 0x3ec:
       
  3580       if (intra_luma)
       
  3581         tab = &RvlcTcoefTab0[14];
       
  3582       else
       
  3583         tab = &RvlcTcoefTab1[88];
       
  3584       break;
       
  3585       
       
  3586     case 0x3ed:
       
  3587       if (intra_luma)
       
  3588         tab = &RvlcTcoefTab0[15];
       
  3589       else
       
  3590         tab = &RvlcTcoefTab1[89];
       
  3591       break;
       
  3592       
       
  3593     case 0x3f4:
       
  3594       if (intra_luma)
       
  3595         tab = &RvlcTcoefTab0[16];
       
  3596       else
       
  3597         tab = &RvlcTcoefTab1[90];
       
  3598       break;
       
  3599       
       
  3600     case 0x3f5:
       
  3601       if (intra_luma)
       
  3602         tab = &RvlcTcoefTab0[105];
       
  3603       else
       
  3604         tab = &RvlcTcoefTab1[105];
       
  3605       break;
       
  3606       
       
  3607     case 0x3f8:
       
  3608       if (intra_luma)
       
  3609         tab = &RvlcTcoefTab0[114];
       
  3610       else
       
  3611         tab = &RvlcTcoefTab1[114];
       
  3612       break;
       
  3613       
       
  3614     case 0x3f9:
       
  3615       if (intra_luma)
       
  3616         tab = &RvlcTcoefTab0[150];
       
  3617       else
       
  3618         tab = &RvlcTcoefTab1[150];
       
  3619       break;
       
  3620       
       
  3621     case 0x3fc:
       
  3622       if (intra_luma)
       
  3623         tab = &RvlcTcoefTab0[91];
       
  3624       else
       
  3625         tab = &RvlcTcoefTab1[11];
       
  3626       break;
       
  3627       
       
  3628     case 0x3fd:
       
  3629       if (intra_luma)
       
  3630         tab = &RvlcTcoefTab0[63];
       
  3631       else
       
  3632         tab = &RvlcTcoefTab1[25];
       
  3633       break;
       
  3634       
       
  3635     case 0x402:
       
  3636       if (intra_luma)
       
  3637         tab = &RvlcTcoefTab0[151];
       
  3638       else
       
  3639         tab = &RvlcTcoefTab1[151];
       
  3640       break;
       
  3641       
       
  3642     case 0x403:
       
  3643       if (intra_luma)
       
  3644         tab = &RvlcTcoefTab0[152];
       
  3645       else
       
  3646         tab = &RvlcTcoefTab1[152];
       
  3647       break;
       
  3648       
       
  3649     case 0x5fc:
       
  3650       if (intra_luma)
       
  3651         tab = &RvlcTcoefTab0[69];
       
  3652       else
       
  3653         tab = &RvlcTcoefTab1[33];
       
  3654       break;
       
  3655       
       
  3656     case 0x5fd:
       
  3657       if (intra_luma)
       
  3658         tab = &RvlcTcoefTab0[75];
       
  3659       else
       
  3660         tab = &RvlcTcoefTab1[39];
       
  3661       break;
       
  3662       
       
  3663     case 0x6fc:
       
  3664       if (intra_luma)
       
  3665         tab = &RvlcTcoefTab0[55];
       
  3666       else
       
  3667         tab = &RvlcTcoefTab1[54];
       
  3668       break;
       
  3669       
       
  3670     case 0x6fd:
       
  3671       if (intra_luma)
       
  3672         tab = &RvlcTcoefTab0[64];
       
  3673       else
       
  3674         tab = &RvlcTcoefTab1[58];
       
  3675       break;
       
  3676       
       
  3677     case 0x77c:
       
  3678       if (intra_luma)
       
  3679         tab = &RvlcTcoefTab0[36];
       
  3680       else
       
  3681         tab = &RvlcTcoefTab1[69];
       
  3682       break;
       
  3683       
       
  3684     case 0x77d:
       
  3685       if (intra_luma)
       
  3686         tab = &RvlcTcoefTab0[17];
       
  3687       else
       
  3688         tab = &RvlcTcoefTab1[91];
       
  3689       break;
       
  3690       
       
  3691     case 0x7bc:
       
  3692       if (intra_luma)
       
  3693         tab = &RvlcTcoefTab0[18];
       
  3694       else
       
  3695         tab = &RvlcTcoefTab1[92];
       
  3696       break;
       
  3697       
       
  3698     case 0x7bd:
       
  3699       if (intra_luma)
       
  3700         tab = &RvlcTcoefTab0[21];
       
  3701       else
       
  3702         tab = &RvlcTcoefTab1[93];
       
  3703       break;
       
  3704       
       
  3705     case 0x7dc:
       
  3706       if (intra_luma)
       
  3707         tab = &RvlcTcoefTab0[110];
       
  3708       else
       
  3709         tab = &RvlcTcoefTab1[110];
       
  3710       break;
       
  3711       
       
  3712     case 0x7dd:
       
  3713       if (intra_luma)
       
  3714         tab = &RvlcTcoefTab0[117];
       
  3715       else
       
  3716         tab = &RvlcTcoefTab1[117];
       
  3717       break;
       
  3718       
       
  3719     case 0x7ec:
       
  3720       if (intra_luma)
       
  3721         tab = &RvlcTcoefTab0[119];
       
  3722       else
       
  3723         tab = &RvlcTcoefTab1[119];
       
  3724       break;
       
  3725       
       
  3726     case 0x7ed:
       
  3727       if (intra_luma)
       
  3728         tab = &RvlcTcoefTab0[153];
       
  3729       else
       
  3730         tab = &RvlcTcoefTab1[153];
       
  3731       break;
       
  3732       
       
  3733     case 0x7f4:
       
  3734       if (intra_luma)
       
  3735         tab = &RvlcTcoefTab0[154];
       
  3736       else
       
  3737         tab = &RvlcTcoefTab1[154];
       
  3738       break;
       
  3739       
       
  3740     case 0x7f5:
       
  3741       if (intra_luma)
       
  3742         tab = &RvlcTcoefTab0[155];
       
  3743       else
       
  3744         tab = &RvlcTcoefTab1[155];
       
  3745       break;
       
  3746       
       
  3747     case 0x7f8:
       
  3748       if (intra_luma)
       
  3749         tab = &RvlcTcoefTab0[156];
       
  3750       else
       
  3751         tab = &RvlcTcoefTab1[156];
       
  3752       break;
       
  3753       
       
  3754     case 0x7f9:
       
  3755       if (intra_luma)
       
  3756         tab = &RvlcTcoefTab0[157];
       
  3757       else
       
  3758         tab = &RvlcTcoefTab1[157];
       
  3759       break;
       
  3760       
       
  3761     case 0x7fc:
       
  3762       if (intra_luma)
       
  3763         tab = &RvlcTcoefTab0[97];
       
  3764       else
       
  3765         tab = &RvlcTcoefTab1[12];
       
  3766       break;
       
  3767       
       
  3768     case 0x7fd:
       
  3769       if (intra_luma)
       
  3770         tab = &RvlcTcoefTab0[98];
       
  3771       else
       
  3772         tab = &RvlcTcoefTab1[13];
       
  3773       break;
       
  3774       
       
  3775     case 0x802:
       
  3776       if (intra_luma)
       
  3777         tab = &RvlcTcoefTab0[158];
       
  3778       else
       
  3779         tab = &RvlcTcoefTab1[158];
       
  3780       break;
       
  3781       
       
  3782     case 0x803:
       
  3783       if (intra_luma)
       
  3784         tab = &RvlcTcoefTab0[159];
       
  3785       else
       
  3786         tab = &RvlcTcoefTab1[159];
       
  3787       break;
       
  3788       
       
  3789     case 0xbfc:
       
  3790       if (intra_luma)
       
  3791         tab = &RvlcTcoefTab0[93];
       
  3792       else
       
  3793         tab = &RvlcTcoefTab1[14];
       
  3794       break;
       
  3795       
       
  3796     case 0xbfd:
       
  3797       if (intra_luma)
       
  3798         tab = &RvlcTcoefTab0[84];
       
  3799       else
       
  3800         tab = &RvlcTcoefTab1[15];
       
  3801       break;
       
  3802       
       
  3803     case 0xdfc:
       
  3804       if (intra_luma)
       
  3805         tab = &RvlcTcoefTab0[88];
       
  3806       else
       
  3807         tab = &RvlcTcoefTab1[26];
       
  3808       break;
       
  3809       
       
  3810     case 0xdfd:
       
  3811       if (intra_luma)
       
  3812         tab = &RvlcTcoefTab0[80];
       
  3813       else
       
  3814         tab = &RvlcTcoefTab1[40];
       
  3815       break;
       
  3816       
       
  3817     case 0xefc:
       
  3818       if (intra_luma)
       
  3819         tab = &RvlcTcoefTab0[56];
       
  3820       else
       
  3821         tab = &RvlcTcoefTab1[46];
       
  3822       break;
       
  3823       
       
  3824     case 0xefd:
       
  3825       if (intra_luma)
       
  3826         tab = &RvlcTcoefTab0[46];
       
  3827       else
       
  3828         tab = &RvlcTcoefTab1[51];
       
  3829       break;
       
  3830       
       
  3831     case 0xf7c:
       
  3832       if (intra_luma)
       
  3833         tab = &RvlcTcoefTab0[47];
       
  3834       else
       
  3835         tab = &RvlcTcoefTab1[62];
       
  3836       break;
       
  3837       
       
  3838     case 0xf7d:
       
  3839       if (intra_luma)
       
  3840         tab = &RvlcTcoefTab0[48];
       
  3841       else
       
  3842         tab = &RvlcTcoefTab1[71];
       
  3843       break;
       
  3844       
       
  3845     case 0xfbc:
       
  3846       if (intra_luma)
       
  3847         tab = &RvlcTcoefTab0[37];
       
  3848       else
       
  3849         tab = &RvlcTcoefTab1[94];
       
  3850       break;
       
  3851       
       
  3852     case 0xfbd:
       
  3853       if (intra_luma)
       
  3854         tab = &RvlcTcoefTab0[19];
       
  3855       else
       
  3856         tab = &RvlcTcoefTab1[95];
       
  3857       break;
       
  3858       
       
  3859     case 0xfdc:
       
  3860       if (intra_luma)
       
  3861         tab = &RvlcTcoefTab0[20];
       
  3862       else
       
  3863         tab = &RvlcTcoefTab1[96];
       
  3864       break;
       
  3865       
       
  3866     case 0xfdd:
       
  3867       if (intra_luma)
       
  3868         tab = &RvlcTcoefTab0[22];
       
  3869       else
       
  3870         tab = &RvlcTcoefTab1[97];
       
  3871       break;
       
  3872       
       
  3873     case 0xfec:
       
  3874       if (intra_luma)
       
  3875         tab = &RvlcTcoefTab0[106];
       
  3876       else
       
  3877         tab = &RvlcTcoefTab1[106];
       
  3878       break;
       
  3879       
       
  3880     case 0xfed:
       
  3881       if (intra_luma)
       
  3882         tab = &RvlcTcoefTab0[121];
       
  3883       else
       
  3884         tab = &RvlcTcoefTab1[121];
       
  3885       break;
       
  3886       
       
  3887     case 0xff4:
       
  3888       if (intra_luma)
       
  3889         tab = &RvlcTcoefTab0[123];
       
  3890       else
       
  3891         tab = &RvlcTcoefTab1[123];
       
  3892       break;
       
  3893       
       
  3894     case 0xff5:
       
  3895       if (intra_luma)
       
  3896         tab = &RvlcTcoefTab0[125];
       
  3897       else
       
  3898         tab = &RvlcTcoefTab1[125];
       
  3899       break;
       
  3900       
       
  3901     case 0xff8:
       
  3902       if (intra_luma)
       
  3903         tab = &RvlcTcoefTab0[127];
       
  3904       else
       
  3905         tab = &RvlcTcoefTab1[127];
       
  3906       break;
       
  3907       
       
  3908     case 0xff9:
       
  3909       if (intra_luma)
       
  3910         tab = &RvlcTcoefTab0[129];
       
  3911       else
       
  3912         tab = &RvlcTcoefTab1[129];
       
  3913       break;
       
  3914       
       
  3915     case 0xffc:
       
  3916       if (intra_luma)
       
  3917         tab = &RvlcTcoefTab0[99];
       
  3918       else
       
  3919         tab = &RvlcTcoefTab1[16];
       
  3920       break;
       
  3921       
       
  3922     case 0xffd:
       
  3923       if (intra_luma)
       
  3924         tab = &RvlcTcoefTab0[100];
       
  3925       else
       
  3926         tab = &RvlcTcoefTab1[17];
       
  3927       break;
       
  3928       
       
  3929     case 0x1002:
       
  3930       if (intra_luma)
       
  3931         tab = &RvlcTcoefTab0[160];
       
  3932       else
       
  3933         tab = &RvlcTcoefTab1[160];
       
  3934       break;
       
  3935       
       
  3936     case 0x1003:
       
  3937       if (intra_luma)
       
  3938         tab = &RvlcTcoefTab0[161];
       
  3939       else
       
  3940         tab = &RvlcTcoefTab1[161];
       
  3941       break;
       
  3942       
       
  3943     case 0x17fc:
       
  3944       if (intra_luma)
       
  3945         tab = &RvlcTcoefTab0[101];
       
  3946       else
       
  3947         tab = &RvlcTcoefTab1[27];
       
  3948       break;
       
  3949       
       
  3950     case 0x17fd:
       
  3951       if (intra_luma)
       
  3952         tab = &RvlcTcoefTab0[85];
       
  3953       else
       
  3954         tab = &RvlcTcoefTab1[28];
       
  3955       break;
       
  3956       
       
  3957     case 0x1bfc:
       
  3958       if (intra_luma)
       
  3959         tab = &RvlcTcoefTab0[70];
       
  3960       else
       
  3961         tab = &RvlcTcoefTab1[34];
       
  3962       break;
       
  3963       
       
  3964     case 0x1bfd:
       
  3965       if (intra_luma)
       
  3966         tab = &RvlcTcoefTab0[65];
       
  3967       else
       
  3968         tab = &RvlcTcoefTab1[35];
       
  3969       break;
       
  3970       
       
  3971     case 0x1dfc:
       
  3972       if (intra_luma)
       
  3973         tab = &RvlcTcoefTab0[71];
       
  3974       else
       
  3975         tab = &RvlcTcoefTab1[41];
       
  3976       break;
       
  3977       
       
  3978     case 0x1dfd:
       
  3979       if (intra_luma)
       
  3980         tab = &RvlcTcoefTab0[57];
       
  3981       else
       
  3982         tab = &RvlcTcoefTab1[55];
       
  3983       break;
       
  3984       
       
  3985     case 0x1efc:
       
  3986       if (intra_luma)
       
  3987         tab = &RvlcTcoefTab0[58];
       
  3988       else
       
  3989         tab = &RvlcTcoefTab1[65];
       
  3990       break;
       
  3991       
       
  3992     case 0x1efd:
       
  3993       if (intra_luma)
       
  3994         tab = &RvlcTcoefTab0[49];
       
  3995       else
       
  3996         tab = &RvlcTcoefTab1[73];
       
  3997       break;
       
  3998       
       
  3999     case 0x1f7c:
       
  4000       if (intra_luma)
       
  4001         tab = &RvlcTcoefTab0[50];
       
  4002       else
       
  4003         tab = &RvlcTcoefTab1[75];
       
  4004       break;
       
  4005       
       
  4006     case 0x1f7d:
       
  4007       if (intra_luma)
       
  4008         tab = &RvlcTcoefTab0[38];
       
  4009       else
       
  4010         tab = &RvlcTcoefTab1[77];
       
  4011       break;
       
  4012       
       
  4013     case 0x1fbc:
       
  4014       if (intra_luma)
       
  4015         tab = &RvlcTcoefTab0[39];
       
  4016       else
       
  4017         tab = &RvlcTcoefTab1[79];
       
  4018       break;
       
  4019       
       
  4020     case 0x1fbd:
       
  4021       if (intra_luma)
       
  4022         tab = &RvlcTcoefTab0[23];
       
  4023       else
       
  4024         tab = &RvlcTcoefTab1[98];
       
  4025       break;
       
  4026       
       
  4027     case 0x1fdc:
       
  4028       if (intra_luma)
       
  4029         tab = &RvlcTcoefTab0[24];
       
  4030       else
       
  4031         tab = &RvlcTcoefTab1[99];
       
  4032       break;
       
  4033       
       
  4034     case 0x1fdd:
       
  4035       if (intra_luma)
       
  4036         tab = &RvlcTcoefTab0[25];
       
  4037       else
       
  4038         tab = &RvlcTcoefTab1[100];
       
  4039       break;
       
  4040       
       
  4041     case 0x1fec:
       
  4042       if (intra_luma)
       
  4043         tab = &RvlcTcoefTab0[107];
       
  4044       else
       
  4045         tab = &RvlcTcoefTab1[107];
       
  4046       break;
       
  4047       
       
  4048     case 0x1fed:
       
  4049       if (intra_luma)
       
  4050         tab = &RvlcTcoefTab0[111];
       
  4051       else
       
  4052         tab = &RvlcTcoefTab1[111];
       
  4053       break;
       
  4054       
       
  4055     case 0x1ff4:
       
  4056       if (intra_luma)
       
  4057         tab = &RvlcTcoefTab0[131];
       
  4058       else
       
  4059         tab = &RvlcTcoefTab1[131];
       
  4060       break;
       
  4061       
       
  4062     case 0x1ff5:
       
  4063       if (intra_luma)
       
  4064         tab = &RvlcTcoefTab0[133];
       
  4065       else
       
  4066         tab = &RvlcTcoefTab1[133];
       
  4067       break;
       
  4068       
       
  4069     case 0x1ff8:
       
  4070       if (intra_luma)
       
  4071         tab = &RvlcTcoefTab0[135];
       
  4072       else
       
  4073         tab = &RvlcTcoefTab1[135];
       
  4074       break;
       
  4075       
       
  4076     case 0x1ff9:
       
  4077       if (intra_luma)
       
  4078         tab = &RvlcTcoefTab0[162];
       
  4079       else
       
  4080         tab = &RvlcTcoefTab1[162];
       
  4081       break;
       
  4082       
       
  4083     case 0x1ffc:
       
  4084       if (intra_luma)
       
  4085         tab = &RvlcTcoefTab0[26];
       
  4086       else
       
  4087         tab = &RvlcTcoefTab1[18];
       
  4088       break;
       
  4089       
       
  4090     case 0x1ffd:
       
  4091       if (intra_luma)
       
  4092         tab = &RvlcTcoefTab0[59];
       
  4093       else
       
  4094         tab = &RvlcTcoefTab1[42];
       
  4095       break;
       
  4096       
       
  4097     case 0x2002:
       
  4098       if (intra_luma)
       
  4099         tab = &RvlcTcoefTab0[163];
       
  4100       else
       
  4101         tab = &RvlcTcoefTab1[163];
       
  4102       break;
       
  4103       
       
  4104     case 0x2003:
       
  4105       if (intra_luma)
       
  4106         tab = &RvlcTcoefTab0[164];
       
  4107       else
       
  4108         tab = &RvlcTcoefTab1[164];
       
  4109       break;
       
  4110       
       
  4111     case 0x2ffc:
       
  4112       if (intra_luma)
       
  4113         tab = &RvlcTcoefTab0[76];
       
  4114       else
       
  4115         tab = &RvlcTcoefTab1[47];
       
  4116       break;
       
  4117       
       
  4118     case 0x2ffd:
       
  4119       if (intra_luma)
       
  4120         tab = &RvlcTcoefTab0[81];
       
  4121       else
       
  4122         tab = &RvlcTcoefTab1[59];
       
  4123       break;
       
  4124       
       
  4125     case 0x37fc:
       
  4126       if (intra_luma)
       
  4127         tab = &RvlcTcoefTab0[89];
       
  4128       else
       
  4129         tab = &RvlcTcoefTab1[81];
       
  4130       break;
       
  4131       
       
  4132     case 0x37fd:
       
  4133       if (intra_luma)
       
  4134         tab = &RvlcTcoefTab0[95];
       
  4135       else
       
  4136         tab = &RvlcTcoefTab1[101];
       
  4137       break;
       
  4138       
       
  4139     case 0x3bfc:
       
  4140       if (intra_luma)
       
  4141         tab = &RvlcTcoefTab0[102];
       
  4142       else
       
  4143         tab = &RvlcTcoefTab1[102];
       
  4144       break;
       
  4145       
       
  4146     case 0x3bfd:
       
  4147       if (intra_luma)
       
  4148         tab = &RvlcTcoefTab0[112];
       
  4149       else
       
  4150         tab = &RvlcTcoefTab1[112];
       
  4151       break;
       
  4152       
       
  4153     case 0x3dfc:
       
  4154       if (intra_luma)
       
  4155         tab = &RvlcTcoefTab0[115];
       
  4156       else
       
  4157         tab = &RvlcTcoefTab1[115];
       
  4158       break;
       
  4159       
       
  4160     case 0x3dfd:
       
  4161       if (intra_luma)
       
  4162         tab = &RvlcTcoefTab0[137];
       
  4163       else
       
  4164         tab = &RvlcTcoefTab1[137];
       
  4165       break;
       
  4166       
       
  4167     case 0x3efc:
       
  4168       if (intra_luma)
       
  4169         tab = &RvlcTcoefTab0[165];
       
  4170       else
       
  4171         tab = &RvlcTcoefTab1[165];
       
  4172       break;
       
  4173       
       
  4174     case 0x3efd:
       
  4175       if (intra_luma)
       
  4176         tab = &RvlcTcoefTab0[166];
       
  4177       else
       
  4178         tab = &RvlcTcoefTab1[166];
       
  4179       break;
       
  4180       
       
  4181     case 0x3f7c:
       
  4182       if (intra_luma)
       
  4183         tab = &RvlcTcoefTab0[167];
       
  4184       else
       
  4185         tab = &RvlcTcoefTab1[167];
       
  4186       break;
       
  4187       
       
  4188     case 0x3f7d:
       
  4189       if (intra_luma)
       
  4190         tab = &RvlcTcoefTab0[168];
       
  4191       else
       
  4192         tab = &RvlcTcoefTab1[168];
       
  4193       break;
       
  4194 
       
  4195     default:
       
  4196       deb("ERROR - illegal RVLC TCOEF\n");
       
  4197       return VDX_OK_BUT_BIT_ERROR;
       
  4198     }
       
  4199 
       
  4200    *index = tab->val;
       
  4201    *length = tab->len;
       
  4202    
       
  4203    return VDX_OK;
       
  4204 }
       
  4205 
       
  4206 /*
       
  4207  * vdxGetRVLCDCTBlock
       
  4208  *    
       
  4209  *
       
  4210  * Parameters:
       
  4211  *    inBuffer                   pointer to bit buffer instance
       
  4212  *    startIndex                 the first index in block where to put data
       
  4213  *    block                      array for block (length 64)
       
  4214  *   fIntraBlock            indicates an intra "1" or inter "0" Block
       
  4215  *    bitErrorIndication         non-zero if a bit error has been detected
       
  4216  *                               within the bits accessed in this function,
       
  4217  *                               see biterr.h for possible values
       
  4218  *
       
  4219  * Function:
       
  4220  *    This function reads a block from bit buffer using Huffman codes listed
       
  4221  *    in RVLC TCOEF table. The place, where the block is read is given as a
       
  4222  *    pointer parameter.
       
  4223  *
       
  4224  * Returns:
       
  4225  *    VDX_OK                     the function was successful
       
  4226  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  4227  *                               occured
       
  4228  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  4229  *
       
  4230  *   
       
  4231  *
       
  4232  */
       
  4233 
       
  4234 int vdxGetRVLCDCTBlock(
       
  4235    bibBuffer_t *inBuffer, 
       
  4236    int startIndex,
       
  4237    int fIntraBlock,
       
  4238    int *block,
       
  4239    int *bitErrorIndication)
       
  4240 {
       
  4241     int numBitsGot,
       
  4242       retValue = VDX_OK,
       
  4243       index = startIndex,  /* index to zigzag table running from 1 to 63 */
       
  4244       tmpvar;
       
  4245    u_int32
       
  4246       bits,
       
  4247       RVLCIndex = 0,
       
  4248       RVLCLength = 0;
       
  4249    int16
       
  4250       bibError = 0;
       
  4251 
       
  4252    int run,    /* RUN code */
       
  4253       level;      /* LEVEL code */
       
  4254    u_int32 
       
  4255       last,       /* LAST code (see standard) */
       
  4256       sign;       /* sign for level */
       
  4257 
       
  4258    vdxAssert(inBuffer != NULL);
       
  4259    vdxAssert(startIndex == 0 || startIndex == 1);
       
  4260    vdxAssert(block != NULL);
       
  4261    vdxAssert(bitErrorIndication != NULL);
       
  4262 
       
  4263    do {
       
  4264 
       
  4265       /* Read next codeword */
       
  4266       bits = (int) bibShowBits(15, inBuffer, &numBitsGot, bitErrorIndication,
       
  4267          &bibError);
       
  4268       if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  4269          deb("vdxGetRVLCDCTBlock: bibShowBits returned not enough data --> "
       
  4270             "try to use the data available.\n");
       
  4271          bits <<= (15 - numBitsGot);
       
  4272          bibError = 0;
       
  4273       }
       
  4274       else if (bibError ) {
       
  4275          deb("vdxGetRVLCDCTBlock: ERROR - bibShowBits failed.\n");
       
  4276          goto exitFunction;
       
  4277       }
       
  4278 
       
  4279       /* Identifying the codeword in the read bits */
       
  4280       {
       
  4281          int count, len = 1;
       
  4282          u_int32 mask = 0x4000; /* mask  100000000000000   */
       
  4283          
       
  4284          if (bits & mask) {
       
  4285             count = 1;
       
  4286             for (len = 1; count > 0 && len < 15; len++) {
       
  4287                mask = mask >> 1;
       
  4288                if (bits & mask) 
       
  4289                   count--;
       
  4290             }
       
  4291          } else {
       
  4292             count = 2;
       
  4293             for (len = 1; count > 0 && len < 15; len++) {
       
  4294                mask = mask >> 1;
       
  4295                if (!(bits & mask))
       
  4296                   count--;
       
  4297             }
       
  4298          }
       
  4299 
       
  4300          if (len >= 15) {
       
  4301             deb("vdxGetRVLCDCTBlock:ERROR - illegal RVLC codeword.\n");
       
  4302             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4303             goto exitFunction;
       
  4304          }
       
  4305 
       
  4306          bits = bits & 0x7fff;
       
  4307          bits = bits >> (15 - (len + 1));
       
  4308       }
       
  4309 
       
  4310       /* Get the RVLC table Index and length belonging to the codeword */
       
  4311       if (vdxGetRVLCIndex(bits, &RVLCIndex, (int *) &RVLCLength, fIntraBlock, bitErrorIndication) != VDX_OK)
       
  4312          goto exitFunction;
       
  4313 
       
  4314       /* Flush the codeword from the buffer */
       
  4315       bibFlushBits(RVLCLength, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  4316       if (bibError)
       
  4317          goto exitFunction;
       
  4318 
       
  4319       if (RVLCIndex == 7167)     /* ESCAPE */
       
  4320       {  
       
  4321          /* Flush the rest of the ESCAPE code from the buffer */
       
  4322          bibFlushBits(1, inBuffer, &numBitsGot, bitErrorIndication, &bibError);
       
  4323          if (bibError)
       
  4324             goto exitFunction;
       
  4325 
       
  4326          /* LAST */
       
  4327          last = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4328             &bibError);
       
  4329          if (bibError)
       
  4330             goto exitFunction;
       
  4331          /* RUN */
       
  4332          run = (int) bibGetBits(6, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4333             &bibError);
       
  4334          if (bibError)
       
  4335             goto exitFunction;
       
  4336          /* MARKER BIT */
       
  4337          tmpvar = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError);
       
  4338          if (bibError)
       
  4339             goto exitFunction;
       
  4340          if (!tmpvar) {
       
  4341             deb("vdxGetRVLCDCTBlock:ERROR - Wrong marker bit.\n");
       
  4342             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4343             goto exitFunction;
       
  4344          }
       
  4345          /* LEVEL */
       
  4346          level = (int) bibGetBits(11, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4347             &bibError);
       
  4348          if (bibError)
       
  4349             goto exitFunction;
       
  4350          if (level == 0) {
       
  4351             deb("vdxGetRVLCDCTBlock:ERROR - Escape level invalid.\n");
       
  4352             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4353             goto exitFunction;
       
  4354          }
       
  4355          /* MARKER BIT */
       
  4356          tmpvar = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError);
       
  4357          if (bibError)
       
  4358             goto exitFunction;
       
  4359          if (!tmpvar) {
       
  4360             deb("vdxGetRVLCDCTBlock:ERROR - Wrong marker bit.\n");
       
  4361             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4362             goto exitFunction;
       
  4363          }
       
  4364          /* SIGN */
       
  4365          sign = bibGetBits(5, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4366             &bibError);
       
  4367          if (bibError)
       
  4368             goto exitFunction;
       
  4369 
       
  4370          if (sign == 1) {
       
  4371             level = -level;
       
  4372          } else if (sign != 0) {
       
  4373             deb("vdxGetRVLCDCTBlock:ERROR - illegal sign.\n");
       
  4374             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4375             goto exitFunction;
       
  4376          }
       
  4377 
       
  4378       } else {
       
  4379 
       
  4380          last = (RVLCIndex >> 16) & 1;
       
  4381          run = (RVLCIndex >> 8) & 255;
       
  4382          level = RVLCIndex & 255;
       
  4383 
       
  4384          sign = bibGetBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4385             &bibError);
       
  4386          if (bibError)
       
  4387             goto exitFunction;
       
  4388 
       
  4389          if (sign)
       
  4390             level = -level;
       
  4391       }
       
  4392 
       
  4393       /* If too many coefficients */
       
  4394       if (index + run > 63) {
       
  4395          deb("vdxGetRVLCDCTBlock:ERROR - too many TCOEFs.\n");
       
  4396          retValue = VDX_OK_BUT_BIT_ERROR;
       
  4397          goto exitFunction;
       
  4398       }
       
  4399 
       
  4400       /* Do run-length decoding */
       
  4401       while (run--)
       
  4402          block[index++] = 0;
       
  4403 
       
  4404       block[index++] = level;
       
  4405 
       
  4406    } while (!last);
       
  4407 
       
  4408    exitFunction:
       
  4409 
       
  4410    /* Set the rest of the coefficients to zero */
       
  4411    while (index <= 63) {
       
  4412       block[index++] = 0;
       
  4413    }
       
  4414 
       
  4415    if (!bibError)
       
  4416       return retValue;
       
  4417 
       
  4418    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  4419       return VDX_OK_BUT_BIT_ERROR;
       
  4420    }
       
  4421 
       
  4422    else
       
  4423       return VDX_ERR_BIB;
       
  4424 }
       
  4425 
       
  4426 /*
       
  4427  * vdxGetRVLCDCTBlockBackwards
       
  4428  *    
       
  4429  *
       
  4430  * Parameters:
       
  4431  *    inBuffer                   pointer to bit buffer instance
       
  4432  *    startIndex                 the first index in block where to put data
       
  4433  *    fIntraBlock                indicates an intra "1" or inter "0" Block
       
  4434  *    BitPosBeforeRVLC           bit position of inBuffer before the RVLC block,
       
  4435  *                               indicates the point to stop decoding backwards
       
  4436  *    block                      array for block (length 64)
       
  4437  *    bitErrorIndication         non-zero if a bit error has been detected
       
  4438  *                               within the bits accessed in this function,
       
  4439  *                               see biterr.h for possible values
       
  4440  *
       
  4441  * Function:
       
  4442  *   This function reads a block in backwards direction from the bit buffer
       
  4443  *   using Huffman codes listed in RVLC TCOEF table. The bit position of the 
       
  4444  *   buffer at return from the function is where the DCT data of the current 
       
  4445  *   block starts.
       
  4446  *
       
  4447  * Returns:
       
  4448  *    VDX_OK                     the function was successful
       
  4449  *    VDX_OK_BUT_BIT_ERROR       the function behaved normally, but a bit error
       
  4450  *                               occured
       
  4451  *    VDX_ERR_BIB                an error occured when accessing bit buffer
       
  4452  *
       
  4453  *   
       
  4454  *
       
  4455  */
       
  4456 
       
  4457 int vdxGetRVLCDCTBlockBackwards(
       
  4458    bibBuffer_t *inBuffer, 
       
  4459    int startIndex,
       
  4460    int fIntraBlock,
       
  4461 //   u_int32 BitPosBeforeRVLC,
       
  4462    int *block,
       
  4463    int *bitErrorIndication)
       
  4464 {
       
  4465     int numBitsGot,
       
  4466       retValue = VDX_OK,
       
  4467       index = 63; /* index to zigzag table running from 1 to 63 */
       
  4468    u_int32
       
  4469       bits,
       
  4470       RVLCIndex = 0,
       
  4471       RVLCLength = 0;
       
  4472    int16
       
  4473       bibError = 0;
       
  4474 
       
  4475    int run,    /* RUN code */
       
  4476       level;      /* LEVEL code */
       
  4477    u_int32 
       
  4478       last,       /* LAST code (see standard) */
       
  4479       sign,       /* sign for level */
       
  4480        escape;
       
  4481 
       
  4482    vdxAssert(inBuffer != NULL);
       
  4483    vdxAssert(startIndex == 0 || startIndex == 1);
       
  4484    vdxAssert(block != NULL);
       
  4485    vdxAssert(bitErrorIndication != NULL);
       
  4486 
       
  4487    do {
       
  4488       /* SIGN */
       
  4489       bibRewindBits(1, inBuffer, &bibError);
       
  4490       if (bibError)
       
  4491          goto exitFunction;
       
  4492       sign = bibShowBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4493          &bibError);
       
  4494       if (bibError) {
       
  4495          goto exitFunction;
       
  4496       }
       
  4497       
       
  4498       /* Read next codeword */
       
  4499       bibRewindBits(15, inBuffer, &bibError);
       
  4500       if (bibError)
       
  4501          goto exitFunction;
       
  4502       bits = (int) bibGetBits(15, inBuffer, &numBitsGot, bitErrorIndication,
       
  4503          &bibError);
       
  4504       if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  4505          deb("vdxGetRVLCDCTBlockBackwards: bibShowBits returned not enough data --> "
       
  4506             "try to use the data available.\n");
       
  4507          bits <<= (15 - numBitsGot);
       
  4508          bibError = 0;
       
  4509       }
       
  4510       else if (bibError) {
       
  4511          deb("vdxGetRVLCDCTBlockBackwards: ERROR - bibGetBits failed.\n");
       
  4512          goto exitFunction;
       
  4513       }
       
  4514 
       
  4515       /* Identifying the codeword in the read bits */
       
  4516       {
       
  4517          int count, len = 1;
       
  4518          u_int32 mask = 2; /* mask  000000000000010   */
       
  4519          
       
  4520          if (bits & mask) {
       
  4521             count = 1;
       
  4522             for (len = 1; count > 0 && len < 15; len++) {
       
  4523                mask = mask << 1;
       
  4524                if (bits & mask) 
       
  4525                   count--;
       
  4526             }
       
  4527          } else {
       
  4528             count = 2;
       
  4529             for (len = 1; count > 0 && len < 15; len++) {
       
  4530                mask = mask << 1;
       
  4531                if (!(bits & mask))
       
  4532                   count--;
       
  4533             }
       
  4534          }
       
  4535          
       
  4536          if (len >= 15) {
       
  4537             deb("vdxGetRVLCDCTBlockBackwards:ERROR - illegal RVLC codeword.\n");
       
  4538             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4539             goto exitFunction;
       
  4540          }
       
  4541 
       
  4542          bits = bits & (0x7fff >> (15 - (len + 1)));
       
  4543       }
       
  4544 
       
  4545       /* Get the RVLC table Index and length belonging to the codeword */
       
  4546       if (vdxGetRVLCIndex(bits, &RVLCIndex, (int *) &RVLCLength, fIntraBlock, bitErrorIndication) != VDX_OK)
       
  4547          goto exitFunction;
       
  4548 
       
  4549       /* Flush the codeword from the buffer */
       
  4550       bibRewindBits(RVLCLength, inBuffer, &bibError);
       
  4551       if (bibError)
       
  4552          goto exitFunction;
       
  4553 
       
  4554       if (RVLCIndex == 7167)     /* ESCAPE */
       
  4555       {  
       
  4556          /* MARKER BIT */
       
  4557          bibRewindBits(1, inBuffer, &bibError);
       
  4558          if (bibError)
       
  4559             goto exitFunction;
       
  4560          if(!bibShowBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError) 
       
  4561             || bibError ) {
       
  4562             deb("vdxGetRVLCDCTBlockBackwards:ERROR - Wrong marker bit.\n");
       
  4563             if ( !bibError )
       
  4564                retValue = VDX_OK_BUT_BIT_ERROR;
       
  4565             goto exitFunction;
       
  4566          }
       
  4567          /* LEVEL */
       
  4568          bibRewindBits(11, inBuffer, &bibError);
       
  4569          if (bibError)
       
  4570             goto exitFunction;
       
  4571          level = (int) bibShowBits(11, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4572             &bibError);
       
  4573          if (level == 0 || bibError ) {
       
  4574             if (!bibError) {
       
  4575                deb("vdxGetRVLCDCTBlockBackwards:ERROR - Invalid Level.\n");
       
  4576                retValue = VDX_OK_BUT_BIT_ERROR;
       
  4577             }
       
  4578             goto exitFunction;
       
  4579          }
       
  4580          /* MARKER BIT */
       
  4581          bibRewindBits(1, inBuffer, &bibError);
       
  4582          if (bibError)
       
  4583             goto exitFunction;
       
  4584          if(!bibShowBits(1, inBuffer, &numBitsGot, bitErrorIndication,&bibError) 
       
  4585             || bibError ) {
       
  4586             if ( !bibError ) {
       
  4587                deb("vdxGetRVLCDCTBlockBackwards:ERROR - Wrong marker bit.\n");
       
  4588                retValue = VDX_OK_BUT_BIT_ERROR;
       
  4589             }
       
  4590             goto exitFunction;
       
  4591          }
       
  4592          /* RUN */
       
  4593          bibRewindBits(6, inBuffer, &bibError);
       
  4594          if (bibError)
       
  4595             goto exitFunction;
       
  4596          run = (int) bibShowBits(6, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4597             &bibError);
       
  4598          if (bibError) {
       
  4599             goto exitFunction;
       
  4600          }
       
  4601          /* LAST */
       
  4602          bibRewindBits(1, inBuffer, &bibError);
       
  4603          if (bibError)
       
  4604             goto exitFunction;
       
  4605          last = bibShowBits(1, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4606             &bibError);
       
  4607          if (bibError) {
       
  4608             goto exitFunction;
       
  4609          }
       
  4610 
       
  4611          /* Get the first ESCAPE code from the buffer */
       
  4612          bibRewindBits(5, inBuffer, &bibError);
       
  4613          if (bibError)
       
  4614             goto exitFunction;
       
  4615          escape = bibShowBits(5, inBuffer, &numBitsGot, bitErrorIndication, 
       
  4616             &bibError);
       
  4617          if (bibError) {
       
  4618             goto exitFunction;
       
  4619          }
       
  4620 
       
  4621          if (escape != 1) {
       
  4622             deb("vdxGetRVLCDCTBlockBackwards:ERROR - illegal escape code.\n");
       
  4623             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4624             goto exitFunction;
       
  4625          }
       
  4626 
       
  4627          RVLCLength += 25;
       
  4628 
       
  4629       } else {
       
  4630 
       
  4631          last = (RVLCIndex >> 16) & 1;
       
  4632          run = (RVLCIndex >> 8) & 255;
       
  4633          level = RVLCIndex & 255;
       
  4634       }
       
  4635 
       
  4636       if (sign)
       
  4637          level = -level;
       
  4638 
       
  4639       if (index == 63) {
       
  4640          if (!last) {
       
  4641             deb("vdxGetRVLCDCTBlockBackwards:ERROR - last TCOEFF problem.\n");
       
  4642             retValue = VDX_OK_BUT_BIT_ERROR;
       
  4643             goto exitFunction;
       
  4644          } else 
       
  4645             last = 0;
       
  4646       }
       
  4647       
       
  4648       if (last) {
       
  4649          bibFlushBits((RVLCLength + 1), inBuffer, &numBitsGot, bitErrorIndication, 
       
  4650             &bibError);
       
  4651          if (bibError)
       
  4652             goto exitFunction;
       
  4653 
       
  4654       } else if (index - run < startIndex) {
       
  4655          deb("vdxGetRVLCDCTBlockBackwards:ERROR - too many TCOEFFs.\n");
       
  4656          retValue = VDX_OK_BUT_BIT_ERROR;
       
  4657          goto exitFunction;
       
  4658 
       
  4659       } else {
       
  4660          /* Do run-length decoding. Since we are decoding backwards, level has to be inserted first */
       
  4661          block[index--] = level;
       
  4662 
       
  4663          while (run--)
       
  4664             block[index--] = 0;
       
  4665          
       
  4666       }
       
  4667 
       
  4668    } while (!last); 
       
  4669 
       
  4670    exitFunction:
       
  4671 
       
  4672    {
       
  4673       int i;   
       
  4674       for(i=startIndex,index++; i<=63; i++,index++)
       
  4675          block[i]= (index <= 63) ? block[index] : 0;
       
  4676    }
       
  4677 
       
  4678 
       
  4679    if (!bibError)
       
  4680       return retValue;
       
  4681    else if (bibError == ERR_BIB_NOT_ENOUGH_DATA) {
       
  4682       return VDX_OK_BUT_BIT_ERROR;
       
  4683    } else
       
  4684       return VDX_ERR_BIB;
       
  4685 }
       
  4686 
       
  4687 // End of File