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 **
|
|
25 ** $Id: bits.c,v 1.22 2003/07/29 08:20:12 menno Exp $
|
|
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
|
|
40 ld->buffer = malloc((buffer_size+12)*sizeof(uint8_t));
|
|
41 memset(ld->buffer, 0, (buffer_size+12)*sizeof(uint8_t));
|
|
42 memcpy(ld->buffer, _buffer, buffer_size*sizeof(uint8_t));
|
|
43
|
|
44 ld->buffer_size = buffer_size;
|
|
45
|
|
46 tmp = getdword((uint32_t*)ld->buffer);
|
|
47 #ifndef ARCH_IS_BIG_ENDIAN
|
|
48 BSWAP(tmp);
|
|
49 #endif
|
|
50 ld->bufa = tmp;
|
|
51
|
|
52 tmp = getdword((uint32_t*)ld->buffer + 1);
|
|
53 #ifndef ARCH_IS_BIG_ENDIAN
|
|
54 BSWAP(tmp);
|
|
55 #endif
|
|
56 ld->bufb = tmp;
|
|
57
|
|
58 ld->start = (uint32_t*)ld->buffer;
|
|
59 ld->tail = ((uint32_t*)ld->buffer + 2);
|
|
60
|
|
61 ld->bits_left = 32;
|
|
62
|
|
63 ld->bytes_used = 0;
|
|
64 ld->no_more_reading = 0;
|
|
65 ld->error = 0;
|
|
66 }
|
|
67
|
|
68 void faad_endbits(bitfile *ld)
|
|
69 {
|
|
70 if (ld)
|
|
71 if (ld->buffer) free(ld->buffer);
|
|
72 }
|
|
73
|
|
74
|
|
75 uint32_t faad_get_processed_bits(bitfile *ld)
|
|
76 {
|
|
77 return 8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left);
|
|
78 }
|
|
79
|
|
80 uint8_t faad_byte_align(bitfile *ld)
|
|
81 {
|
|
82 uint8_t remainder = (uint8_t)((32 - ld->bits_left) % 8);
|
|
83
|
|
84 if (remainder)
|
|
85 {
|
|
86 faad_flushbits(ld, 8 - remainder);
|
|
87 return (8 - remainder);
|
|
88 }
|
|
89 return 0;
|
|
90 }
|
|
91
|
|
92 /* rewind to beginning */
|
|
93 void faad_rewindbits(bitfile *ld)
|
|
94 {
|
|
95 uint32_t tmp;
|
|
96
|
|
97 tmp = ld->start[0];
|
|
98 #ifndef ARCH_IS_BIG_ENDIAN
|
|
99 BSWAP(tmp);
|
|
100 #endif
|
|
101 ld->bufa = tmp;
|
|
102
|
|
103 tmp = ld->start[1];
|
|
104 #ifndef ARCH_IS_BIG_ENDIAN
|
|
105 BSWAP(tmp);
|
|
106 #endif
|
|
107 ld->bufb = tmp;
|
|
108 ld->bits_left = 32;
|
|
109 ld->tail = &ld->start[2];
|
|
110 ld->bytes_used = 0;
|
|
111 ld->no_more_reading = 0;
|
|
112 }
|
|
113
|
|
114 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
|
|
115 DEBUGDEC)
|
|
116 {
|
|
117 uint16_t i;
|
|
118 uint8_t temp;
|
|
119 uint16_t bytes = (uint16_t)bits / 8;
|
|
120 uint8_t remainder = (uint8_t)bits % 8;
|
|
121
|
|
122 uint8_t *buffer = (uint8_t*)malloc((bytes+1)*sizeof(uint8_t));
|
|
123
|
|
124 for (i = 0; i < bytes; i++)
|
|
125 {
|
|
126 buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
|
|
127 }
|
|
128
|
|
129 if (remainder)
|
|
130 {
|
|
131 temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
|
|
132
|
|
133 buffer[bytes] = temp;
|
|
134 }
|
|
135
|
|
136 return buffer;
|
|
137 }
|
|
138
|
|
139 /* reversed bit reading routines, used for RVLC and HCR */
|
|
140 void faad_initbits_rev(bitfile *ld, void *buffer,
|
|
141 uint32_t bits_in_buffer)
|
|
142 {
|
|
143 uint32_t tmp;
|
|
144 int32_t index;
|
|
145
|
|
146 ld->buffer_size = bit2byte(bits_in_buffer);
|
|
147
|
|
148 index = (bits_in_buffer+31)/32 - 1;
|
|
149
|
|
150 ld->start = (uint32_t*)buffer + index - 2;
|
|
151
|
|
152 tmp = getdword((uint32_t*)buffer + index);
|
|
153 #ifndef ARCH_IS_BIG_ENDIAN
|
|
154 BSWAP(tmp);
|
|
155 #endif
|
|
156 ld->bufa = tmp;
|
|
157
|
|
158 tmp = getdword((uint32_t*)buffer + index - 1);
|
|
159 #ifndef ARCH_IS_BIG_ENDIAN
|
|
160 BSWAP(tmp);
|
|
161 #endif
|
|
162 ld->bufb = tmp;
|
|
163
|
|
164 ld->tail = (uint32_t*)buffer + index;
|
|
165
|
|
166 ld->bits_left = bits_in_buffer % 32;
|
|
167 if (ld->bits_left == 0)
|
|
168 ld->bits_left = 32;
|
|
169
|
|
170 ld->bytes_used = 0;
|
|
171 ld->no_more_reading = 0;
|
|
172 ld->error = 0;
|
|
173 }
|