Skip to content

Commit

Permalink
roc-streaminggh-608: Fix bug in PCM mapper for non-byte aligned formats
Browse files Browse the repository at this point in the history
Packer/unpacker for non-byte aligned formats (e.g. 20-bit ints)
had a bug due to a missing type cast.
  • Loading branch information
gavv committed Jul 6, 2024
1 parent 325b7df commit a2873e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/internal_modules/roc_audio/pcm_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2382,7 +2382,7 @@ pcm_unaligned_write(uint8_t* buffer, size_t& bit_offset, size_t bit_length, uint
buffer[byte_index] = 0;
}

buffer[byte_index] |= uint8_t(arg << (8 - bit_length) >> bit_index);
buffer[byte_index] |= uint8_t(uint8_t(arg << (8 - bit_length)) >> bit_index);

if (bit_index + bit_length > 8) {
buffer[byte_index + 1] = uint8_t(arg << bit_index);
Expand All @@ -2397,10 +2397,10 @@ pcm_unaligned_read(const uint8_t* buffer, size_t& bit_offset, size_t bit_length)
size_t byte_index = (bit_offset >> 3);
size_t bit_index = (bit_offset & 0x7u);

uint8_t ret = uint8_t(buffer[byte_index] << bit_index >> (8 - bit_length));
uint8_t ret = uint8_t(uint8_t(buffer[byte_index] << bit_index) >> (8 - bit_length));

if (bit_index + bit_length > 8) {
ret |= uint8_t(buffer[byte_index + 1] >> (8 - bit_index) >> (8 - bit_length));
ret |= uint8_t(buffer[byte_index + 1] >> ((8 - bit_index) + (8 - bit_length)));
}

bit_offset += bit_length;
Expand Down
6 changes: 3 additions & 3 deletions src/internal_modules/roc_audio/pcm_format_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def nth_chars(codes, prefix=()):
buffer[byte_index] = 0;
}
buffer[byte_index] |= uint8_t(arg << (8 - bit_length) >> bit_index);
buffer[byte_index] |= uint8_t(uint8_t(arg << (8 - bit_length)) >> bit_index);
if (bit_index + bit_length > 8) {
buffer[byte_index + 1] = uint8_t(arg << bit_index);
Expand All @@ -754,10 +754,10 @@ def nth_chars(codes, prefix=()):
size_t byte_index = (bit_offset >> 3);
size_t bit_index = (bit_offset & 0x7u);
uint8_t ret = uint8_t(buffer[byte_index] << bit_index >> (8 - bit_length));
uint8_t ret = uint8_t(uint8_t(buffer[byte_index] << bit_index) >> (8 - bit_length));
if (bit_index + bit_length > 8) {
ret |= uint8_t(buffer[byte_index + 1] >> (8 - bit_index) >> (8 - bit_length));
ret |= uint8_t(buffer[byte_index + 1] >> ((8 - bit_index) + (8 - bit_length)));
}
bit_offset += bit_length;
Expand Down

0 comments on commit a2873e6

Please sign in to comment.