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