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