[Speex-dev] using unsigned short instead of short for input frame

Steve Checkoway s at pahtak.org
Sat Mar 10 12:06:13 PST 2012


On Mar 9, 2012, at 8:36 AM, Mashal al-shboul wrote:

> i found casting from unsigned short to short gives different value: unsigned short FFFF=65535 where short FFFF= -1.

Right.


> however, can i configure speex codec (specifically speex_encode_int function) to take input of type unsigned short instead of short so that i avoid casting errors ?

I don't know about that, but it should be easy to convert your samples. You want to convert from the range [0, 2^16 - 1] to [-2^15, 2^15 - 1]. Thus, all you have to do is subtract 2^15 = 0x8000 from each of them:

steve$ cat a.c
#include <stdio.h>
#include <inttypes.h>

#define convert(x) (int16_t)((x) - 0x8000)

int main()
{
        uint16_t max = 0xffff;
        uint16_t mid = 0x8000;
        uint16_t min = 0x0000;

        printf("%hx -> %hd\n", max, convert(max));
        printf("%hx -> %hd\n", mid, convert(mid));
        printf("%hx -> %hd\n", min, convert(min));
        return 0;
}
steve$ gcc -Wall a.c
steve$ ./a.out
ffff -> 32767
8000 -> 0
0 -> -32768

As you can see, the maximum value 0xffff is indeed converted to 2^15 - 1, the mid point 0x8000 is converted to 0, and the minimum value 0 is converted to -2^15.

-- 
Stephen Checkoway







More information about the Speex-dev mailing list