One consideration is the relative densities of hydrocarbon fuels compared to hydrogen. If we assume that the fuels are completely vapourised in the combustion chamber then the mass of fuel depends on the volume, temperature and pressure. So energy density per kg doesn't necessarily tell you much about how many molecules of fuel you can get in one charge or indeed the chemical energy available per molecule.
There are also issues in IC engines relating to the combustion properties of hydrogen. One particular problem is that hydrogen will detonate at a broad range of air/fuel ratios which has knock on effects for ignition timing and compression ratios which in turn affect how well the chemical energy available from combustion can be converted into useful mechanical power. Premature detonation under compression (especially in hot cylinders) is a particular problem with hydrogen.
Hydrogen is often associated with low compression ratio engines like Wankel (and other rotary) engines for exactly this reason.
Hydrogen also burns hot compared to other fuel gases, so even when overall energies are comparable hydrogen engines can produce localised hot spots in the combustion chamber.
Gasoline is a mixture of fairly short chain hydrocarbons which can be approximated to C8 but the specific mixture of different molecules has a significant effect on its combustion behavior and there is quite a lot of scope for adjusting a particular fuel formulation to get the best performance. This is much more difficult with hydrogen.