CCSDS 124.0-B-1 C 1.0.0
CCSDS 124.0-B-1 Lossless Compression
Loading...
Searching...
No Matches
Bit Buffer API

Variable-length output buffer for compressed data. More...

Data Structures

struct  bitbuffer
 Variable-length bit buffer structure. More...
 

Functions

void bitbuffer_init (bitbuffer_t *bb)
 Initialize bit buffer to empty state.
 
void bitbuffer_clear (bitbuffer_t *bb)
 Clear bit buffer contents.
 
int bitbuffer_append_bit (bitbuffer_t *bb, int bit)
 Append a single bit to buffer.
 
int bitbuffer_append_bits (bitbuffer_t *bb, const uint8_t *data, size_t num_bits)
 Append multiple bits from byte array.
 
int bitbuffer_append_value (bitbuffer_t *bb, uint32_t value, size_t num_bits)
 Append multiple bits from a value directly to accumulator.
 
int bitbuffer_append_bitvector (bitbuffer_t *bb, const bitvector_t *bv)
 Append all bits from a bit vector.
 
size_t bitbuffer_size (const bitbuffer_t *bb)
 Get number of bits in buffer.
 
size_t bitbuffer_to_bytes (const bitbuffer_t *bb, uint8_t *data, size_t max_bytes)
 Convert bit buffer to byte array.
 

Detailed Description

Variable-length output buffer for compressed data.

Bit buffers accumulate compressed output bit-by-bit and convert to byte arrays for transmission or storage.

Function Documentation

◆ bitbuffer_append_bit()

int bitbuffer_append_bit ( bitbuffer_t bb,
int  bit 
)

Append a single bit to buffer.

Parameters
[out]bbBit buffer
[in]bitBit value (0 or 1)
Returns
CCSDS124_OK on success, CCSDS124_ERROR_OVERFLOW if full

Definition at line 77 of file bitbuffer.c.

References acc, acc_len, bitbuffer_flush_acc(), CCSDS124_ERROR_INVALID_ARG, CCSDS124_ERROR_OVERFLOW, CCSDS124_MAX_OUTPUT_BYTES, CCSDS124_OK, and num_bits.

Referenced by bitbuffer_append_bits(), bitbuffer_append_bitvector(), ccsds124_bit_extract(), ccsds124_bit_extract_forward(), ccsds124_compress_packet(), ccsds124_count_encode(), and ccsds124_rle_encode().

◆ bitbuffer_append_bits()

int bitbuffer_append_bits ( bitbuffer_t bb,
const uint8_t *  data,
size_t  num_bits 
)

Append multiple bits from byte array.

Parameters
[out]bbBit buffer
[in]dataSource bytes (MSB first within each byte)
[in]num_bitsNumber of bits to append
Returns
CCSDS124_OK on success, CCSDS124_ERROR_OVERFLOW if full

Definition at line 105 of file bitbuffer.c.

References bitbuffer_append_bit(), CCSDS124_ERROR_INVALID_ARG, CCSDS124_ERROR_OVERFLOW, CCSDS124_MAX_OUTPUT_BYTES, CCSDS124_OK, and num_bits.

◆ bitbuffer_append_bitvector()

int bitbuffer_append_bitvector ( bitbuffer_t bb,
const bitvector_t bv 
)

Append all bits from a bit vector.

Parameters
[out]bbBit buffer
[in]bvBit vector to append
Returns
CCSDS124_OK on success, CCSDS124_ERROR_OVERFLOW if full

Definition at line 181 of file bitbuffer.c.

References bitbuffer_append_bit(), bitvector_get_bit(), CCSDS124_ERROR_INVALID_ARG, CCSDS124_OK, and bitvector::length.

Referenced by ccsds124_compress_packet().

◆ bitbuffer_append_value()

int bitbuffer_append_value ( bitbuffer_t bb,
uint32_t  value,
size_t  num_bits 
)

Append multiple bits from a value directly to accumulator.

Efficiently appends up to 24 bits from a uint32_t value MSB-first. The top 'num_bits' bits of the value (shifted left) are appended.

Parameters
[out]bbBit buffer
[in]valueValue containing bits to append (left-justified)
[in]num_bitsNumber of bits to append (1-24)
Returns
CCSDS124_OK on success, CCSDS124_ERROR_OVERFLOW if full

Append multiple bits from a value directly to accumulator.

Batches bit operations to reduce function call overhead vs calling bitbuffer_append_bit() in a loop. Used by COUNT encoding lookup table.

Performance: ~2% compression speedup from this optimization. Combined with DeBruijn bit extraction and word-level operations, achieves 14-16% faster compression and 13-39% faster decompression on real-world datasets.

Parameters
[out]bbBit buffer
[in]valueValue containing bits (right-justified)
[in]num_bitsNumber of bits to append (1-24)
Returns
CCSDS124_OK on success, error code otherwise

Definition at line 151 of file bitbuffer.c.

References acc, acc_len, bitbuffer_flush_acc(), CCSDS124_ERROR_INVALID_ARG, CCSDS124_ERROR_OVERFLOW, CCSDS124_MAX_OUTPUT_BYTES, CCSDS124_OK, and num_bits.

Referenced by ccsds124_count_encode().

◆ bitbuffer_clear()

void bitbuffer_clear ( bitbuffer_t bb)

Clear bit buffer contents.

Parameters
[out]bbBit buffer to clear

Definition at line 50 of file bitbuffer.c.

References bitbuffer_init().

Referenced by ccsds124_compress_packet().

◆ bitbuffer_init()

void bitbuffer_init ( bitbuffer_t bb)

Initialize bit buffer to empty state.

Parameters
[out]bbBit buffer to initialize

Definition at line 40 of file bitbuffer.c.

References acc, acc_len, CCSDS124_MAX_OUTPUT_BYTES, data, and num_bits.

Referenced by bitbuffer_clear(), and ccsds124_compress().

◆ bitbuffer_size()

size_t bitbuffer_size ( const bitbuffer_t bb)

Get number of bits in buffer.

Parameters
[in]bbBit buffer
Returns
Number of bits currently stored

Definition at line 226 of file bitbuffer.c.

References num_bits.

◆ bitbuffer_to_bytes()

size_t bitbuffer_to_bytes ( const bitbuffer_t bb,
uint8_t *  data,
size_t  max_bytes 
)

Convert bit buffer to byte array.

Pads final byte with zeros if not byte-aligned.

Parameters
[in]bbBit buffer
[out]dataDestination byte array
[in]max_bytesMaximum bytes to write
Returns
Number of bytes written

Definition at line 244 of file bitbuffer.c.

References acc, acc_len, data, and num_bits.

Referenced by ccsds124_compress().