as3525 usb: build as3525v2 file based on CONFIG_USBOTG, and fix warnings
[kugel-rb.git] / apps / codecs / libfaad / bits.c
bloba010e830edb1ac6438c0cba14d16c1205e9fa046
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id$
26 **/
28 #include "common.h"
29 #include "structs.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include "bits.h"
35 /* Need to be large enough to fit the largest compressed sample in a file.
36 * Samples a little larger than 1 KB observed in a 256 kbps file.
38 uint8_t static_buffer[2048];
40 /* initialize buffer, call once before first getbits or showbits */
41 void faad_initbits(bitfile *ld, const void *_buffer, const uint32_t buffer_size)
43 uint32_t tmp;
45 if (ld == NULL)
46 return;
48 memset(ld, 0, sizeof(bitfile));
50 if (buffer_size == 0 || _buffer == NULL)
52 ld->error = 1;
53 ld->no_more_reading = 1;
54 return;
57 ld->buffer = &static_buffer;
58 memset(ld->buffer, 0, (buffer_size+12)*sizeof(uint8_t));
59 memcpy(ld->buffer, _buffer, buffer_size*sizeof(uint8_t));
61 ld->buffer_size = buffer_size;
63 tmp = getdword((uint32_t*)ld->buffer);
64 ld->bufa = tmp;
66 tmp = getdword((uint32_t*)ld->buffer + 1);
67 ld->bufb = tmp;
69 ld->start = (uint32_t*)ld->buffer;
70 ld->tail = ((uint32_t*)ld->buffer + 2);
72 ld->bits_left = 32;
74 ld->bytes_used = 0;
75 ld->no_more_reading = 0;
76 ld->error = 0;
79 void faad_endbits(bitfile *ld)
81 #if 0
82 if (ld)
84 if (ld->buffer)
86 faad_free(ld->buffer);
87 ld->buffer = NULL;
90 #else
91 (void) ld;
92 #endif
95 uint32_t faad_get_processed_bits(bitfile *ld)
97 return (uint32_t)(8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left));
100 uint8_t faad_byte_align(bitfile *ld)
102 uint8_t remainder = (uint8_t)((32 - ld->bits_left) % 8);
104 if (remainder)
106 faad_flushbits(ld, 8 - remainder);
107 return (8 - remainder);
109 return 0;
112 void faad_flushbits_ex(bitfile *ld, uint32_t bits)
114 uint32_t tmp;
116 ld->bufa = ld->bufb;
117 if (ld->no_more_reading == 0)
119 tmp = getdword(ld->tail);
120 ld->tail++;
121 } else {
122 tmp = 0;
124 ld->bufb = tmp;
125 ld->bits_left += (32 - bits);
126 ld->bytes_used += 4;
127 if (ld->bytes_used == ld->buffer_size)
128 ld->no_more_reading = 1;
129 if (ld->bytes_used > ld->buffer_size)
130 ld->error = 1;
133 /* rewind to beginning */
134 void faad_rewindbits(bitfile *ld)
136 uint32_t tmp;
138 tmp = ld->start[0];
139 #ifndef ARCH_IS_BIG_ENDIAN
140 BSWAP(tmp);
141 #endif
142 ld->bufa = tmp;
144 tmp = ld->start[1];
145 #ifndef ARCH_IS_BIG_ENDIAN
146 BSWAP(tmp);
147 #endif
148 ld->bufb = tmp;
149 ld->bits_left = 32;
150 ld->tail = &ld->start[2];
151 ld->bytes_used = 0;
152 ld->no_more_reading = 0;
155 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
156 DEBUGDEC)
158 uint16_t i;
159 uint8_t temp;
160 uint16_t bytes = (uint16_t)bits / 8;
161 uint8_t remainder = (uint8_t)bits % 8;
163 uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t));
165 for (i = 0; i < bytes; i++)
167 buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
170 if (remainder)
172 temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
174 buffer[bytes] = temp;
177 return buffer;
180 #ifdef DRM
181 /* return the original data buffer */
182 void *faad_origbitbuffer(bitfile *ld)
184 return (void*)ld->start;
187 /* return the original data buffer size */
188 uint32_t faad_origbitbuffer_size(bitfile *ld)
190 return ld->buffer_size;
192 #endif
194 /* reversed bit reading routines, used for RVLC and HCR */
195 void faad_initbits_rev(bitfile *ld, void *buffer,
196 uint32_t bits_in_buffer)
198 uint32_t tmp;
199 int32_t index;
201 ld->buffer_size = bit2byte(bits_in_buffer);
203 index = (bits_in_buffer+31)/32 - 1;
205 ld->start = (uint32_t*)buffer + index - 2;
207 tmp = getdword((uint32_t*)buffer + index);
208 ld->bufa = tmp;
210 tmp = getdword((uint32_t*)buffer + index - 1);
211 ld->bufb = tmp;
213 ld->tail = (uint32_t*)buffer + index;
215 ld->bits_left = bits_in_buffer % 32;
216 if (ld->bits_left == 0)
217 ld->bits_left = 32;
219 ld->bytes_used = 0;
220 ld->no_more_reading = 0;
221 ld->error = 0;