public static int build_table(VLC vlc,int table_nb_bits,int nb_codes,VLCcode[] codes_base,int codes_offset,int flags){
  int table_size, table_index, index, code_prefix, symbol, subtable_bits;
  int i, k, n, nb, inc;
  long j;
  long code;
  short[][] table_base;
  int table_offset;
  table_size=1 << table_nb_bits;
  table_index=alloc_table(vlc,table_size,flags & INIT_VLC_USE_NEW_STATIC);
  if (table_index < 0)   return -1;
  table_base=vlc.table_base;
  table_offset=vlc.table_offset + table_index;
  for (i=0; i < table_size; i++) {
    table_base[table_offset + i][1]=0;
    table_base[table_offset + i][0]=-1;
  }
  for (i=0; i < nb_codes; i++) {
    n=codes_base[codes_offset + i].bits;
    code=codes_base[codes_offset + i].code;
    symbol=codes_base[codes_offset + i].symbol;
    if (n <= table_nb_bits) {
      long tmp=code & 0xffffffffL;
      j=(int)(tmp >> (32 - table_nb_bits));
      nb=1 << (table_nb_bits - n);
      inc=1;
      if ((flags & INIT_VLC_LE) != 0) {
        j=bitswap_32(code);
        inc=1 << n;
      }
      for (k=0; k < nb; k++) {
        if (table_base[(int)(table_offset + j)][1] != 0) {
          return -1;
        }
        table_base[(int)(table_offset + j)][1]=(short)n;
        table_base[(int)(table_offset + j)][0]=(short)symbol;
        j+=inc;
      }
    }
 else {
      n-=table_nb_bits;
      long tmp=code & 0xffffffffL;
      code_prefix=(int)(tmp >> (32 - table_nb_bits));
      subtable_bits=n;
      codes_base[codes_offset + i].bits=n;
      codes_base[codes_offset + i].code=code << table_nb_bits;
      for (k=i + 1; k < nb_codes; k++) {
        n=codes_base[codes_offset + k].bits - table_nb_bits;
        if (n <= 0)         break;
        code=codes_base[codes_offset + k].code;
        if (code >> (32 - table_nb_bits) != code_prefix)         break;
        codes_base[codes_offset + k].bits=n;
        codes_base[codes_offset + k].code=code << table_nb_bits;
        subtable_bits=Math.max(subtable_bits,n);
      }
      subtable_bits=Math.min(subtable_bits,table_nb_bits);
      j=((flags & INIT_VLC_LE) != 0) ? (bitswap_32(code_prefix) >> (32 - table_nb_bits)) : code_prefix;
      table_base[(int)(table_offset + j)][1]=(short)-subtable_bits;
      index=build_table(vlc,subtable_bits,k - i,codes_base,codes_offset + i,flags);
      if (index < 0)       return -1;
      table_base=vlc.table_base;
      table_offset=vlc.table_offset + table_index;
      table_base[(int)(table_offset + j)][0]=(short)index;
      i=k - 1;
    }
  }
  return table_index;
}

public static int build_table ( VLC vlc , int table_nb_bits , int nb_codes , VLCcode [ ] codes_base , int codes_offset , int flags ) { int table_size , table_index , index , code_prefix , symbol , subtable_bits ; int i , k , n , nb , inc ; long j ; long code ; short [ ] [ ] table_base ; int table_offset ; table_size = 1 << table_nb_bits ; table_index = alloc_table ( vlc , table_size , flags & INIT_VLC_USE_NEW_STATIC ) ; if ( table_index < 0 ) return - 1 ; table_base = vlc . table_base ; table_offset = vlc . table_offset + table_index ; for ( i = 0 ; i < table_size ; i ++ ) { table_base [ table_offset + i ] [ 1 ] = 0 ; table_base [ table_offset + i ] [ 0 ] = - 1 ; } for ( i = 0 ; i < nb_codes ; i ++ ) { n = codes_base [ codes_offset + i ] . bits ; code = codes_base [ codes_offset + i ] . code ; symbol = codes_base [ codes_offset + i ] . symbol ; if ( n <= table_nb_bits ) { long tmp = code & 0xffffffffL ; j = ( int ) ( tmp > > ( 32 - table_nb_bits ) ) ; nb = 1 << ( table_nb_bits - n ) ; inc = 1 ; if ( ( flags & INIT_VLC_LE ) != 0 ) { j = bitswap_32 ( code ) ; inc = 1 << n ; } for ( k = 0 ; k < nb ; k ++ ) { if ( table_base [ ( int ) ( table_offset + j ) ] [ 1 ] != 0 ) { return - 1 ; } table_base [ ( int ) ( table_offset + j ) ] [ 1 ] = ( short ) n ; table_base [ ( int ) ( table_offset + j ) ] [ 0 ] = ( short ) symbol ; j += inc ; } } else { n -= table_nb_bits ; long tmp = code & 0xffffffffL ; code_prefix = ( int ) ( tmp > > ( 32 - table_nb_bits ) ) ; subtable_bits = n ; codes_base [ codes_offset + i ] . bits = n ; codes_base [ codes_offset + i ] . code = code << table_nb_bits ; for ( k = i + 1 ; k < nb_codes ; k ++ ) { n = codes_base [ codes_offset + k ] . bits - table_nb_bits ; if ( n <= 0 ) break ; code = codes_base [ codes_offset + k ] . code ; if ( code > > ( 32 - table_nb_bits ) != code_prefix ) break ; codes_base [ codes_offset + k ] . bits = n ; codes_base [ codes_offset + k ] . code = code << table_nb_bits ; subtable_bits = Math . max ( subtable_bits , n ) ; } subtable_bits = Math . min ( subtable_bits , table_nb_bits ) ; j = ( ( flags & INIT_VLC_LE ) != 0 ) ? ( bitswap_32 ( code_prefix ) > > ( 32 - table_nb_bits ) ) : code_prefix ; table_base [ ( int ) ( table_offset + j ) ] [ 1 ] = ( short ) - subtable_bits ; index = build_table ( vlc , subtable_bits , k - i , codes_base , codes_offset + i , flags ) ; if ( index < 0 ) return - 1 ; table_base = vlc . table_base ; table_offset = vlc . table_offset + table_index ; table_base [ ( int ) ( table_offset + j ) ] [ 0 ] = ( short ) index ; i = k - 1 ; } } return table_index ; }
build vlc decoding tables suitable for use with get_vlc ( ) .
