Skip to content

Commit 766773c

Browse files
author
Siebren Weertman
committed
improve bitstream read efficiency
Signed-off-by: Siebren Weertman <siebren.weertman@heliox-energy.com>
1 parent e2071da commit 766773c

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/input/code_templates/c/static_code/exi_bitstream.c.jinja

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ int exi_bitstream_write_bits(exi_bitstream_t* stream, size_t bit_count, uint32_t
172172
}
173173

174174
const size_t remaining_bits = 8 - stream->bit_count;
175-
if (remaining_bits > bit_count) {
175+
if (remaining_bits >= bit_count) {
176176
stream->data[stream->byte_pos] |= (uint8_t)((value << (remaining_bits - bit_count)));
177177
stream->bit_count += bit_count;
178178
if (stream->bit_count == 8) {
@@ -228,16 +228,33 @@ int exi_bitstream_read_bits(exi_bitstream_t* stream, size_t bit_count, uint32_t*
228228

229229
int error = EXI_ERROR__NO_ERROR;
230230

231-
for (size_t n = 0; n < bit_count; n++)
232-
{
233-
uint8_t bit;
234-
error = exi_bitstream_read_bit(stream, &bit);
235-
if (error != EXI_ERROR__NO_ERROR)
236-
{
237-
break;
231+
error = exi_bitstream_has_overflow(stream);
232+
if (error != 0) {
233+
return error;
234+
}
235+
236+
const size_t remaining_bits = 8 - stream->bit_count;
237+
if (remaining_bits >= bit_count) {
238+
*value = (uint32_t)((stream->data[stream->byte_pos] >> (remaining_bits - bit_count)) & ((1 << bit_count) - 1));
239+
stream->bit_count += bit_count;
240+
} else {
241+
*value = (uint32_t)(stream->data[stream->byte_pos] & ((1 << remaining_bits) - 1));
242+
bit_count = (bit_count - remaining_bits);
243+
stream->bit_count = 8;
244+
error = exi_bitstream_has_overflow(stream);
245+
246+
while (bit_count >= 8) {
247+
*value = (*value << 8) | stream->data[stream->byte_pos];
248+
bit_count -= 8;
249+
if (stream->byte_pos < stream->data_size) {
250+
stream->byte_pos++;
251+
} else {
252+
error = EXI_ERROR__BITSTREAM_OVERFLOW;
253+
}
238254
}
239255

240-
*value = (*value << 1u) | bit;
256+
*value = (*value << bit_count) | (stream->data[stream->byte_pos] >> (8 - bit_count));
257+
stream->bit_count = bit_count;
241258
}
242259
{%- if add_debug_code == 1 %}
243260

0 commit comments

Comments
 (0)