10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
|
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
10989
|
25 ** $Id: bits.c,v 1.1 2003/08/30 22:30:21 arpi Exp $
|
10725
|
26 **/
|
|
27
|
|
28 #include "common.h"
|
|
29 #include "structs.h"
|
|
30
|
|
31 #include <stdlib.h>
|
|
32 #include <string.h>
|
|
33 #include "bits.h"
|
|
34
|
|
35 /* initialize buffer, call once before first getbits or showbits */
|
|
36 void faad_initbits(bitfile *ld, void *_buffer, uint32_t buffer_size)
|
|
37 {
|
|
38 uint32_t tmp;
|
|
39
|
10989
|
40 if (ld == NULL)
|
|
41 return;
|
|
42
|
|
43 memset(ld, 0, sizeof(bitfile));
|
|
44
|
|
45 if (buffer_size == 0 || _buffer == NULL)
|
|
46 {
|
|
47 ld->error = 1;
|
|
48 ld->no_more_reading = 1;
|
|
49 return;
|
|
50 }
|
|
51
|
10725
|
52 ld->buffer = malloc((buffer_size+12)*sizeof(uint8_t));
|
|
53 memset(ld->buffer, 0, (buffer_size+12)*sizeof(uint8_t));
|
|
54 memcpy(ld->buffer, _buffer, buffer_size*sizeof(uint8_t));
|
|
55
|
|
56 ld->buffer_size = buffer_size;
|
|
57
|
|
58 tmp = getdword((uint32_t*)ld->buffer);
|
|
59 #ifndef ARCH_IS_BIG_ENDIAN
|
|
60 BSWAP(tmp);
|
|
61 #endif
|
|
62 ld->bufa = tmp;
|
|
63
|
|
64 tmp = getdword((uint32_t*)ld->buffer + 1);
|
|
65 #ifndef ARCH_IS_BIG_ENDIAN
|
|
66 BSWAP(tmp);
|
|
67 #endif
|
|
68 ld->bufb = tmp;
|
|
69
|
|
70 ld->start = (uint32_t*)ld->buffer;
|
|
71 ld->tail = ((uint32_t*)ld->buffer + 2);
|
|
72
|
|
73 ld->bits_left = 32;
|
|
74
|
|
75 ld->bytes_used = 0;
|
|
76 ld->no_more_reading = 0;
|
|
77 ld->error = 0;
|
|
78 }
|
|
79
|
|
80 void faad_endbits(bitfile *ld)
|
|
81 {
|
|
82 if (ld)
|
|
83 if (ld->buffer) free(ld->buffer);
|
|
84 }
|
|
85
|
|
86 uint32_t faad_get_processed_bits(bitfile *ld)
|
|
87 {
|
10989
|
88 return (uint32_t)(8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left));
|
10725
|
89 }
|
|
90
|
|
91 uint8_t faad_byte_align(bitfile *ld)
|
|
92 {
|
|
93 uint8_t remainder = (uint8_t)((32 - ld->bits_left) % 8);
|
|
94
|
|
95 if (remainder)
|
|
96 {
|
|
97 faad_flushbits(ld, 8 - remainder);
|
|
98 return (8 - remainder);
|
|
99 }
|
|
100 return 0;
|
|
101 }
|
|
102
|
10989
|
103 void faad_flushbits_ex(bitfile *ld, uint32_t bits)
|
|
104 {
|
|
105 uint32_t tmp;
|
|
106
|
|
107 ld->bufa = ld->bufb;
|
|
108 tmp = getdword(ld->tail);
|
|
109 ld->tail++;
|
|
110 #ifndef ARCH_IS_BIG_ENDIAN
|
|
111 BSWAP(tmp);
|
|
112 #endif
|
|
113 ld->bufb = tmp;
|
|
114 ld->bits_left += (32 - bits);
|
|
115 ld->bytes_used += 4;
|
|
116 if (ld->bytes_used == ld->buffer_size)
|
|
117 ld->no_more_reading = 1;
|
|
118 if (ld->bytes_used > ld->buffer_size)
|
|
119 ld->error = 1;
|
|
120 }
|
|
121
|
10725
|
122 /* rewind to beginning */
|
|
123 void faad_rewindbits(bitfile *ld)
|
|
124 {
|
|
125 uint32_t tmp;
|
|
126
|
|
127 tmp = ld->start[0];
|
|
128 #ifndef ARCH_IS_BIG_ENDIAN
|
|
129 BSWAP(tmp);
|
|
130 #endif
|
|
131 ld->bufa = tmp;
|
|
132
|
|
133 tmp = ld->start[1];
|
|
134 #ifndef ARCH_IS_BIG_ENDIAN
|
|
135 BSWAP(tmp);
|
|
136 #endif
|
|
137 ld->bufb = tmp;
|
|
138 ld->bits_left = 32;
|
|
139 ld->tail = &ld->start[2];
|
|
140 ld->bytes_used = 0;
|
|
141 ld->no_more_reading = 0;
|
|
142 }
|
|
143
|
|
144 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
|
|
145 DEBUGDEC)
|
|
146 {
|
|
147 uint16_t i;
|
|
148 uint8_t temp;
|
|
149 uint16_t bytes = (uint16_t)bits / 8;
|
|
150 uint8_t remainder = (uint8_t)bits % 8;
|
|
151
|
|
152 uint8_t *buffer = (uint8_t*)malloc((bytes+1)*sizeof(uint8_t));
|
|
153
|
|
154 for (i = 0; i < bytes; i++)
|
|
155 {
|
|
156 buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
|
|
157 }
|
|
158
|
|
159 if (remainder)
|
|
160 {
|
|
161 temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
|
|
162
|
|
163 buffer[bytes] = temp;
|
|
164 }
|
|
165
|
|
166 return buffer;
|
|
167 }
|
|
168
|
|
169 /* reversed bit reading routines, used for RVLC and HCR */
|
|
170 void faad_initbits_rev(bitfile *ld, void *buffer,
|
|
171 uint32_t bits_in_buffer)
|
|
172 {
|
|
173 uint32_t tmp;
|
|
174 int32_t index;
|
|
175
|
|
176 ld->buffer_size = bit2byte(bits_in_buffer);
|
|
177
|
|
178 index = (bits_in_buffer+31)/32 - 1;
|
|
179
|
|
180 ld->start = (uint32_t*)buffer + index - 2;
|
|
181
|
|
182 tmp = getdword((uint32_t*)buffer + index);
|
|
183 #ifndef ARCH_IS_BIG_ENDIAN
|
|
184 BSWAP(tmp);
|
|
185 #endif
|
|
186 ld->bufa = tmp;
|
|
187
|
|
188 tmp = getdword((uint32_t*)buffer + index - 1);
|
|
189 #ifndef ARCH_IS_BIG_ENDIAN
|
|
190 BSWAP(tmp);
|
|
191 #endif
|
|
192 ld->bufb = tmp;
|
|
193
|
|
194 ld->tail = (uint32_t*)buffer + index;
|
|
195
|
|
196 ld->bits_left = bits_in_buffer % 32;
|
|
197 if (ld->bits_left == 0)
|
|
198 ld->bits_left = 32;
|
|
199
|
|
200 ld->bytes_used = 0;
|
|
201 ld->no_more_reading = 0;
|
|
202 ld->error = 0;
|
|
203 }
|