[FFmpeg-devel] [PATCH] Fast half-float to float conversion

Reimar Döffinger Reimar.Doeffinger
Tue Jun 30 11:18:47 CEST 2009


On Tue, Jun 30, 2009 at 07:33:43AM +0200, Jimmy Christensen wrote:
> I'm almost done with my OpenEXR decoder, but since it can work in a 
> half-float format I wanted a fast half-float to float conversion. I 
> found one in the FOX toolkit which uses a table lookup. I didn't want 
> the table lookup in the OpenEXR decoder only, since a few other formats 
> can use half-floats aswell. So instead I submit the half-float to float 
> first as a libavutil header.

Shared tables certainly don't belong into headers, either it wastes
space or in this case it just won't compile when the header is used more
than once.

> +/*
> + * This is for fast half float to float conversion. This is derivative work from the
> + * FOX Toolkit authored by Jeroen van der Zijp <jeroen at fox-toolkit.org>. It is based on a
> + * paper also written by Jeroen van der Zijp, which can be found here :
> + * http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf
> + */
> +
> +#ifndef AVUTIL_HALF_H
> +#define AVUTIL_HALF_H
> +
> +// Half to float mantissa table
> +const unsigned int half_float_mantissa[2048] = {

With a 8kB table I very much doubt this is fast, at least not on any
system with a small cache.

> +float half2float(unsigned int v) {
> +    union {
> +        unsigned int u;
> +        float f;
> +    } r;
> +    r.u = half_float_mantissa[half_float_switch[v>>10]+(v&0x3ff)]+half_float_exponent[v>>10];

This definitely would have to be documented. I also suspect it doesn't
handle denormals correctly at all.
Of course hard to tell without a specification for that "half float"
format.
In addition this code will crash if v is too large, or did you mean v to
be uint16_t?
Lastly it seems to me that this code would belong into
libavutil/intfloat_readwrite.*



More information about the ffmpeg-devel mailing list