Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
83fb2d7
feat: Add bitstream module
mrswastik-robot Dec 25, 2024
c3a91a6
run code formatters
mrswastik-robot Dec 25, 2024
c873193
Run cargo clippy --fix
mrswastik-robot Dec 25, 2024
37e126a
Run cargo fmt --all
mrswastik-robot Dec 25, 2024
666a1bf
refactor: remove rust pointer from C struct
mrswastik-robot Dec 27, 2024
d70fcd3
feat: Add bitstream module
mrswastik-robot Dec 25, 2024
e9a9155
run code formatters
mrswastik-robot Dec 25, 2024
bdb0c5f
Run cargo clippy --fix
mrswastik-robot Dec 25, 2024
d869d7c
Run cargo fmt --all
mrswastik-robot Dec 25, 2024
04a0eed
refactor: remove rust pointer from C struct
mrswastik-robot Dec 27, 2024
e04b430
Added Bitstream to libccxr_exports
steel-bucket Jun 14, 2025
0ebccad
Merge branch 'migration-bitstream-module' into migration-module-changes
steel-bucket Jun 14, 2025
b1284d2
Merge pull request #1 from steel-bucket/migration-module-changes
mrswastik-robot Jun 14, 2025
22a7259
Minor Formatting Issue
steel-bucket Jun 14, 2025
4ac2792
Merge pull request #2 from steel-bucket/bitstream-fmt-issue
mrswastik-robot Jun 14, 2025
d1b7984
Bitstream: Removed redundant CType
steel-bucket Jun 30, 2025
ca5aafd
Merge pull request #4 from steel-bucket/fix-bitstream-redundant-ctype
mrswastik-robot Jun 30, 2025
420979c
bitstream: recommended changes for is_byte_aligned
steel-bucket Jul 6, 2025
90de368
Merge pull request #5 from steel-bucket/bitstream-recommended-control…
mrswastik-robot Jul 6, 2025
befe801
bitstream: recommended changes for long comments
steel-bucket Jul 6, 2025
18d0b11
bitstream: comment fix
steel-bucket Jul 6, 2025
4fbb0ce
bitstream: removed redundant comparism comments
steel-bucket Jul 6, 2025
8a0a587
Merge pull request #6 from steel-bucket/bitstream-recommended-comment…
mrswastik-robot Jul 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion src/lib_ccx/cc_bitstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
// Hold functions to read streams on a bit or byte oriented basis
// plus some data related helper functions.

#ifndef DISABLE_RUST
extern uint64_t ccxr_next_bits(struct bitstream *bs, uint32_t bnum);
extern uint64_t ccxr_read_bits(struct bitstream *bs, uint32_t bnum);
extern int ccxr_skip_bits(struct bitstream *bs, uint32_t bnum);
extern int ccxr_is_byte_aligned(struct bitstream *bs);
extern void ccxr_make_byte_aligned(struct bitstream *bs);
extern const uint8_t *ccxr_next_bytes(struct bitstream *bs, size_t bynum);
extern const uint8_t *ccxr_read_bytes(struct bitstream *bs, size_t bynum);
extern uint64_t ccxr_read_exp_golomb_unsigned(struct bitstream *bs);
extern int64_t ccxr_read_exp_golomb(struct bitstream *bs);
extern uint8_t ccxr_reverse8(uint8_t data);
extern uint64_t ccxr_bitstream_get_num(struct bitstream *bs, unsigned bytes, int advance);
extern int64_t ccxr_read_int(struct bitstream *bs, unsigned bnum);
#endif

