1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4 © Copyright 1995 - 2018 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
FDK_CreateBitBuffer(HANDLE_FDK_BITBUF * hBitBuf,UCHAR * pBuffer,UINT bufSize)117 void FDK_CreateBitBuffer(HANDLE_FDK_BITBUF *hBitBuf, UCHAR *pBuffer,
118 UINT bufSize) {
119 FDK_InitBitBuffer(*hBitBuf, pBuffer, bufSize, 0);
120
121 FDKmemclear((*hBitBuf)->Buffer, bufSize * sizeof(UCHAR));
122 }
123
FDK_DeleteBitBuffer(HANDLE_FDK_BITBUF hBitBuf)124 void FDK_DeleteBitBuffer(HANDLE_FDK_BITBUF hBitBuf) { ; }
125
FDK_InitBitBuffer(HANDLE_FDK_BITBUF hBitBuf,UCHAR * pBuffer,UINT bufSize,UINT validBits)126 void FDK_InitBitBuffer(HANDLE_FDK_BITBUF hBitBuf, UCHAR *pBuffer, UINT bufSize,
127 UINT validBits) {
128 hBitBuf->ValidBits = validBits;
129 hBitBuf->ReadOffset = 0;
130 hBitBuf->WriteOffset = 0;
131 hBitBuf->BitCnt = 0;
132 hBitBuf->BitNdx = 0;
133
134 hBitBuf->Buffer = pBuffer;
135 hBitBuf->bufSize = bufSize;
136 hBitBuf->bufBits = (bufSize << 3);
137 /*assure bufsize (2^n) */
138 FDK_ASSERT(hBitBuf->ValidBits <= hBitBuf->bufBits);
139 FDK_ASSERT((bufSize > 0) && (bufSize <= MAX_BUFSIZE_BYTES));
140 {
141 UINT x = 0, n = bufSize;
142 for (x = 0; n > 0; x++, n >>= 1) {
143 }
144 if (bufSize != ((UINT)1 << (x - 1))) {
145 FDK_ASSERT(0);
146 }
147 }
148 }
149
FDK_ResetBitBuffer(HANDLE_FDK_BITBUF hBitBuf)150 void FDK_ResetBitBuffer(HANDLE_FDK_BITBUF hBitBuf) {
151 hBitBuf->ValidBits = 0;
152 hBitBuf->ReadOffset = 0;
153 hBitBuf->WriteOffset = 0;
154 hBitBuf->BitCnt = 0;
155 hBitBuf->BitNdx = 0;
156 }
157
158 #ifndef FUNCTION_FDK_get
FDK_get(HANDLE_FDK_BITBUF hBitBuf,const UINT numberOfBits)159 INT FDK_get(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits) {
160 UINT byteOffset = hBitBuf->BitNdx >> 3;
161 UINT bitOffset = hBitBuf->BitNdx & 0x07;
162
163 hBitBuf->BitNdx = (hBitBuf->BitNdx + numberOfBits) & (hBitBuf->bufBits - 1);
164 hBitBuf->BitCnt += numberOfBits;
165 hBitBuf->ValidBits -= numberOfBits;
166
167 UINT byteMask = hBitBuf->bufSize - 1;
168
169 UINT tx = (hBitBuf->Buffer[byteOffset & byteMask] << 24) |
170 (hBitBuf->Buffer[(byteOffset + 1) & byteMask] << 16) |
171 (hBitBuf->Buffer[(byteOffset + 2) & byteMask] << 8) |
172 hBitBuf->Buffer[(byteOffset + 3) & byteMask];
173
174 if (bitOffset) {
175 tx <<= bitOffset;
176 tx |= hBitBuf->Buffer[(byteOffset + 4) & byteMask] >> (8 - bitOffset);
177 }
178
179 return (tx >> (32 - numberOfBits));
180 }
181 #endif /* #ifndef FUNCTION_FDK_get */
182
183 #ifndef FUNCTION_FDK_get32
FDK_get32(HANDLE_FDK_BITBUF hBitBuf)184 INT FDK_get32(HANDLE_FDK_BITBUF hBitBuf) {
185 UINT BitNdx = hBitBuf->BitNdx + 32;
186 hBitBuf->BitNdx = BitNdx & (hBitBuf->bufBits - 1);
187 hBitBuf->BitCnt += 32;
188 hBitBuf->ValidBits = (UINT)((INT)hBitBuf->ValidBits - (INT)32);
189
190 UINT byteOffset = (BitNdx - 1) >> 3;
191 if (BitNdx <= hBitBuf->bufBits) {
192 UINT cache = (hBitBuf->Buffer[(byteOffset - 3)] << 24) |
193 (hBitBuf->Buffer[(byteOffset - 2)] << 16) |
194 (hBitBuf->Buffer[(byteOffset - 1)] << 8) |
195 hBitBuf->Buffer[(byteOffset - 0)];
196
197 if ((BitNdx = (BitNdx & 7)) != 0) {
198 cache = (cache >> (8 - BitNdx)) |
199 ((UINT)hBitBuf->Buffer[byteOffset - 4] << (24 + BitNdx));
200 }
201 return (cache);
202 } else {
203 UINT byte_mask = hBitBuf->bufSize - 1;
204 UINT cache = (hBitBuf->Buffer[(byteOffset - 3) & byte_mask] << 24) |
205 (hBitBuf->Buffer[(byteOffset - 2) & byte_mask] << 16) |
206 (hBitBuf->Buffer[(byteOffset - 1) & byte_mask] << 8) |
207 hBitBuf->Buffer[(byteOffset - 0) & byte_mask];
208
209 if ((BitNdx = (BitNdx & 7)) != 0) {
210 cache = (cache >> (8 - BitNdx)) |
211 ((UINT)hBitBuf->Buffer[(byteOffset - 4) & byte_mask]
212 << (24 + BitNdx));
213 }
214 return (cache);
215 }
216 }
217 #endif
218
FDK_getBwd(HANDLE_FDK_BITBUF hBitBuf,const UINT numberOfBits)219 INT FDK_getBwd(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits) {
220 UINT byteOffset = hBitBuf->BitNdx >> 3;
221 UINT bitOffset = hBitBuf->BitNdx & 0x07;
222 UINT byteMask = hBitBuf->bufSize - 1;
223 int i;
224
225 hBitBuf->BitNdx = (hBitBuf->BitNdx - numberOfBits) & (hBitBuf->bufBits - 1);
226 hBitBuf->BitCnt -= numberOfBits;
227 hBitBuf->ValidBits += numberOfBits;
228
229 UINT tx = hBitBuf->Buffer[(byteOffset - 3) & byteMask] << 24 |
230 hBitBuf->Buffer[(byteOffset - 2) & byteMask] << 16 |
231 hBitBuf->Buffer[(byteOffset - 1) & byteMask] << 8 |
232 hBitBuf->Buffer[byteOffset & byteMask];
233 UINT txa = 0x0;
234
235 tx >>= (8 - bitOffset);
236
237 if (bitOffset && numberOfBits > 24) {
238 tx |= hBitBuf->Buffer[(byteOffset - 4) & byteMask] << (24 + bitOffset);
239 }
240
241 /* in place turn around */
242 for (i = 0; i < 16; i++) {
243 UINT bitMaskR = 0x00000001 << i;
244 UINT bitMaskL = 0x80000000 >> i;
245
246 txa |= (tx & bitMaskR) << (31 - (i << 1));
247 txa |= (tx & bitMaskL) >> (31 - (i << 1));
248 }
249
250 return (txa >> (32 - numberOfBits));
251 }
252
FDK_put(HANDLE_FDK_BITBUF hBitBuf,UINT value,const UINT numberOfBits)253 void FDK_put(HANDLE_FDK_BITBUF hBitBuf, UINT value, const UINT numberOfBits) {
254 if (numberOfBits != 0) {
255 UINT byteOffset0 = hBitBuf->BitNdx >> 3;
256 UINT bitOffset = hBitBuf->BitNdx & 0x7;
257
258 hBitBuf->BitNdx = (hBitBuf->BitNdx + numberOfBits) & (hBitBuf->bufBits - 1);
259 hBitBuf->BitCnt += numberOfBits;
260 hBitBuf->ValidBits += numberOfBits;
261
262 UINT byteMask = hBitBuf->bufSize - 1;
263
264 UINT byteOffset1 = (byteOffset0 + 1) & byteMask;
265 UINT byteOffset2 = (byteOffset0 + 2) & byteMask;
266 UINT byteOffset3 = (byteOffset0 + 3) & byteMask;
267
268 // Create tmp containing free bits at the left border followed by bits to
269 // write, LSB's are cleared, if available Create mask to apply upon all
270 // buffer bytes
271 UINT tmp = (value << (32 - numberOfBits)) >> bitOffset;
272 UINT mask = ~((BitMask[numberOfBits] << (32 - numberOfBits)) >> bitOffset);
273
274 // read all 4 bytes from buffer and create a 32-bit cache
275 UINT cache = (((UINT)hBitBuf->Buffer[byteOffset0]) << 24) |
276 (((UINT)hBitBuf->Buffer[byteOffset1]) << 16) |
277 (((UINT)hBitBuf->Buffer[byteOffset2]) << 8) |
278 (((UINT)hBitBuf->Buffer[byteOffset3]) << 0);
279
280 cache = (cache & mask) | tmp;
281 hBitBuf->Buffer[byteOffset0] = (UCHAR)(cache >> 24);
282 hBitBuf->Buffer[byteOffset1] = (UCHAR)(cache >> 16);
283 hBitBuf->Buffer[byteOffset2] = (UCHAR)(cache >> 8);
284 hBitBuf->Buffer[byteOffset3] = (UCHAR)(cache >> 0);
285
286 if ((bitOffset + numberOfBits) > 32) {
287 UINT byteOffset4 = (byteOffset0 + 4) & byteMask;
288 // remaining bits: in range 1..7
289 // replace MSBits of next byte in buffer by LSBits of "value"
290 int bits = (bitOffset + numberOfBits) & 7;
291 cache =
292 (UINT)hBitBuf->Buffer[byteOffset4] & (~(BitMask[bits] << (8 - bits)));
293 cache |= value << (8 - bits);
294 hBitBuf->Buffer[byteOffset4] = (UCHAR)cache;
295 }
296 }
297 }
298
FDK_putBwd(HANDLE_FDK_BITBUF hBitBuf,UINT value,const UINT numberOfBits)299 void FDK_putBwd(HANDLE_FDK_BITBUF hBitBuf, UINT value,
300 const UINT numberOfBits) {
301 UINT byteOffset = hBitBuf->BitNdx >> 3;
302 UINT bitOffset = 7 - (hBitBuf->BitNdx & 0x07);
303 UINT byteMask = hBitBuf->bufSize - 1;
304
305 UINT mask = ~(BitMask[numberOfBits] << bitOffset);
306 UINT tmp = 0x0000;
307 int i;
308
309 hBitBuf->BitNdx = (hBitBuf->BitNdx - numberOfBits) & (hBitBuf->bufBits - 1);
310 hBitBuf->BitCnt -= numberOfBits;
311 hBitBuf->ValidBits -= numberOfBits;
312
313 /* in place turn around */
314 for (i = 0; i < 16; i++) {
315 UINT bitMaskR = 0x00000001 << i;
316 UINT bitMaskL = 0x80000000 >> i;
317
318 tmp |= (value & bitMaskR) << (31 - (i << 1));
319 tmp |= (value & bitMaskL) >> (31 - (i << 1));
320 }
321 value = tmp;
322 tmp = value >> (32 - numberOfBits) << bitOffset;
323
324 hBitBuf->Buffer[byteOffset & byteMask] =
325 (hBitBuf->Buffer[byteOffset & byteMask] & (mask)) | (UCHAR)(tmp);
326 hBitBuf->Buffer[(byteOffset - 1) & byteMask] =
327 (hBitBuf->Buffer[(byteOffset - 1) & byteMask] & (mask >> 8)) |
328 (UCHAR)(tmp >> 8);
329 hBitBuf->Buffer[(byteOffset - 2) & byteMask] =
330 (hBitBuf->Buffer[(byteOffset - 2) & byteMask] & (mask >> 16)) |
331 (UCHAR)(tmp >> 16);
332 hBitBuf->Buffer[(byteOffset - 3) & byteMask] =
333 (hBitBuf->Buffer[(byteOffset - 3) & byteMask] & (mask >> 24)) |
334 (UCHAR)(tmp >> 24);
335
336 if ((bitOffset + numberOfBits) > 32) {
337 hBitBuf->Buffer[(byteOffset - 4) & byteMask] =
338 (UCHAR)(value >> (64 - numberOfBits - bitOffset)) |
339 (hBitBuf->Buffer[(byteOffset - 4) & byteMask] &
340 ~(BitMask[bitOffset] >> (32 - numberOfBits)));
341 }
342 }
343
344 #ifndef FUNCTION_FDK_pushBack
FDK_pushBack(HANDLE_FDK_BITBUF hBitBuf,const UINT numberOfBits,UCHAR config)345 void FDK_pushBack(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits,
346 UCHAR config) {
347 hBitBuf->BitCnt = (UINT)((INT)hBitBuf->BitCnt - (INT)numberOfBits);
348 hBitBuf->ValidBits =
349 (config == 0) ? (UINT)((INT)hBitBuf->ValidBits + (INT)numberOfBits)
350 : ((UINT)((INT)hBitBuf->ValidBits - (INT)numberOfBits));
351 hBitBuf->BitNdx = ((UINT)((INT)hBitBuf->BitNdx - (INT)numberOfBits)) &
352 (hBitBuf->bufBits - 1);
353 }
354 #endif
355
FDK_pushForward(HANDLE_FDK_BITBUF hBitBuf,const UINT numberOfBits,UCHAR config)356 void FDK_pushForward(HANDLE_FDK_BITBUF hBitBuf, const UINT numberOfBits,
357 UCHAR config) {
358 hBitBuf->BitCnt = (UINT)((INT)hBitBuf->BitCnt + (INT)numberOfBits);
359 hBitBuf->ValidBits =
360 (config == 0) ? ((UINT)((INT)hBitBuf->ValidBits - (INT)numberOfBits))
361 : (UINT)((INT)hBitBuf->ValidBits + (INT)numberOfBits);
362 hBitBuf->BitNdx =
363 (UINT)((INT)hBitBuf->BitNdx + (INT)numberOfBits) & (hBitBuf->bufBits - 1);
364 }
365
FDK_byteAlign(HANDLE_FDK_BITBUF hBitBuf,UCHAR config)366 void FDK_byteAlign(HANDLE_FDK_BITBUF hBitBuf, UCHAR config) {
367 INT alignment = hBitBuf->BitCnt & 0x07;
368
369 if (alignment) {
370 if (config == 0)
371 FDK_pushForward(hBitBuf, 8 - alignment, config); /* BS_READER */
372 else
373 FDK_put(hBitBuf, 0, 8 - alignment); /* BS_WRITER */
374 }
375
376 hBitBuf->BitCnt = 0;
377 }
378
379 #ifndef FUNCTION_FDK_getValidBits
FDK_getValidBits(HANDLE_FDK_BITBUF hBitBuf)380 UINT FDK_getValidBits(HANDLE_FDK_BITBUF hBitBuf) { return hBitBuf->ValidBits; }
381 #endif /* #ifndef FUNCTION_FDK_getValidBits */
382
FDK_getFreeBits(HANDLE_FDK_BITBUF hBitBuf)383 INT FDK_getFreeBits(HANDLE_FDK_BITBUF hBitBuf) {
384 return (hBitBuf->bufBits - hBitBuf->ValidBits);
385 }
386
FDK_setBitCnt(HANDLE_FDK_BITBUF hBitBuf,const UINT value)387 void FDK_setBitCnt(HANDLE_FDK_BITBUF hBitBuf, const UINT value) {
388 hBitBuf->BitCnt = value;
389 }
390
FDK_getBitCnt(HANDLE_FDK_BITBUF hBitBuf)391 INT FDK_getBitCnt(HANDLE_FDK_BITBUF hBitBuf) { return hBitBuf->BitCnt; }
392
FDK_Feed(HANDLE_FDK_BITBUF hBitBuf,const UCHAR * RESTRICT inputBuffer,const UINT bufferSize,UINT * bytesValid)393 void FDK_Feed(HANDLE_FDK_BITBUF hBitBuf, const UCHAR *RESTRICT inputBuffer,
394 const UINT bufferSize, UINT *bytesValid) {
395 inputBuffer = &inputBuffer[bufferSize - *bytesValid];
396
397 UINT bTotal = 0;
398
399 UINT bToRead = (hBitBuf->bufBits - hBitBuf->ValidBits) >> 3;
400 UINT noOfBytes =
401 fMin(bToRead,
402 *bytesValid); //(bToRead < *bytesValid) ? bToRead : *bytesValid ;
403
404 while (noOfBytes > 0) {
405 /* split read to buffer size */
406 bToRead = hBitBuf->bufSize - hBitBuf->ReadOffset;
407 bToRead = fMin(bToRead,
408 noOfBytes); //(bToRead < noOfBytes) ? bToRead : noOfBytes ;
409
410 /* copy 'bToRead' bytes from 'ptr' to inputbuffer */
411 FDKmemcpy(&hBitBuf->Buffer[hBitBuf->ReadOffset], inputBuffer,
412 bToRead * sizeof(UCHAR));
413
414 /* add noOfBits to number of valid bits in buffer */
415 hBitBuf->ValidBits += bToRead << 3;
416 bTotal += bToRead;
417 inputBuffer += bToRead;
418
419 hBitBuf->ReadOffset =
420 (hBitBuf->ReadOffset + bToRead) & (hBitBuf->bufSize - 1);
421 noOfBytes -= bToRead;
422 }
423
424 *bytesValid -= bTotal;
425 }
426
CopyAlignedBlock(HANDLE_FDK_BITBUF h_BitBufSrc,UCHAR * RESTRICT dstBuffer,UINT bToRead)427 void CopyAlignedBlock(HANDLE_FDK_BITBUF h_BitBufSrc, UCHAR *RESTRICT dstBuffer,
428 UINT bToRead) {
429 UINT byteOffset = h_BitBufSrc->BitNdx >> 3;
430 const UINT byteMask = h_BitBufSrc->bufSize - 1;
431
432 UCHAR *RESTRICT pBBB = h_BitBufSrc->Buffer;
433 for (UINT i = 0; i < bToRead; i++) {
434 dstBuffer[i] = pBBB[(byteOffset + i) & byteMask];
435 }
436
437 bToRead <<= 3;
438
439 h_BitBufSrc->BitNdx =
440 (h_BitBufSrc->BitNdx + bToRead) & (h_BitBufSrc->bufBits - 1);
441 h_BitBufSrc->BitCnt += bToRead;
442 h_BitBufSrc->ValidBits -= bToRead;
443 }
444
FDK_Copy(HANDLE_FDK_BITBUF h_BitBufDst,HANDLE_FDK_BITBUF h_BitBufSrc,UINT * bytesValid)445 void FDK_Copy(HANDLE_FDK_BITBUF h_BitBufDst, HANDLE_FDK_BITBUF h_BitBufSrc,
446 UINT *bytesValid) {
447 INT bTotal = 0;
448
449 /* limit noOfBytes to valid bytes in src buffer and available bytes in dst
450 * buffer */
451 UINT bToRead = h_BitBufSrc->ValidBits >> 3;
452 UINT noOfBytes =
453 fMin(bToRead,
454 *bytesValid); //(*bytesValid < bToRead) ? *bytesValid : bToRead ;
455 bToRead = FDK_getFreeBits(h_BitBufDst);
456 noOfBytes =
457 fMin(bToRead, noOfBytes); //(bToRead < noOfBytes) ? bToRead : noOfBytes;
458
459 while (noOfBytes > 0) {
460 /* Split Read to buffer size */
461 bToRead = h_BitBufDst->bufSize - h_BitBufDst->ReadOffset;
462 bToRead = fMin(noOfBytes,
463 bToRead); //(noOfBytes < bToRead) ? noOfBytes : bToRead ;
464
465 /* copy 'bToRead' bytes from buffer to buffer */
466 if (!(h_BitBufSrc->BitNdx & 0x07)) {
467 CopyAlignedBlock(h_BitBufSrc,
468 h_BitBufDst->Buffer + h_BitBufDst->ReadOffset, bToRead);
469 } else {
470 for (UINT i = 0; i < bToRead; i++) {
471 h_BitBufDst->Buffer[h_BitBufDst->ReadOffset + i] =
472 (UCHAR)FDK_get(h_BitBufSrc, 8);
473 }
474 }
475
476 /* add noOfBits to number of valid bits in buffer */
477 h_BitBufDst->ValidBits += bToRead << 3;
478 bTotal += bToRead;
479
480 h_BitBufDst->ReadOffset =
481 (h_BitBufDst->ReadOffset + bToRead) & (h_BitBufDst->bufSize - 1);
482 noOfBytes -= bToRead;
483 }
484
485 *bytesValid -= bTotal;
486 }
487
FDK_Fetch(HANDLE_FDK_BITBUF hBitBuf,UCHAR * outBuf,UINT * writeBytes)488 void FDK_Fetch(HANDLE_FDK_BITBUF hBitBuf, UCHAR *outBuf, UINT *writeBytes) {
489 UCHAR *RESTRICT outputBuffer = outBuf;
490 UINT bTotal = 0;
491
492 UINT bToWrite = (hBitBuf->ValidBits) >> 3;
493 UINT noOfBytes =
494 fMin(bToWrite,
495 *writeBytes); //(bToWrite < *writeBytes) ? bToWrite : *writeBytes ;
496
497 while (noOfBytes > 0) {
498 /* split write to buffer size */
499 bToWrite = hBitBuf->bufSize - hBitBuf->WriteOffset;
500 bToWrite = fMin(
501 bToWrite, noOfBytes); //(bToWrite < noOfBytes) ? bToWrite : noOfBytes ;
502
503 /* copy 'bToWrite' bytes from bitbuffer to outputbuffer */
504 FDKmemcpy(outputBuffer, &hBitBuf->Buffer[hBitBuf->WriteOffset],
505 bToWrite * sizeof(UCHAR));
506
507 /* sub noOfBits from number of valid bits in buffer */
508 hBitBuf->ValidBits -= bToWrite << 3;
509 bTotal += bToWrite;
510 outputBuffer += bToWrite;
511
512 hBitBuf->WriteOffset =
513 (hBitBuf->WriteOffset + bToWrite) & (hBitBuf->bufSize - 1);
514 noOfBytes -= bToWrite;
515 }
516
517 *writeBytes = bTotal;
518 }
519