Mercurial > mplayer.hg
annotate libfaad2/bits.c @ 12631:6cbccc0c7d7b
Fix memory corruption, noticable at reallocate image
author | iive |
---|---|
date | Thu, 24 Jun 2004 12:01:53 +0000 |
parents | d81145997036 |
children | 2ae5ab4331ca |
rev | line source |
---|---|
10725 | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | |
12527 | 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com |
10725 | 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 ** | |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
26 ** $Id: bits.c,v 1.3 2004/06/02 22:59:02 diego Exp $ |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
10725 | 28 **/ |
29 | |
30 #include "common.h" | |
31 #include "structs.h" | |
32 | |
33 #include <stdlib.h> | |
34 #include <string.h> | |
35 #include "bits.h" | |
36 | |
37 /* initialize buffer, call once before first getbits or showbits */ | |
12527 | 38 void faad_initbits(bitfile *ld, const void *_buffer, const uint32_t buffer_size) |
10725 | 39 { |
40 uint32_t tmp; | |
41 | |
10989 | 42 if (ld == NULL) |
43 return; | |
44 | |
45 memset(ld, 0, sizeof(bitfile)); | |
46 | |
47 if (buffer_size == 0 || _buffer == NULL) | |
48 { | |
49 ld->error = 1; | |
50 ld->no_more_reading = 1; | |
51 return; | |
52 } | |
53 | |
12527 | 54 ld->buffer = faad_malloc((buffer_size+12)*sizeof(uint8_t)); |
10725 | 55 memset(ld->buffer, 0, (buffer_size+12)*sizeof(uint8_t)); |
56 memcpy(ld->buffer, _buffer, buffer_size*sizeof(uint8_t)); | |
57 | |
58 ld->buffer_size = buffer_size; | |
59 | |
60 tmp = getdword((uint32_t*)ld->buffer); | |
61 ld->bufa = tmp; | |
62 | |
63 tmp = getdword((uint32_t*)ld->buffer + 1); | |
64 ld->bufb = tmp; | |
65 | |
66 ld->start = (uint32_t*)ld->buffer; | |
67 ld->tail = ((uint32_t*)ld->buffer + 2); | |
68 | |
69 ld->bits_left = 32; | |
70 | |
71 ld->bytes_used = 0; | |
72 ld->no_more_reading = 0; | |
73 ld->error = 0; | |
74 } | |
75 | |
76 void faad_endbits(bitfile *ld) | |
77 { | |
78 if (ld) | |
12527 | 79 { |
80 if (ld->buffer) | |
81 { | |
82 faad_free(ld->buffer); | |
83 ld->buffer = NULL; | |
84 } | |
85 } | |
10725 | 86 } |
87 | |
88 uint32_t faad_get_processed_bits(bitfile *ld) | |
89 { | |
10989 | 90 return (uint32_t)(8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left)); |
10725 | 91 } |
92 | |
93 uint8_t faad_byte_align(bitfile *ld) | |
94 { | |
95 uint8_t remainder = (uint8_t)((32 - ld->bits_left) % 8); | |
96 | |
97 if (remainder) | |
98 { | |
99 faad_flushbits(ld, 8 - remainder); | |
100 return (8 - remainder); | |
101 } | |
102 return 0; | |
103 } | |
104 | |
10989 | 105 void faad_flushbits_ex(bitfile *ld, uint32_t bits) |
106 { | |
107 uint32_t tmp; | |
108 | |
109 ld->bufa = ld->bufb; | |
12527 | 110 if (ld->no_more_reading == 0) |
111 { | |
112 tmp = getdword(ld->tail); | |
113 ld->tail++; | |
114 } else { | |
115 tmp = 0; | |
116 } | |
10989 | 117 ld->bufb = tmp; |
118 ld->bits_left += (32 - bits); | |
119 ld->bytes_used += 4; | |
120 if (ld->bytes_used == ld->buffer_size) | |
121 ld->no_more_reading = 1; | |
122 if (ld->bytes_used > ld->buffer_size) | |
123 ld->error = 1; | |
124 } | |
125 | |
10725 | 126 /* rewind to beginning */ |
127 void faad_rewindbits(bitfile *ld) | |
128 { | |
129 uint32_t tmp; | |
130 | |
131 tmp = ld->start[0]; | |
132 #ifndef ARCH_IS_BIG_ENDIAN | |
133 BSWAP(tmp); | |
134 #endif | |
135 ld->bufa = tmp; | |
136 | |
137 tmp = ld->start[1]; | |
138 #ifndef ARCH_IS_BIG_ENDIAN | |
139 BSWAP(tmp); | |
140 #endif | |
141 ld->bufb = tmp; | |
142 ld->bits_left = 32; | |
143 ld->tail = &ld->start[2]; | |
144 ld->bytes_used = 0; | |
145 ld->no_more_reading = 0; | |
146 } | |
147 | |
148 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits | |
149 DEBUGDEC) | |
150 { | |
151 uint16_t i; | |
152 uint8_t temp; | |
153 uint16_t bytes = (uint16_t)bits / 8; | |
154 uint8_t remainder = (uint8_t)bits % 8; | |
155 | |
12527 | 156 uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t)); |
10725 | 157 |
158 for (i = 0; i < bytes; i++) | |
159 { | |
160 buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg)); | |
161 } | |
162 | |
163 if (remainder) | |
164 { | |
165 temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder); | |
166 | |
167 buffer[bytes] = temp; | |
168 } | |
169 | |
170 return buffer; | |
171 } | |
172 | |
12527 | 173 #ifdef DRM |
174 /* return the original data buffer */ | |
175 void *faad_origbitbuffer(bitfile *ld) | |
176 { | |
177 return (void*)ld->start; | |
178 } | |
179 | |
180 /* return the original data buffer size */ | |
181 uint32_t faad_origbitbuffer_size(bitfile *ld) | |
182 { | |
183 return ld->buffer_size; | |
184 } | |
185 #endif | |
186 | |
10725 | 187 /* reversed bit reading routines, used for RVLC and HCR */ |
188 void faad_initbits_rev(bitfile *ld, void *buffer, | |
189 uint32_t bits_in_buffer) | |
190 { | |
191 uint32_t tmp; | |
192 int32_t index; | |
193 | |
194 ld->buffer_size = bit2byte(bits_in_buffer); | |
195 | |
196 index = (bits_in_buffer+31)/32 - 1; | |
197 | |
198 ld->start = (uint32_t*)buffer + index - 2; | |
199 | |
200 tmp = getdword((uint32_t*)buffer + index); | |
201 ld->bufa = tmp; | |
202 | |
203 tmp = getdword((uint32_t*)buffer + index - 1); | |
204 ld->bufb = tmp; | |
205 | |
206 ld->tail = (uint32_t*)buffer + index; | |
207 | |
208 ld->bits_left = bits_in_buffer % 32; | |
209 if (ld->bits_left == 0) | |
210 ld->bits_left = 32; | |
211 | |
212 ld->bytes_used = 0; | |
213 ld->no_more_reading = 0; | |
214 ld->error = 0; | |
215 } |