Mercurial > libdvdread4.hg
annotate libdvdread/bitreader.c @ 0:427b7da5cbdb src
first split of dvdread; it's just a copy of dvdnav still to be cleaned
author | nicodvb |
---|---|
date | Sun, 01 Jun 2008 08:39:07 +0000 |
parents | |
children |
rev | line source |
---|---|
0
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
1 /* |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
2 * Copyright (C) 2000, 2001, 2002, 2003 Håkan Hjort <d95hjort@dtek.chalmers.se> |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
3 * |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
7 * (at your option) any later version. |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
8 * |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
9 * This program is distributed in the hope that it will be useful, |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
12 * GNU General Public License for more details. |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
13 * |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
14 * You should have received a copy of the GNU General Public License |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
15 * along with this program; if not, write to the Free Software |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
17 */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
18 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
19 #include "config.h" |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
20 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
21 #include <stdio.h> |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
22 #include <stdlib.h> |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
23 #include <string.h> |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
24 #include <inttypes.h> |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
25 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
26 #include "bitreader.h" |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
27 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
28 int dvdread_getbits_init(getbits_state_t *state, uint8_t *start) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
29 if ((state == NULL) || (start == NULL)) return 0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
30 state->start = start; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
31 state->bit_position = 0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
32 state->byte_position = 0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
33 state->byte = start[0]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
34 return 1; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
35 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
36 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
37 /* Non-optimized getbits. */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
38 /* This can easily be optimized for particular platforms. */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
39 uint32_t dvdread_getbits(getbits_state_t *state, uint32_t number_of_bits) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
40 uint32_t result=0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
41 uint8_t byte=0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
42 if (number_of_bits > 32) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
43 printf("Number of bits > 32 in getbits\n"); |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
44 abort(); |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
45 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
46 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
47 if ((state->bit_position) > 0) { /* Last getbits left us in the middle of a byte. */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
48 if (number_of_bits > (8-state->bit_position)) { /* this getbits will span 2 or more bytes. */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
49 byte = state->byte; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
50 byte = byte >> (state->bit_position); |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
51 result = byte; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
52 number_of_bits -= (8-state->bit_position); |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
53 state->bit_position = 0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
54 state->byte_position++; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
55 state->byte = state->start[state->byte_position]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
56 } else { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
57 byte=state->byte; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
58 state->byte = state->byte << number_of_bits; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
59 byte = byte >> (8 - number_of_bits); |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
60 result = byte; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
61 state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 8 */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
62 if (state->bit_position == 8) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
63 state->bit_position = 0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
64 state->byte_position++; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
65 state->byte = state->start[state->byte_position]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
66 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
67 number_of_bits = 0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
68 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
69 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
70 if ((state->bit_position) == 0) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
71 while (number_of_bits > 7) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
72 result = (result << 8) + state->byte; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
73 state->byte_position++; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
74 state->byte = state->start[state->byte_position]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
75 number_of_bits -= 8; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
76 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
77 if (number_of_bits > 0) { /* number_of_bits < 8 */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
78 byte = state->byte; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
79 state->byte = state->byte << number_of_bits; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
80 state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 7 */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
81 byte = byte >> (8 - number_of_bits); |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
82 result = (result << number_of_bits) + byte; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
83 number_of_bits = 0; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
84 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
85 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
86 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
87 return result; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
88 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
89 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
90 #if 0 /* TODO: optimized versions not yet used */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
91 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
92 /* WARNING: This function can only be used on a byte boundary. |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
93 No checks are made that we are in fact on a byte boundary. |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
94 */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
95 uint16_t dvdread_get16bits(getbits_state_t *state) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
96 uint16_t result; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
97 state->byte_position++; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
98 result = (state->byte << 8) + state->start[state->byte_position++]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
99 state->byte = state->start[state->byte_position]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
100 return result; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
101 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
102 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
103 /* WARNING: This function can only be used on a byte boundary. |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
104 No checks are made that we are in fact on a byte boundary. |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
105 */ |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
106 uint32_t dvdread_get32bits(getbits_state_t *state) { |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
107 uint32_t result; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
108 state->byte_position++; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
109 result = (state->byte << 8) + state->start[state->byte_position++]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
110 result = (result << 8) + state->start[state->byte_position++]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
111 result = (result << 8) + state->start[state->byte_position++]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
112 state->byte = state->start[state->byte_position]; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
113 return result; |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
114 } |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
115 |
427b7da5cbdb
first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff
changeset
|
116 #endif |