// Guidelines for all bitsream functions:
// * No function shall advance the pointer past the end marker
// * If bitstream.bitsleft < 0 do not attempt any read access,
Expand Down Expand Up @@ -35,6 +50,9 @@ int init_bitstream(struct bitstream *bstr, unsigned char *start, unsigned char *
// there are not enough bits left in the bitstream.
uint64_t next_bits(struct bitstream *bstr, unsigned bnum)
{
#ifndef DISABLE_RUST
return ccxr_next_bits(bstr, bnum);
#else
uint64_t res = 0;

if (bnum > 64)
Expand Down Expand Up @@ -99,12 +117,16 @@ uint64_t next_bits(struct bitstream *bstr, unsigned bnum)
bstr->_i_pos = vpos;

return res;
#endif
}

// Read bnum bits from bitstream bstr with the most significant
// bit read first. A 64 bit unsigned integer is returned.
uint64_t read_bits(struct bitstream *bstr, unsigned bnum)
{
#ifndef DISABLE_RUST
return ccxr_read_bits(bstr, bnum);
#else
uint64_t res = next_bits(bstr, bnum);

// Special case for reading zero bits. Also abort when not enough
Expand All @@ -117,13 +139,17 @@ uint64_t read_bits(struct bitstream *bstr, unsigned bnum)
bstr->pos = bstr->_i_pos;

return res;
#endif
}

// This function will advance the bitstream by bnum bits, if possible.
// Advancing of more than 64 bits is possible.
// Return TRUE when successful, otherwise FALSE
int skip_bits(struct bitstream *bstr, unsigned bnum)
{
#ifndef DISABLE_RUST
return ccxr_skip_bits(bstr, bnum);
#else
// Sanity check
if (bstr->end - bstr->pos < 0)
fatal(CCX_COMMON_EXIT_BUG_BUG, "In skip_bits: bitstream length cannot be negative!");
Expand All @@ -140,7 +166,7 @@ int skip_bits(struct bitstream *bstr, unsigned bnum)
if (bstr->bitsleft < 0)
return 0;

// Special case for reading zero bits. Return zero
// Special case for reading zero bits. Return one == success
if (bnum == 0)
return 1;

Expand All @@ -153,13 +179,17 @@ int skip_bits(struct bitstream *bstr, unsigned bnum)
bstr->pos += 1;
}
return 1;
#endif
}

// Return TRUE if the current position in the bitstream is on a byte
// boundary, i.e., the next bit in the bitstream is the first bit in
// a byte, otherwise return FALSE
int is_byte_aligned(struct bitstream *bstr)
{
#ifndef DISABLE_RUST
return ccxr_is_byte_aligned(bstr);
#else
// Sanity check
if (bstr->end - bstr->pos < 0)
fatal(CCX_COMMON_EXIT_BUG_BUG, "In is_byte_aligned: bitstream length can not be negative!");
Expand All @@ -175,11 +205,15 @@ int is_byte_aligned(struct bitstream *bstr)
return 1;
else
return 0;
#endif
}

// Move bitstream to next byte border. Adjust bitsleft.
void make_byte_aligned(struct bitstream *bstr)
{
#ifndef DISABLE_RUST
ccxr_make_byte_aligned(bstr);
#else
// Sanity check
if (bstr->end - bstr->pos < 0)
fatal(CCX_COMMON_EXIT_BUG_BUG, "In make_byte_aligned: bitstream length can not be negative!");
Expand Down Expand Up @@ -208,6 +242,7 @@ void make_byte_aligned(struct bitstream *bstr)
bstr->bitsleft = 0LL + 8 * (bstr->end - bstr->pos - 1) + bstr->bpos;

return;
#endif
}

// Return pointer to first of bynum bytes from the bitstream if the
Expand All @@ -217,6 +252,9 @@ void make_byte_aligned(struct bitstream *bstr)
// This function does not advance the bitstream pointer.
unsigned char *next_bytes(struct bitstream *bstr, unsigned bynum)
{
#ifndef DISABLE_RUST
return (unsigned char *)ccxr_next_bytes(bstr, bynum);
#else
// Sanity check
if (bstr->end - bstr->pos < 0)
fatal(CCX_COMMON_EXIT_BUG_BUG, "In next_bytes: bitstream length can not be negative!");
Expand All @@ -238,6 +276,7 @@ unsigned char *next_bytes(struct bitstream *bstr, unsigned bynum)
bstr->_i_pos = bstr->pos + bynum;

return bstr->pos;
#endif
}

