/src/aac/libFDK/src/FDK_bitbuffer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* ----------------------------------------------------------------------------- |
2 | | Software License for The Fraunhofer FDK AAC Codec Library for Android |
3 | | |
4 | | © Copyright 1995 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten |
5 | | Forschung e.V. All rights reserved. |
6 | | |
7 | | 1. INTRODUCTION |
8 | | The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software |
9 | | that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding |
10 | | scheme for digital audio. This FDK AAC Codec software is intended to be used on |
11 | | a wide variety of Android devices. |
12 | | |
13 | | AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient |
14 | | general perceptual audio codecs. AAC-ELD is considered the best-performing |
15 | | full-bandwidth communications codec by independent studies and is widely |
16 | | deployed. AAC has been standardized by ISO and IEC as part of the MPEG |
17 | | specifications. |
18 | | |
19 | | Patent licenses for necessary patent claims for the FDK AAC Codec (including |
20 | | those of Fraunhofer) may be obtained through Via Licensing |
21 | | (www.vialicensing.com) or through the respective patent owners individually for |
22 | | the purpose of encoding or decoding bit streams in products that are compliant |
23 | | with the ISO/IEC MPEG audio standards. Please note that most manufacturers of |
24 | | Android devices already license these patent claims through Via Licensing or |
25 | | directly from the patent owners, and therefore FDK AAC Codec software may |
26 | | already be covered under those patent licenses when it is used for those |
27 | | licensed purposes only. |
28 | | |
29 | | Commercially-licensed AAC software libraries, including floating-point versions |
30 | | with enhanced sound quality, are also available from Fraunhofer. Users are |
31 | | encouraged to check the Fraunhofer website for additional applications |
32 | | information and documentation. |
33 | | |
34 | | 2. COPYRIGHT LICENSE |
35 | | |
36 | | Redistribution and use in source and binary forms, with or without modification, |
37 | | are permitted without payment of copyright license fees provided that you |
38 | | satisfy the following conditions: |
39 | | |
40 | | You must retain the complete text of this software license in redistributions of |
41 | | the FDK AAC Codec or your modifications thereto in source code form. |
42 | | |
43 | | You must retain the complete text of this software license in the documentation |
44 | | and/or other materials provided with redistributions of the FDK AAC Codec or |
45 | | your modifications thereto in binary form. You must make available free of |
46 | | charge copies of the complete source code of the FDK AAC Codec and your |
47 | | modifications thereto to recipients of copies in binary form. |
48 | | |
49 | | The name of Fraunhofer may not be used to endorse or promote products derived |
50 | | from this library without prior written permission. |
51 | | |
52 | | You may not charge copyright license fees for anyone to use, copy or distribute |
53 | | the FDK AAC Codec software or your modifications thereto. |
54 | | |
55 | | Your modified versions of the FDK AAC Codec must carry prominent notices stating |
56 | | that you changed the software and the date of any change. For modified versions |
57 | | of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" |
58 | | must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK |
59 | | AAC Codec Library for Android." |
60 | | |
61 | | 3. NO PATENT LICENSE |
62 | | |
63 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without |
64 | | limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. |
65 | | Fraunhofer provides no warranty of patent non-infringement with respect to this |
66 | | software. |
67 | | |
68 | | You may use this FDK AAC Codec software or modifications thereto only for |
69 | | purposes that are authorized by appropriate patent licenses. |
70 | | |
71 | | 4. DISCLAIMER |
72 | | |
73 | | This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright |
74 | | holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, |
75 | | including but not limited to the implied warranties of merchantability and |
76 | | fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
77 | | CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, |
78 | | or consequential damages, including but not limited to procurement of substitute |
79 | | goods or services; loss of use, data, or profits, or business interruption, |
80 | | however caused and on any theory of liability, whether in contract, strict |
81 | | liability, or tort (including negligence), arising in any way out of the use of |
82 | | this software, even if advised of the possibility of such damage. |
83 | | |
84 | | 5. CONTACT INFORMATION |
85 | | |
86 | | Fraunhofer Institute for Integrated Circuits IIS |
87 | | Attention: Audio and Multimedia Departments - FDK AAC LL |
88 | | Am Wolfsmantel 33 |
89 | | 91058 Erlangen, Germany |
90 | | |
91 | | www.iis.fraunhofer.de/amm |
92 | | amm-info@iis.fraunhofer.de |
93 | | ----------------------------------------------------------------------------- */ |
94 | | |
95 | | /******************* Library for basic calculation routines ******************** |
96 | | |
97 | | Author(s): M. Lohwasser |
98 | | |
99 | | Description: common bitbuffer read/write routines |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "FDK_bitbuffer.h" |
104 | | |
105 | | #include "genericStds.h" |
106 | | #include "common_fix.h" |
107 | | #include "fixminmax.h" |
108 | | |
109 | | const UINT BitMask[32 + 1] = { |
110 | | 0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, |
111 | | 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff, 0x7ff, |
112 | | 0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff, 0x1ffff, |
113 | | 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, |
114 | | 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, |
115 | | 0x3fffffff, 0x7fffffff, 0xffffffff}; |
116 | | |
117 | | void FDK_CreateBitBuffer(HANDLE_FDK_BITBUF *hBitBuf, UCHAR *pBuffer, |
118 | 0 | UINT bufSize) { |
119 | 0 | FDK_InitBitBuffer(*hBitBuf, pBuffer, bufSize, 0); |
120 | |
|
121 | 0 | FDKmemclear((*hBitBuf)->Buffer, bufSize * sizeof(UCHAR)); |
122 | 0 | } |
123 | | |
124 | 0 | void FDK_DeleteBitBuffer(HANDLE_FDK_BITBUF hBitBuf) { ; } |
125 | | |
126 | | void FDK_InitBitBuffer(HANDLE_FDK_BITBUF hBitBuf, UCHAR *pBuffer, UINT bufSize, |
127 | 24.8k | UINT validBits) { |
128 | 24.8k | hBitBuf->ValidBits = validBits; |
129 | 24.8k | hBitBuf->ReadOffset = 0; |
130 | 24.8k | hBitBuf->WriteOffset = 0; |
131 | 24.8k | hBitBuf->BitNdx = 0; |
132 | | |
133 | 24.8k | hBitBuf->Buffer = pBuffer; |
134 | 24.8k | hBitBuf->bufSize = bufSize; |
135 | 24.8k | hBitBuf->bufBits = (bufSize << 3); |
136 | | /*assure bufsize (2^n) */ |
137 | 24.8k | FDK_ASSERT(hBitBuf->ValidBits <= hBitBuf->bufBits); |
138 | 24.8k | FDK_ASSERT((bufSize > 0) && (bufSize <= MAX_BUFSIZE_BYTES)); |
139 | 24.8k | { |
140 | 24.8k | UINT x = 0, n = bufSize; |
141 | 295k | for (x = 0; n > 0; x++, n >>= 1) { |
142 | 270k | } |
143 | 24.8k | if (bufSize != ((UINT)1 << (x - 1))) { |
144 | 0 | FDK_ASSERT(0); |
145 | 0 | } |
146 | 24.8k | } |
147 | 24.8k | } |
148 | | |
149 | 0 | void FDK_ResetBitBuffer(HANDLE_FDK_BITBUF hBitBuf) { |
150 | 0 | hBitBuf->ValidBits = 0; |
151 | 0 | hBitBuf->ReadOffset = 0; |
152 | 0 | hBitBuf->WriteOffset = 0; |
153 | 0 | hBitBuf->BitNdx = 0; |
154 | 0 | } |
155 | | |
156 | | #ifndef FUNCTION_FDK_get |
157 | 0 | INT FDK_get(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits) { |
158 | 0 | UINT byteOffset = hBitBuf->BitNdx >> 3; |
159 | 0 | UINT bitOffset = hBitBuf->BitNdx & 0x07; |
160 | |
|
161 | 0 | hBitBuf->BitNdx = (hBitBuf->BitNdx + numberOfBits) & (hBitBuf->bufBits - 1); |
162 | 0 | hBitBuf->ValidBits -= numberOfBits; |
163 | |
|
164 | 0 | UINT byteMask = hBitBuf->bufSize - 1; |
165 | |
|
166 | 0 | UINT tx = (hBitBuf->Buffer[byteOffset & byteMask] << 24) | |
167 | 0 | (hBitBuf->Buffer[(byteOffset + 1) & byteMask] << 16) | |
168 | 0 | (hBitBuf->Buffer[(byteOffset + 2) & byteMask] << 8) | |
169 | 0 | hBitBuf->Buffer[(byteOffset + 3) & byteMask]; |
170 | |
|
171 | 0 | if (bitOffset) { |
172 | 0 | tx <<= bitOffset; |
173 | 0 | tx |= hBitBuf->Buffer[(byteOffset + 4) & byteMask] >> (8 - bitOffset); |
174 | 0 | } |
175 | |
|
176 | 0 | return (tx >> (32 - numberOfBits)); |
177 | 0 | } |
178 | | #endif /* #ifndef FUNCTION_FDK_get */ |
179 | | |
180 | | #ifndef FUNCTION_FDK_get32 |
181 | 3.78M | INT FDK_get32(HANDLE_FDK_BITBUF hBitBuf) { |
182 | 3.78M | UINT BitNdx = hBitBuf->BitNdx + 32; |
183 | 3.78M | hBitBuf->BitNdx = BitNdx & (hBitBuf->bufBits - 1); |
184 | 3.78M | hBitBuf->ValidBits = (UINT)((INT)hBitBuf->ValidBits - (INT)32); |
185 | | |
186 | 3.78M | UINT byteOffset = (BitNdx - 1) >> 3; |
187 | 3.78M | if (BitNdx <= hBitBuf->bufBits) { |
188 | 3.77M | UINT cache = (hBitBuf->Buffer[(byteOffset - 3)] << 24) | |
189 | 3.77M | (hBitBuf->Buffer[(byteOffset - 2)] << 16) | |
190 | 3.77M | (hBitBuf->Buffer[(byteOffset - 1)] << 8) | |
191 | 3.77M | hBitBuf->Buffer[(byteOffset - 0)]; |
192 | | |
193 | 3.77M | if ((BitNdx = (BitNdx & 7)) != 0) { |
194 | 910k | cache = (cache >> (8 - BitNdx)) | |
195 | 910k | ((UINT)hBitBuf->Buffer[byteOffset - 4] << (24 + BitNdx)); |
196 | 910k | } |
197 | 3.77M | return (cache); |
198 | 3.77M | } else { |
199 | 11.4k | UINT byte_mask = hBitBuf->bufSize - 1; |
200 | 11.4k | UINT cache = (hBitBuf->Buffer[(byteOffset - 3) & byte_mask] << 24) | |
201 | 11.4k | (hBitBuf->Buffer[(byteOffset - 2) & byte_mask] << 16) | |
202 | 11.4k | (hBitBuf->Buffer[(byteOffset - 1) & byte_mask] << 8) | |
203 | 11.4k | hBitBuf->Buffer[(byteOffset - 0) & byte_mask]; |
204 | | |
205 | 11.4k | if ((BitNdx = (BitNdx & 7)) != 0) { |
206 | 1.86k | cache = (cache >> (8 - BitNdx)) | |
207 | 1.86k | ((UINT)hBitBuf->Buffer[(byteOffset - 4) & byte_mask] |
208 | 1.86k | << (24 + BitNdx)); |
209 | 1.86k | } |
210 | 11.4k | return (cache); |
211 | 11.4k | } |
212 | 3.78M | } |
213 | | #endif |
214 | | |
215 | 0 | INT FDK_getBwd(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits) { |
216 | 0 | UINT byteOffset = hBitBuf->BitNdx >> 3; |
217 | 0 | UINT bitOffset = hBitBuf->BitNdx & 0x07; |
218 | 0 | UINT byteMask = hBitBuf->bufSize - 1; |
219 | 0 | int i; |
220 | |
|
221 | 0 | hBitBuf->BitNdx = (hBitBuf->BitNdx - numberOfBits) & (hBitBuf->bufBits - 1); |
222 | 0 | hBitBuf->ValidBits += numberOfBits; |
223 | |
|
224 | 0 | UINT tx = hBitBuf->Buffer[(byteOffset - 3) & byteMask] << 24 | |
225 | 0 | hBitBuf->Buffer[(byteOffset - 2) & byteMask] << 16 | |
226 | 0 | hBitBuf->Buffer[(byteOffset - 1) & byteMask] << 8 | |
227 | 0 | hBitBuf->Buffer[byteOffset & byteMask]; |
228 | 0 | UINT txa = 0x0; |
229 | |
|
230 | 0 | tx >>= (8 - bitOffset); |
231 | |
|
232 | 0 | if (bitOffset && numberOfBits > 24) { |
233 | 0 | tx |= hBitBuf->Buffer[(byteOffset - 4) & byteMask] << (24 + bitOffset); |
234 | 0 | } |
235 | | |
236 | | /* in place turn around */ |
237 | 0 | for (i = 0; i < 16; i++) { |
238 | 0 | UINT bitMaskR = 0x00000001 << i; |
239 | 0 | UINT bitMaskL = 0x80000000 >> i; |
240 | |
|
241 | 0 | txa |= (tx & bitMaskR) << (31 - (i << 1)); |
242 | 0 | txa |= (tx & bitMaskL) >> (31 - (i << 1)); |
243 | 0 | } |
244 | |
|
245 | 0 | return (txa >> (32 - numberOfBits)); |
246 | 0 | } |
247 | | |
248 | 117k | void FDK_put(HANDLE_FDK_BITBUF hBitBuf, UINT value, const UINT numberOfBits) { |
249 | 117k | if (numberOfBits != 0) { |
250 | 117k | UINT byteOffset0 = hBitBuf->BitNdx >> 3; |
251 | 117k | UINT bitOffset = hBitBuf->BitNdx & 0x7; |
252 | | |
253 | 117k | hBitBuf->BitNdx = (hBitBuf->BitNdx + numberOfBits) & (hBitBuf->bufBits - 1); |
254 | 117k | hBitBuf->ValidBits += numberOfBits; |
255 | | |
256 | 117k | UINT byteMask = hBitBuf->bufSize - 1; |
257 | | |
258 | 117k | UINT byteOffset1 = (byteOffset0 + 1) & byteMask; |
259 | 117k | UINT byteOffset2 = (byteOffset0 + 2) & byteMask; |
260 | 117k | UINT byteOffset3 = (byteOffset0 + 3) & byteMask; |
261 | | |
262 | | // Create tmp containing free bits at the left border followed by bits to |
263 | | // write, LSB's are cleared, if available Create mask to apply upon all |
264 | | // buffer bytes |
265 | 117k | UINT tmp = (value << (32 - numberOfBits)) >> bitOffset; |
266 | 117k | UINT mask = ~((BitMask[numberOfBits] << (32 - numberOfBits)) >> bitOffset); |
267 | | |
268 | | // read all 4 bytes from buffer and create a 32-bit cache |
269 | 117k | UINT cache = (((UINT)hBitBuf->Buffer[byteOffset0]) << 24) | |
270 | 117k | (((UINT)hBitBuf->Buffer[byteOffset1]) << 16) | |
271 | 117k | (((UINT)hBitBuf->Buffer[byteOffset2]) << 8) | |
272 | 117k | (((UINT)hBitBuf->Buffer[byteOffset3]) << 0); |
273 | | |
274 | 117k | cache = (cache & mask) | tmp; |
275 | 117k | hBitBuf->Buffer[byteOffset0] = (UCHAR)(cache >> 24); |
276 | 117k | hBitBuf->Buffer[byteOffset1] = (UCHAR)(cache >> 16); |
277 | 117k | hBitBuf->Buffer[byteOffset2] = (UCHAR)(cache >> 8); |
278 | 117k | hBitBuf->Buffer[byteOffset3] = (UCHAR)(cache >> 0); |
279 | | |
280 | 117k | if ((bitOffset + numberOfBits) > 32) { |
281 | 0 | UINT byteOffset4 = (byteOffset0 + 4) & byteMask; |
282 | | // remaining bits: in range 1..7 |
283 | | // replace MSBits of next byte in buffer by LSBits of "value" |
284 | 0 | int bits = (bitOffset + numberOfBits) & 7; |
285 | 0 | cache = |
286 | 0 | (UINT)hBitBuf->Buffer[byteOffset4] & (~(BitMask[bits] << (8 - bits))); |
287 | 0 | cache |= value << (8 - bits); |
288 | 0 | hBitBuf->Buffer[byteOffset4] = (UCHAR)cache; |
289 | 0 | } |
290 | 117k | } |
291 | 117k | } |
292 | | |
293 | | void FDK_putBwd(HANDLE_FDK_BITBUF hBitBuf, UINT value, |
294 | 0 | const UINT numberOfBits) { |
295 | 0 | UINT byteOffset = hBitBuf->BitNdx >> 3; |
296 | 0 | UINT bitOffset = 7 - (hBitBuf->BitNdx & 0x07); |
297 | 0 | UINT byteMask = hBitBuf->bufSize - 1; |
298 | |
|
299 | 0 | UINT mask = ~(BitMask[numberOfBits] << bitOffset); |
300 | 0 | UINT tmp = 0x0000; |
301 | 0 | int i; |
302 | |
|
303 | 0 | hBitBuf->BitNdx = (hBitBuf->BitNdx - numberOfBits) & (hBitBuf->bufBits - 1); |
304 | 0 | hBitBuf->ValidBits -= numberOfBits; |
305 | | |
306 | | /* in place turn around */ |
307 | 0 | for (i = 0; i < 16; i++) { |
308 | 0 | UINT bitMaskR = 0x00000001 << i; |
309 | 0 | UINT bitMaskL = 0x80000000 >> i; |
310 | |
|
311 | 0 | tmp |= (value & bitMaskR) << (31 - (i << 1)); |
312 | 0 | tmp |= (value & bitMaskL) >> (31 - (i << 1)); |
313 | 0 | } |
314 | 0 | value = tmp; |
315 | 0 | tmp = value >> (32 - numberOfBits) << bitOffset; |
316 | |
|
317 | 0 | hBitBuf->Buffer[byteOffset & byteMask] = |
318 | 0 | (hBitBuf->Buffer[byteOffset & byteMask] & (mask)) | (UCHAR)(tmp); |
319 | 0 | hBitBuf->Buffer[(byteOffset - 1) & byteMask] = |
320 | 0 | (hBitBuf->Buffer[(byteOffset - 1) & byteMask] & (mask >> 8)) | |
321 | 0 | (UCHAR)(tmp >> 8); |
322 | 0 | hBitBuf->Buffer[(byteOffset - 2) & byteMask] = |
323 | 0 | (hBitBuf->Buffer[(byteOffset - 2) & byteMask] & (mask >> 16)) | |
324 | 0 | (UCHAR)(tmp >> 16); |
325 | 0 | hBitBuf->Buffer[(byteOffset - 3) & byteMask] = |
326 | 0 | (hBitBuf->Buffer[(byteOffset - 3) & byteMask] & (mask >> 24)) | |
327 | 0 | (UCHAR)(tmp >> 24); |
328 | |
|
329 | 0 | if ((bitOffset + numberOfBits) > 32) { |
330 | 0 | hBitBuf->Buffer[(byteOffset - 4) & byteMask] = |
331 | 0 | (UCHAR)(value >> (64 - numberOfBits - bitOffset)) | |
332 | 0 | (hBitBuf->Buffer[(byteOffset - 4) & byteMask] & |
333 | 0 | ~(BitMask[bitOffset] >> (32 - numberOfBits))); |
334 | 0 | } |
335 | 0 | } |
336 | | |
337 | | #ifndef FUNCTION_FDK_pushBack |
338 | | void FDK_pushBack(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits, |
339 | 563k | UCHAR config) { |
340 | 563k | hBitBuf->ValidBits = |
341 | 563k | (config == 0) ? (UINT)((INT)hBitBuf->ValidBits + (INT)numberOfBits) |
342 | 563k | : ((UINT)((INT)hBitBuf->ValidBits - (INT)numberOfBits)); |
343 | 563k | hBitBuf->BitNdx = ((UINT)((INT)hBitBuf->BitNdx - (INT)numberOfBits)) & |
344 | 563k | (hBitBuf->bufBits - 1); |
345 | 563k | } |
346 | | #endif |
347 | | |
348 | | void FDK_pushForward(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits, |
349 | 337k | UCHAR config) { |
350 | 337k | hBitBuf->ValidBits = |
351 | 337k | (config == 0) ? ((UINT)((INT)hBitBuf->ValidBits - (INT)numberOfBits)) |
352 | 337k | : (UINT)((INT)hBitBuf->ValidBits + (INT)numberOfBits); |
353 | 337k | hBitBuf->BitNdx = |
354 | 337k | (UINT)((INT)hBitBuf->BitNdx + (INT)numberOfBits) & (hBitBuf->bufBits - 1); |
355 | 337k | } |
356 | | |
357 | | #ifndef FUNCTION_FDK_getValidBits |
358 | 206k | UINT FDK_getValidBits(HANDLE_FDK_BITBUF hBitBuf) { return hBitBuf->ValidBits; } |
359 | | #endif /* #ifndef FUNCTION_FDK_getValidBits */ |
360 | | |
361 | 0 | INT FDK_getFreeBits(HANDLE_FDK_BITBUF hBitBuf) { |
362 | 0 | return (hBitBuf->bufBits - hBitBuf->ValidBits); |
363 | 0 | } |
364 | | |
365 | | void FDK_Feed(HANDLE_FDK_BITBUF hBitBuf, const UCHAR *RESTRICT inputBuffer, |
366 | 0 | const UINT bufferSize, UINT *bytesValid) { |
367 | 0 | inputBuffer = &inputBuffer[bufferSize - *bytesValid]; |
368 | |
|
369 | 0 | UINT bTotal = 0; |
370 | |
|
371 | 0 | UINT bToRead = |
372 | 0 | fMin(hBitBuf->bufBits, |
373 | 0 | (UINT)fMax(0, ((INT)hBitBuf->bufBits - (INT)hBitBuf->ValidBits))) >> |
374 | 0 | 3; |
375 | 0 | UINT noOfBytes = |
376 | 0 | fMin(bToRead, |
377 | 0 | *bytesValid); //(bToRead < *bytesValid) ? bToRead : *bytesValid ; |
378 | |
|
379 | 0 | while (noOfBytes > 0) { |
380 | | /* split read to buffer size */ |
381 | 0 | bToRead = hBitBuf->bufSize - hBitBuf->ReadOffset; |
382 | 0 | bToRead = fMin(bToRead, |
383 | 0 | noOfBytes); //(bToRead < noOfBytes) ? bToRead : noOfBytes ; |
384 | | |
385 | | /* copy 'bToRead' bytes from 'ptr' to inputbuffer */ |
386 | 0 | FDKmemcpy(&hBitBuf->Buffer[hBitBuf->ReadOffset], inputBuffer, |
387 | 0 | bToRead * sizeof(UCHAR)); |
388 | | |
389 | | /* add noOfBits to number of valid bits in buffer */ |
390 | 0 | hBitBuf->ValidBits = (UINT)((INT)hBitBuf->ValidBits + (INT)(bToRead << 3)); |
391 | 0 | bTotal += bToRead; |
392 | 0 | inputBuffer += bToRead; |
393 | |
|
394 | 0 | hBitBuf->ReadOffset = |
395 | 0 | (hBitBuf->ReadOffset + bToRead) & (hBitBuf->bufSize - 1); |
396 | 0 | noOfBytes -= bToRead; |
397 | 0 | } |
398 | |
|
399 | 0 | *bytesValid -= bTotal; |
400 | 0 | } |
401 | | |
402 | | void CopyAlignedBlock(HANDLE_FDK_BITBUF h_BitBufSrc, UCHAR *RESTRICT dstBuffer, |
403 | 0 | UINT bToRead) { |
404 | 0 | UINT byteOffset = h_BitBufSrc->BitNdx >> 3; |
405 | 0 | const UINT byteMask = h_BitBufSrc->bufSize - 1; |
406 | |
|
407 | 0 | UCHAR *RESTRICT pBBB = h_BitBufSrc->Buffer; |
408 | 0 | for (UINT i = 0; i < bToRead; i++) { |
409 | 0 | dstBuffer[i] = pBBB[(byteOffset + i) & byteMask]; |
410 | 0 | } |
411 | |
|
412 | 0 | bToRead <<= 3; |
413 | |
|
414 | 0 | h_BitBufSrc->BitNdx = |
415 | 0 | (h_BitBufSrc->BitNdx + bToRead) & (h_BitBufSrc->bufBits - 1); |
416 | 0 | h_BitBufSrc->ValidBits -= bToRead; |
417 | 0 | } |
418 | | |
419 | | void FDK_Copy(HANDLE_FDK_BITBUF h_BitBufDst, HANDLE_FDK_BITBUF h_BitBufSrc, |
420 | 0 | UINT *bytesValid) { |
421 | 0 | INT bTotal = 0; |
422 | | |
423 | | /* limit noOfBytes to valid bytes in src buffer and available bytes in dst |
424 | | * buffer */ |
425 | 0 | UINT bToRead = h_BitBufSrc->ValidBits >> 3; |
426 | 0 | UINT noOfBytes = |
427 | 0 | fMin(bToRead, |
428 | 0 | *bytesValid); //(*bytesValid < bToRead) ? *bytesValid : bToRead ; |
429 | 0 | bToRead = FDK_getFreeBits(h_BitBufDst); |
430 | 0 | noOfBytes = |
431 | 0 | fMin(bToRead, noOfBytes); //(bToRead < noOfBytes) ? bToRead : noOfBytes; |
432 | |
|
433 | 0 | while (noOfBytes > 0) { |
434 | | /* Split Read to buffer size */ |
435 | 0 | bToRead = h_BitBufDst->bufSize - h_BitBufDst->ReadOffset; |
436 | 0 | bToRead = fMin(noOfBytes, |
437 | 0 | bToRead); //(noOfBytes < bToRead) ? noOfBytes : bToRead ; |
438 | | |
439 | | /* copy 'bToRead' bytes from buffer to buffer */ |
440 | 0 | if (!(h_BitBufSrc->BitNdx & 0x07)) { |
441 | 0 | CopyAlignedBlock(h_BitBufSrc, |
442 | 0 | h_BitBufDst->Buffer + h_BitBufDst->ReadOffset, bToRead); |
443 | 0 | } else { |
444 | 0 | for (UINT i = 0; i < bToRead; i++) { |
445 | 0 | h_BitBufDst->Buffer[h_BitBufDst->ReadOffset + i] = |
446 | 0 | (UCHAR)FDK_get(h_BitBufSrc, 8); |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | | /* add noOfBits to number of valid bits in buffer */ |
451 | 0 | h_BitBufDst->ValidBits += bToRead << 3; |
452 | 0 | bTotal += bToRead; |
453 | |
|
454 | 0 | h_BitBufDst->ReadOffset = |
455 | 0 | (h_BitBufDst->ReadOffset + bToRead) & (h_BitBufDst->bufSize - 1); |
456 | 0 | noOfBytes -= bToRead; |
457 | 0 | } |
458 | |
|
459 | 0 | *bytesValid -= bTotal; |
460 | 0 | } |
461 | | |
462 | 0 | void FDK_Fetch(HANDLE_FDK_BITBUF hBitBuf, UCHAR *outBuf, UINT *writeBytes) { |
463 | 0 | UCHAR *RESTRICT outputBuffer = outBuf; |
464 | 0 | UINT bTotal = 0; |
465 | |
|
466 | 0 | UINT bToWrite = (hBitBuf->ValidBits) >> 3; |
467 | 0 | UINT noOfBytes = |
468 | 0 | fMin(bToWrite, |
469 | 0 | *writeBytes); //(bToWrite < *writeBytes) ? bToWrite : *writeBytes ; |
470 | |
|
471 | 0 | while (noOfBytes > 0) { |
472 | | /* split write to buffer size */ |
473 | 0 | bToWrite = hBitBuf->bufSize - hBitBuf->WriteOffset; |
474 | 0 | bToWrite = fMin( |
475 | 0 | bToWrite, noOfBytes); //(bToWrite < noOfBytes) ? bToWrite : noOfBytes ; |
476 | | |
477 | | /* copy 'bToWrite' bytes from bitbuffer to outputbuffer */ |
478 | 0 | FDKmemcpy(outputBuffer, &hBitBuf->Buffer[hBitBuf->WriteOffset], |
479 | 0 | bToWrite * sizeof(UCHAR)); |
480 | | |
481 | | /* sub noOfBits from number of valid bits in buffer */ |
482 | 0 | hBitBuf->ValidBits -= bToWrite << 3; |
483 | 0 | bTotal += bToWrite; |
484 | 0 | outputBuffer += bToWrite; |
485 | |
|
486 | 0 | hBitBuf->WriteOffset = |
487 | 0 | (hBitBuf->WriteOffset + bToWrite) & (hBitBuf->bufSize - 1); |
488 | 0 | noOfBytes -= bToWrite; |
489 | 0 | } |
490 | |
|
491 | 0 | *writeBytes = bTotal; |
492 | 0 | } |