Mercurial > libdvdnav.hg
comparison dvdread/nav_read.c @ 347:a5319a5c34f4 src
split bitstream reader functions in 2 separate files for reuse
author | nicodvb |
---|---|
date | Thu, 01 May 2008 17:13:36 +0000 |
parents | e75c52894630 |
children |
comparison
equal
deleted
inserted
replaced
346:e14f453bb208 | 347:a5319a5c34f4 |
---|---|
25 | 25 |
26 #include "bswap.h" | 26 #include "bswap.h" |
27 #include "nav_types.h" | 27 #include "nav_types.h" |
28 #include "nav_read.h" | 28 #include "nav_read.h" |
29 #include "dvdread_internal.h" | 29 #include "dvdread_internal.h" |
30 | 30 #include "bitreader.h" |
31 typedef struct { | 31 |
32 uint8_t *start; | 32 #define getbits_init dvdread_getbits_init |
33 uint32_t byte_position; | 33 #define getbits dvdread_getbits |
34 uint32_t bit_position; | |
35 uint8_t byte; | |
36 } getbits_state_t; | |
37 | |
38 static int getbits_init(getbits_state_t *state, uint8_t *start) { | |
39 if ((state == NULL) || (start == NULL)) return 0; | |
40 state->start = start; | |
41 state->bit_position = 0; | |
42 state->byte_position = 0; | |
43 state->byte = start[0]; | |
44 return 1; | |
45 } | |
46 | |
47 /* Non-optimized getbits. */ | |
48 /* This can easily be optimized for particular platforms. */ | |
49 static uint32_t getbits(getbits_state_t *state, uint32_t number_of_bits) { | |
50 uint32_t result=0; | |
51 uint8_t byte=0; | |
52 if (number_of_bits > 32) { | |
53 printf("Number of bits > 32 in getbits\n"); | |
54 abort(); | |
55 } | |
56 | |
57 if ((state->bit_position) > 0) { /* Last getbits left us in the middle of a byte. */ | |
58 if (number_of_bits > (8-state->bit_position)) { /* this getbits will span 2 or more bytes. */ | |
59 byte = state->byte; | |
60 byte = byte >> (state->bit_position); | |
61 result = byte; | |
62 number_of_bits -= (8-state->bit_position); | |
63 state->bit_position = 0; | |
64 state->byte_position++; | |
65 state->byte = state->start[state->byte_position]; | |
66 } else { | |
67 byte=state->byte; | |
68 state->byte = state->byte << number_of_bits; | |
69 byte = byte >> (8 - number_of_bits); | |
70 result = byte; | |
71 state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 8 */ | |
72 if (state->bit_position == 8) { | |
73 state->bit_position = 0; | |
74 state->byte_position++; | |
75 state->byte = state->start[state->byte_position]; | |
76 } | |
77 number_of_bits = 0; | |
78 } | |
79 } | |
80 if ((state->bit_position) == 0) { | |
81 while (number_of_bits > 7) { | |
82 result = (result << 8) + state->byte; | |
83 state->byte_position++; | |
84 state->byte = state->start[state->byte_position]; | |
85 number_of_bits -= 8; | |
86 } | |
87 if (number_of_bits > 0) { /* number_of_bits < 8 */ | |
88 byte = state->byte; | |
89 state->byte = state->byte << number_of_bits; | |
90 state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 7 */ | |
91 byte = byte >> (8 - number_of_bits); | |
92 result = (result << number_of_bits) + byte; | |
93 number_of_bits = 0; | |
94 } | |
95 } | |
96 | |
97 return result; | |
98 } | |
99 | |
100 #if 0 /* TODO: optimized versions not yet used */ | |
101 | |
102 /* WARNING: This function can only be used on a byte boundary. | |
103 No checks are made that we are in fact on a byte boundary. | |
104 */ | |
105 static uint16_t get16bits(getbits_state_t *state) { | |
106 uint16_t result; | |
107 state->byte_position++; | |
108 result = (state->byte << 8) + state->start[state->byte_position++]; | |
109 state->byte = state->start[state->byte_position]; | |
110 return result; | |
111 } | |
112 | |
113 /* WARNING: This function can only be used on a byte boundary. | |
114 No checks are made that we are in fact on a byte boundary. | |
115 */ | |
116 static uint32_t get32bits(getbits_state_t *state) { | |
117 uint32_t result; | |
118 state->byte_position++; | |
119 result = (state->byte << 8) + state->start[state->byte_position++]; | |
120 result = (result << 8) + state->start[state->byte_position++]; | |
121 result = (result << 8) + state->start[state->byte_position++]; | |
122 state->byte = state->start[state->byte_position]; | |
123 return result; | |
124 } | |
125 | |
126 #endif | |
127 | 34 |
128 void navRead_PCI(pci_t *pci, unsigned char *buffer) { | 35 void navRead_PCI(pci_t *pci, unsigned char *buffer) { |
129 int32_t i, j; | 36 int32_t i, j; |
130 getbits_state_t state; | 37 getbits_state_t state; |
131 if (!getbits_init(&state, buffer)) abort(); /* Passed NULL pointers */ | 38 if (!getbits_init(&state, buffer)) abort(); /* Passed NULL pointers */ |