[FFmpeg-devel] [PATCH] Fast half-float to float conversion
Jimmy Christensen
jimmy
Wed Jul 1 13:53:53 CEST 2009
On 2009-07-01 13:40, Reimar D?ffinger wrote:
> On Wed, Jul 01, 2009 at 01:21:57PM +0200, Jimmy Christensen wrote:
>> On 2009-07-01 13:15, Reimar D?ffinger wrote:
>>> On Wed, Jul 01, 2009 at 12:52:08PM +0200, Jimmy Christensen wrote:
>>>> I modified for the purpose I need it for. Converting from half-float to
>>>> unsigned int.
>>>>
>>>> uint16_t av_halflt2uint(uint16_t v){
>>>> uint16_t nosign = v+v;
>>>> if (v>>15)
>>>> return 0; // negatives are not interesting so clamp it to 0
>>>> if (nosign>= 0xfc00)
>>>> return 65535; // Anything above 1 should be clamped to 65535
>>>> if (nosign< 0x0200)
>>>> return ldexp((v&0x3ff), 1-25)*65535; // denormal or 0
>>>> return ldexp((v&0x3ff) + (1<<10), (v>>10&0x1f)-25)*65535;
>>>> }
>>>
>>> Btw. a probably much faster but +-1 inaccurate version is:
>>> int exp = v>> 10;
>>> if ((v& 0x8000) || !exp)
>>> return 0;
>>> if (exp>= 15)
>>> return 0xffff;
>>> v<<= 6;
>>> return (v+(1<<16))>> (15-exp);
>>
>> Thanks. Alot faster than the table based. Gives 66fps.
>
> That's why I said that you have to optimize when you know the code and
> what you need, i.e. after everything else is done.
> Obviously if you go for speed the function absolutely should be either
> in the same file or in a header as "static inline" function.
>
The decoder still have issues with big endian systems. I'm not really
sure which way to go for reading a 32bit float. Started using AV_RL16
and av_int2flt which is really slow, it also uses the ldexp method. I
suppose a similar approach can be used for 32bit floats aswell? Will
post the OpenEXR decoder using the av_int2flt method and it can be
replaced with something faster later.
>> I really appreciate this. I can live with the inaccuracy since it's
>> rounded off to integer anyway.
>
> Um, the +-1 error is in the generated value. E.g. the 16-bit value "1"
> can never appear.
> I also just think that the error is +-1 at most, I haven't tested it.
>
That was also what I meant. After converting from half-float to 16-bit
integer and eg. back to half-float it would be hard to get the same
output as input.
>> Will submit the decoder soon.
>
> It would be possible to use the "bitexact" flag to switch to a more
> accurate version, though this may be a bit of a misuse of "bitexact".
--
Best Regards
Jimmy Christensen
Developer
Ghost A/S
More information about the ffmpeg-devel
mailing list