// Return pointer to first of bynum bytes from the bitstream if the
Expand All @@ -247,6 +286,9 @@ unsigned char *next_bytes(struct bitstream *bstr, unsigned bynum)
// This function does advance the bitstream pointer.
unsigned char *read_bytes(struct bitstream *bstr, unsigned bynum)
{
#ifndef DISABLE_RUST
return (unsigned char *)ccxr_read_bytes(bstr, bynum);
#else
unsigned char *res = next_bytes(bstr, bynum);

// Advance the bitstream when a read was possible
Expand All @@ -256,6 +298,7 @@ unsigned char *read_bytes(struct bitstream *bstr, unsigned bynum)
bstr->pos = bstr->_i_pos;
}
return res;
#endif
}

// Return an integer number with "bytes" precision from the current
Expand All @@ -266,6 +309,9 @@ unsigned char *read_bytes(struct bitstream *bstr, unsigned bynum)
// little-endian and big-endian CPUs.
uint64_t bitstream_get_num(struct bitstream *bstr, unsigned bytes, int advance)
{
#ifndef DISABLE_RUST
return ccxr_bitstream_get_num(bstr, bytes, advance);
#else
void *bpos;
uint64_t rval = 0;

Expand Down Expand Up @@ -296,11 +342,15 @@ uint64_t bitstream_get_num(struct bitstream *bstr, unsigned bytes, int advance)
rval = (rval << 8) + uc;
}
return rval;
#endif
}

// Read unsigned Exp-Golomb code from bitstream
uint64_t read_exp_golomb_unsigned(struct bitstream *bstr)
{
#ifndef DISABLE_RUST
return ccxr_read_exp_golomb_unsigned(bstr);
#else
uint64_t res = 0;
int zeros = 0;

Expand All @@ -310,11 +360,15 @@ uint64_t read_exp_golomb_unsigned(struct bitstream *bstr)
res = (0x01 << zeros) - 1 + read_bits(bstr, zeros);

return res;
#endif
}

// Read signed Exp-Golomb code from bitstream
int64_t read_exp_golomb(struct bitstream *bstr)
{
#ifndef DISABLE_RUST
return ccxr_read_exp_golomb(bstr);
#else
int64_t res = 0;

res = read_exp_golomb_unsigned(bstr);
Expand All @@ -325,6 +379,7 @@ int64_t read_exp_golomb(struct bitstream *bstr)
res = (res / 2 + (res % 2 ? 1 : 0)) * (res % 2 ? 1 : -1);

return res;
#endif
}

// Read unsigned integer with bnum bits length. Basically an
Expand All @@ -337,18 +392,25 @@ uint64_t read_int_unsigned(struct bitstream *bstr, unsigned bnum)
// Read signed integer with bnum bits length.
int64_t read_int(struct bitstream *bstr, unsigned bnum)
{
#ifndef DISABLE_RUST
return ccxr_read_int(bstr, bnum);
#else
uint64_t res = read_bits(bstr, bnum);

// Special case for reading zero bits. Return zero
if (bnum == 0)
return 0;

return (0xFFFFFFFFFFFFFFFFULL << bnum) | res;
#endif
}

// Return the value with the bit order reversed.
uint8_t reverse8(uint8_t data)
{
#ifndef DISABLE_RUST
return ccxr_reverse8(data);
#else
uint8_t res = 0;

for (int k = 0; k < 8; k++)
Expand All @@ -358,4 +420,5 @@ uint8_t reverse8(uint8_t data)
}

return res;
#endif
}
1 change: 0 additions & 1 deletion src/lib_ccx/cc_bitstream.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef _BITSTREAM_
#define _BITSTREAM_


// The structure holds the current position in the bitstream.
// pos points to the current byte position and bpos counts the
// bits left unread at the current byte pos. No bit read means
Expand Down
1 change: 1 addition & 0 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn main() {
".*(?i)_?dtvcc_.*",
"encoder_ctx",
"lib_cc_decode",
"bitstream",
"cc_subtitle",
"ccx_output_format",
"ccx_boundary_time",
Expand Down
Loading
Loading