[libdvdnav-devel] [Git][videolan/libdvdread][master] 2 commits: bitreader: Add a minimal test

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Tue Jan 11 04:53:24 UTC 2022



Jean-Baptiste Kempf pushed to branch master at VideoLAN / libdvdread


Commits:
3a58bf30 by Hugo Beauzée-Luyssen at 2022-01-03T09:16:24+00:00
bitreader: Add a minimal test

- - - - -
f6774c38 by Steve Lhomme at 2022-01-03T09:16:24+00:00
avoid reading the next byte ahead

It may not be in the end of the buffer anymore.

The byte value used was always equivalent to
> state->start[state->byte_position] << state->bit_position

Also constify the read buffer.

- - - - -


2 changed files:

- src/bitreader.c
- src/dvdread/bitreader.h


Changes:

=====================================
src/bitreader.c
=====================================
@@ -23,15 +23,15 @@
 #include <stdlib.h>
 #include <string.h>
 #include <inttypes.h>
+#include <assert.h>
 
 #include "dvdread/bitreader.h"
 
-int dvdread_getbits_init(getbits_state_t *state, uint8_t *start) {
+int dvdread_getbits_init(getbits_state_t *state, const uint8_t *start) {
   if ((state == NULL) || (start == NULL)) return 0;
   state->start = start;
   state->bit_position = 0;
   state->byte_position = 0;
-  state->byte = start[0];
   return 1;
 }
 
@@ -47,37 +47,32 @@ uint32_t dvdread_getbits(getbits_state_t *state, uint32_t number_of_bits) {
 
   if ((state->bit_position) > 0) {  /* Last getbits left us in the middle of a byte. */
     if (number_of_bits > (8-state->bit_position)) { /* this getbits will span 2 or more bytes. */
-      byte = state->byte;
+      byte = state->start[state->byte_position] << state->bit_position;
       byte = byte >> (state->bit_position);
       result = byte;
       number_of_bits -= (8-state->bit_position);
       state->bit_position = 0;
       state->byte_position++;
-      state->byte = state->start[state->byte_position];
     } else {
-      byte=state->byte;
-      state->byte = state->byte << number_of_bits;
+      byte = state->start[state->byte_position] << state->bit_position;
       byte = byte >> (8 - number_of_bits);
       result = byte;
       state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 8 */
       if (state->bit_position == 8) {
         state->bit_position = 0;
         state->byte_position++;
-        state->byte = state->start[state->byte_position];
       }
       number_of_bits = 0;
     }
   }
   if ((state->bit_position) == 0) {
     while (number_of_bits > 7) {
-      result = (result << 8) + state->byte;
+      result = (result << 8) + state->start[state->byte_position];
       state->byte_position++;
-      state->byte = state->start[state->byte_position];
       number_of_bits -= 8;
     }
     if (number_of_bits > 0) { /* number_of_bits < 8 */
-      byte = state->byte;
-      state->byte = state->byte << number_of_bits;
+      byte = state->start[state->byte_position] << state->bit_position;
       state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 7 */
       byte = byte >> (8 - number_of_bits);
       result = (result << number_of_bits) + byte;
@@ -115,3 +110,54 @@ uint32_t dvdread_get32bits(getbits_state_t *state) {
 }
 
 #endif
+
+#ifdef BITREADER_TESTS
+
+int main()
+{
+    uint8_t buff[2] = {
+        0x6E, 0xC2
+        // 0b 01101110 11000010
+    };
+    getbits_state_t state;
+    dvdread_getbits_init(&state, buff);
+
+    uint32_t bits = dvdread_getbits(&state, 3);
+    assert(bits == 3);
+
+    bits = dvdread_getbits(&state, 3);
+    assert(bits == 3);
+
+    bits = dvdread_getbits(&state, 4);
+    assert(bits == 11);
+
+    bits = dvdread_getbits(&state, 6);
+    assert(bits == 2);
+
+    dvdread_getbits_init(&state, buff);
+    bits = dvdread_getbits(&state, 10);
+    assert(bits == 443);
+
+    bits = dvdread_getbits(&state, 6);
+    assert(bits == 2);
+
+    dvdread_getbits_init(&state, buff);
+    bits = dvdread_getbits(&state, 16);
+    assert(bits == 28354);
+
+    buff[0] = buff[1] = 0xFF;
+    dvdread_getbits_init(&state, buff);
+    bits = dvdread_getbits(&state, 16);
+    assert(bits == 0xFFFF);
+
+    uint8_t large[5] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
+    dvdread_getbits_init(&state, large);
+    bits = dvdread_getbits(&state, 8);
+    assert(bits == 0xFF);
+    bits = dvdread_getbits(&state, 32);
+    assert(bits == 0xFFFFFFFF);
+
+    return 0;
+}
+
+#endif


=====================================
src/dvdread/bitreader.h
=====================================
@@ -26,13 +26,12 @@ extern "C" {
 #endif
 
 typedef struct {
-  uint8_t *start;
+  const uint8_t *start;
   uint32_t byte_position;
   uint32_t bit_position;
-  uint8_t byte;
 } getbits_state_t;
 
-int dvdread_getbits_init(getbits_state_t *state, uint8_t *start);
+int dvdread_getbits_init(getbits_state_t *state, const uint8_t *start);
 uint32_t dvdread_getbits(getbits_state_t *state, uint32_t number_of_bits);
 
 #ifdef __cplusplus



View it on GitLab: https://code.videolan.org/videolan/libdvdread/-/compare/46754b9ea0090b9af0b5bbd5781b88de788e2aab...f6774c386f404b49a13d9027567d57e104c5a415

-- 
View it on GitLab: https://code.videolan.org/videolan/libdvdread/-/compare/46754b9ea0090b9af0b5bbd5781b88de788e2aab...f6774c386f404b49a13d9027567d57e104c5a415
You're receiving this email because of your account on code.videolan.org.




More information about the libdvdnav-devel mailing list