Skip to content

[FEAT] Add bitstream module in lib_ccxr #1649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
63 changes: 63 additions & 0 deletions 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 Down Expand Up @@ -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