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