Skip to content

Commit 81fdecd

Browse files
[FEAT] Add bitstream module in lib_ccxr (#1649)
* feat: Add bitstream module * run code formatters * Run cargo clippy --fix * Run cargo fmt --all * refactor: remove rust pointer from C struct * feat: Add bitstream module * run code formatters * Run cargo clippy --fix * Run cargo fmt --all * refactor: remove rust pointer from C struct * Added Bitstream to libccxr_exports * Minor Formatting Issue * Bitstream: Removed redundant CType * bitstream: recommended changes for is_byte_aligned * bitstream: recommended changes for long comments * bitstream: comment fix * bitstream: removed redundant comparism comments --------- Co-authored-by: Deepnarayan Sett <[email protected]> Co-authored-by: Deepnarayan Sett <[email protected]>
1 parent 099fa05 commit 81fdecd

File tree

8 files changed

+1382
-2
lines changed

8 files changed

+1382
-2
lines changed

src/lib_ccx/cc_bitstream.c

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
// Hold functions to read streams on a bit or byte oriented basis
44
// plus some data related helper functions.
55

6+
#ifndef DISABLE_RUST
7+
extern uint64_t ccxr_next_bits(struct bitstream *bs, uint32_t bnum);
8+
extern uint64_t ccxr_read_bits(struct bitstream *bs, uint32_t bnum);
9+
extern int ccxr_skip_bits(struct bitstream *bs, uint32_t bnum);
10+
extern int ccxr_is_byte_aligned(struct bitstream *bs);
11+
extern void ccxr_make_byte_aligned(struct bitstream *bs);
12+
extern const uint8_t *ccxr_next_bytes(struct bitstream *bs, size_t bynum);
13+
extern const uint8_t *ccxr_read_bytes(struct bitstream *bs, size_t bynum);
14+
extern uint64_t ccxr_read_exp_golomb_unsigned(struct bitstream *bs);
15+
extern int64_t ccxr_read_exp_golomb(struct bitstream *bs);
16+
extern uint8_t ccxr_reverse8(uint8_t data);
17+
extern uint64_t ccxr_bitstream_get_num(struct bitstream *bs, unsigned bytes, int advance);
18+
extern int64_t ccxr_read_int(struct bitstream *bs, unsigned bnum);
19+
#endif
20+
621
// Guidelines for all bitsream functions:
722
// * No function shall advance the pointer past the end marker
823
// * If bitstream.bitsleft < 0 do not attempt any read access,
@@ -35,6 +50,9 @@ int init_bitstream(struct bitstream *bstr, unsigned char *start, unsigned char *
3550
// there are not enough bits left in the bitstream.
3651
uint64_t next_bits(struct bitstream *bstr, unsigned bnum)
3752
{
53+
#ifndef DISABLE_RUST
54+
return ccxr_next_bits(bstr, bnum);
55+
#else
3856
uint64_t res = 0;
3957

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

101119
return res;
120+
#endif
102121
}
103122

104123
// Read bnum bits from bitstream bstr with the most significant
105124
// bit read first. A 64 bit unsigned integer is returned.
106125
uint64_t read_bits(struct bitstream *bstr, unsigned bnum)
107126
{
127+
#ifndef DISABLE_RUST
128+
return ccxr_read_bits(bstr, bnum);
129+
#else
108130
uint64_t res = next_bits(bstr, bnum);
109131

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

119141
return res;
142+
#endif
120143
}
121144

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

143-
// Special case for reading zero bits. Return zero
169+
// Special case for reading zero bits. Return one == success
144170
if (bnum == 0)
145171
return 1;
146172

@@ -153,13 +179,17 @@ int skip_bits(struct bitstream *bstr, unsigned bnum)
153179
bstr->pos += 1;
154180
}
155181
return 1;
182+
#endif
156183
}
157184

158185
// Return TRUE if the current position in the bitstream is on a byte
159186
// boundary, i.e., the next bit in the bitstream is the first bit in
160187
// a byte, otherwise return FALSE
161188
int is_byte_aligned(struct bitstream *bstr)
162189
{
190+
#ifndef DISABLE_RUST
191+
return ccxr_is_byte_aligned(bstr);
192+
#else
163193
// Sanity check
164194
if (bstr->end - bstr->pos < 0)
165195
fatal(CCX_COMMON_EXIT_BUG_BUG, "In is_byte_aligned: bitstream length can not be negative!");
@@ -175,11 +205,15 @@ int is_byte_aligned(struct bitstream *bstr)
175205
return 1;
176206
else
177207
return 0;
208+
#endif
178209
}
179210

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

210244
return;
245+
#endif
211246
}
212247

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

240278
return bstr->pos;
279+
#endif
241280
}
242281

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

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

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

@@ -296,11 +342,15 @@ uint64_t bitstream_get_num(struct bitstream *bstr, unsigned bytes, int advance)
296342
rval = (rval << 8) + uc;
297343
}
298344
return rval;
345+
#endif
299346
}
300347

301348
// Read unsigned Exp-Golomb code from bitstream
302349
uint64_t read_exp_golomb_unsigned(struct bitstream *bstr)
303350
{
351+
#ifndef DISABLE_RUST
352+
return ccxr_read_exp_golomb_unsigned(bstr);
353+
#else
304354
uint64_t res = 0;
305355
int zeros = 0;
306356

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

312362
return res;
363+
#endif
313364
}
314365

315366
// Read signed Exp-Golomb code from bitstream
316367
int64_t read_exp_golomb(struct bitstream *bstr)
317368
{
369+
#ifndef DISABLE_RUST
370+
return ccxr_read_exp_golomb(bstr);
371+
#else
318372
int64_t res = 0;
319373

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

327381
return res;
382+
#endif
328383
}
329384

330385
// Read unsigned integer with bnum bits length. Basically an
@@ -337,18 +392,25 @@ uint64_t read_int_unsigned(struct bitstream *bstr, unsigned bnum)
337392
// Read signed integer with bnum bits length.
338393
int64_t read_int(struct bitstream *bstr, unsigned bnum)
339394
{
395+
#ifndef DISABLE_RUST
396+
return ccxr_read_int(bstr, bnum);
397+
#else
340398
uint64_t res = read_bits(bstr, bnum);
341399

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

346404
return (0xFFFFFFFFFFFFFFFFULL << bnum) | res;
405+
#endif
347406
}
348407

349408
// Return the value with the bit order reversed.
350409
uint8_t reverse8(uint8_t data)
351410
{
411+
#ifndef DISABLE_RUST
412+
return ccxr_reverse8(data);
413+
#else
352414
uint8_t res = 0;
353415

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

360422
return res;
423+
#endif
361424
}

src/lib_ccx/cc_bitstream.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _BITSTREAM_
22
#define _BITSTREAM_
33

4-
54
// The structure holds the current position in the bitstream.
65
// pos points to the current byte position and bpos counts the
76
// bits left unread at the current byte pos. No bit read means

src/rust/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn main() {
2727
".*(?i)_?dtvcc_.*",
2828
"encoder_ctx",
2929
"lib_cc_decode",
30+
"bitstream",
3031
"cc_subtitle",
3132
"ccx_output_format",
3233
"ccx_boundary_time",

0 commit comments

Comments
 (0)