Skip to content
Snippets Groups Projects
Commit 41d62ad1 authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #9979: ulaw: normalize types

parent ca4fb50b
No related branches found
No related tags found
No related merge requests found
...@@ -44,73 +44,71 @@ class Ulaw : public sfl::AudioCodec { ...@@ -44,73 +44,71 @@ class Ulaw : public sfl::AudioCodec {
hasDynamicPayload_ = false; hasDynamicPayload_ = false;
} }
virtual int decode(short *dst, unsigned char *src, size_t buf_size) { virtual int decode(SFLDataFormat *dst, unsigned char *src, size_t buf_size) {
assert(buf_size == frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat)); assert(buf_size == frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat));
unsigned char* end = src+buf_size; for (unsigned char* end = src + buf_size; src < end; ++src, ++dst)
*dst = ULawDecode(*src);
while (src<end)
*dst++ = ULawDecode(*src++);
return frameSize_; return frameSize_;
} }
virtual int encode(unsigned char *dst, short *src, size_t buf_size) { virtual int encode(unsigned char *dst, SFLDataFormat *src, size_t buf_size) {
assert(buf_size >= frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat)); assert(buf_size >= frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat));
uint8* end = dst + frameSize_; for (uint8* end = dst + frameSize_; dst < end; ++src, ++dst)
*dst = ULawEncode(*src);
while (dst<end)
*dst++ = ULawEncode(*src++);
return frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat);; return frameSize_ / 2 /* compression factor = 2:1 */ * sizeof(SFLDataFormat);;
} }
int ULawDecode(uint8 ulaw) { SFLDataFormat ULawDecode(uint8 ulaw)
{
ulaw ^= 0xff; // u-law has all bits inverted for transmission ulaw ^= 0xff; // u-law has all bits inverted for transmission
int linear = ulaw&0x0f; int linear = ulaw & 0x0f;
linear <<= 3; linear <<= 3;
linear |= 0x84; // Set MSB (0x80) and a 'half' bit (0x04) to place PCM value in middle of range linear |= 0x84; // Set MSB (0x80) and a 'half' bit (0x04) to place PCM value in middle of range
uint shift = ulaw>>4; uint shift = ulaw >> 4;
shift &= 7; shift &= 7;
linear <<= shift; linear <<= shift;
linear -= 0x84; // Subract uLaw bias linear -= 0x84; // Subract uLaw bias
if (ulaw&0x80) if (ulaw & 0x80)
return -linear; return -linear;
else else
return linear; return linear;
} }
uint8 ULawEncode(int16 pcm16) { uint8 ULawEncode(SFLDataFormat pcm16)
{
int p = pcm16; int p = pcm16;
uint u; // u-law value we are forming uint u; // u-law value we are forming
if (p<0) { if (p < 0) {
p = ~p; p = ~p;
u = 0x80^0x10^0xff; // Sign bit = 1 (^0x10 because this will get inverted later) ^0xff ^0xff to invert final u-Law code u = 0x80 ^ 0x10 ^ 0xff; // Sign bit = 1 (^0x10 because this will get inverted later) ^0xff ^0xff to invert final u-Law code
} else { } else {
u = 0x00^0x10^0xff; // Sign bit = 0 (-0x10 because this amount extra will get added later) ^0xff to invert final u-Law code u = 0x00 ^ 0x10 ^ 0xff; // Sign bit = 0 (-0x10 because this amount extra will get added later) ^0xff to invert final u-Law code
} }
p += 0x84; // Add uLaw bias p += 0x84; // Add uLaw bias
if (p>0x7f00) if (p > 0x7f00)
p = 0x7f00; // Clip to 15 bits p = 0x7f00; // Clip to 15 bits
// Calculate segment and interval numbers // Calculate segment and interval numbers
p >>= 3; // Shift down to 13bit p >>= 3; // Shift down to 13bit
if (p>=0x100) { if (p >= 0x100) {
p >>= 4; p >>= 4;
u ^= 0x40; u ^= 0x40;
} }
if (p>=0x40) { if (p >= 0x40) {
p >>= 2; p >>= 2;
u ^= 0x20; u ^= 0x20;
} }
if (p>=0x20) { if (p >= 0x20) {
p >>= 1; p >>= 1;
u ^= 0x10; u ^= 0x10;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment