ag_dec.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. /*
  21. File: ag_dec.c
  22. Contains: Adaptive Golomb decode routines.
  23. Copyright: (c) 2001-2011 Apple, Inc.
  24. */
  25. #include "aglib.h"
  26. #include "ALACBitUtilities.h"
  27. #include "ALACAudioTypes.h"
  28. #include <math.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #if __GNUC__ && TARGET_OS_MAC
  33. #if __POWERPC__
  34. #include <ppc_intrinsics.h>
  35. #else
  36. #include <libkern/OSByteOrder.h>
  37. #endif
  38. #endif
  39. #define CODE_TO_LONG_MAXBITS 32
  40. #define N_MAX_MEAN_CLAMP 0xffff
  41. #define N_MEAN_CLAMP_VAL 0xffff
  42. #define REPORT_VAL 40
  43. #if __GNUC__
  44. #define ALWAYS_INLINE __attribute__((always_inline))
  45. #else
  46. #define ALWAYS_INLINE
  47. #endif
  48. /* And on the subject of the CodeWarrior x86 compiler and inlining, I reworked a lot of this
  49. to help the compiler out. In many cases this required manual inlining or a macro. Sorry
  50. if it is ugly but the performance gains are well worth it.
  51. - WSK 5/19/04
  52. */
  53. void set_standard_ag_params(AGParamRecPtr params, uint32_t fullwidth, uint32_t sectorwidth)
  54. {
  55. /* Use
  56. fullwidth = sectorwidth = numOfSamples, for analog 1-dimensional type-short data,
  57. but use
  58. fullwidth = full image width, sectorwidth = sector (patch) width
  59. for such as image (2-dim.) data.
  60. */
  61. set_ag_params( params, MB0, PB0, KB0, fullwidth, sectorwidth, MAX_RUN_DEFAULT );
  62. }
  63. void set_ag_params(AGParamRecPtr params, uint32_t m, uint32_t p, uint32_t k, uint32_t f, uint32_t s, uint32_t maxrun)
  64. {
  65. params->mb = params->mb0 = m;
  66. params->pb = p;
  67. params->kb = k;
  68. params->wb = (1u<<params->kb)-1;
  69. params->qb = QB-params->pb;
  70. params->fw = f;
  71. params->sw = s;
  72. params->maxrun = maxrun;
  73. }
  74. #if PRAGMA_MARK
  75. #pragma mark -
  76. #endif
  77. // note: implementing this with some kind of "count leading zeros" assembly is a big performance win
  78. static inline int32_t lead( int32_t m )
  79. {
  80. long j;
  81. unsigned long c = (1ul << 31);
  82. for(j=0; j < 32; j++)
  83. {
  84. if((c & m) != 0)
  85. break;
  86. c >>= 1;
  87. }
  88. return (j);
  89. }
  90. #define arithmin(a, b) ((a) < (b) ? (a) : (b))
  91. static inline int32_t ALWAYS_INLINE lg3a( int32_t x)
  92. {
  93. int32_t result;
  94. x += 3;
  95. result = lead(x);
  96. return 31 - result;
  97. }
  98. static inline uint32_t ALWAYS_INLINE read32bit( uint8_t * buffer )
  99. {
  100. // embedded CPUs typically can't read unaligned 32-bit words so just read the bytes
  101. uint32_t value;
  102. value = ((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) |
  103. ((uint32_t)buffer[2] << 8) | (uint32_t)buffer[3];
  104. return value;
  105. }
  106. #if PRAGMA_MARK
  107. #pragma mark -
  108. #endif
  109. #define get_next_fromlong(inlong, suff) ((inlong) >> (32 - (suff)))
  110. static inline uint32_t ALWAYS_INLINE
  111. getstreambits( uint8_t *in, int32_t bitoffset, int32_t numbits )
  112. {
  113. uint32_t load1, load2;
  114. uint32_t byteoffset = bitoffset / 8;
  115. uint32_t result;
  116. //Assert( numbits <= 32 );
  117. load1 = read32bit( in + byteoffset );
  118. if ( (numbits + (bitoffset & 0x7)) > 32)
  119. {
  120. int32_t load2shift;
  121. result = load1 << (bitoffset & 0x7);
  122. load2 = (uint32_t) in[byteoffset+4];
  123. load2shift = (8-(numbits + (bitoffset & 0x7)-32));
  124. load2 >>= load2shift;
  125. result >>= (32-numbits);
  126. result |= load2;
  127. }
  128. else
  129. {
  130. result = load1 >> (32-numbits-(bitoffset & 7));
  131. }
  132. // a shift of >= "the number of bits in the type of the value being shifted" results in undefined
  133. // behavior so don't try to shift by 32
  134. if ( numbits != (sizeof(result) * 8) )
  135. result &= ~(0xfffffffful << numbits);
  136. return result;
  137. }
  138. static inline int32_t dyn_get(unsigned char *in, uint32_t *bitPos, uint32_t m, uint32_t k)
  139. {
  140. uint32_t tempbits = *bitPos;
  141. uint32_t result;
  142. uint32_t pre = 0, v;
  143. uint32_t streamlong;
  144. streamlong = read32bit( in + (tempbits >> 3) );
  145. streamlong <<= (tempbits & 7);
  146. /* find the number of bits in the prefix */
  147. {
  148. uint32_t notI = ~streamlong;
  149. pre = lead( notI);
  150. }
  151. if(pre >= MAX_PREFIX_16)
  152. {
  153. pre = MAX_PREFIX_16;
  154. tempbits += pre;
  155. streamlong <<= pre;
  156. result = get_next_fromlong(streamlong,MAX_DATATYPE_BITS_16);
  157. tempbits += MAX_DATATYPE_BITS_16;
  158. }
  159. else
  160. {
  161. // all of the bits must fit within the long we have loaded
  162. //Assert(pre+1+k <= 32);
  163. tempbits += pre;
  164. tempbits += 1;
  165. streamlong <<= pre+1;
  166. v = get_next_fromlong(streamlong, k);
  167. tempbits += k;
  168. result = pre*m + v-1;
  169. if(v<2) {
  170. result -= (v-1);
  171. tempbits -= 1;
  172. }
  173. }
  174. *bitPos = tempbits;
  175. return result;
  176. }
  177. static inline int32_t dyn_get_32bit( uint8_t * in, uint32_t * bitPos, int32_t m, int32_t k, int32_t maxbits )
  178. {
  179. uint32_t tempbits = *bitPos;
  180. uint32_t v;
  181. uint32_t streamlong;
  182. uint32_t result;
  183. streamlong = read32bit( in + (tempbits >> 3) );
  184. streamlong <<= (tempbits & 7);
  185. /* find the number of bits in the prefix */
  186. {
  187. uint32_t notI = ~streamlong;
  188. result = lead( notI);
  189. }
  190. if(result >= MAX_PREFIX_32)
  191. {
  192. result = getstreambits(in, tempbits+MAX_PREFIX_32, maxbits);
  193. tempbits += MAX_PREFIX_32 + maxbits;
  194. }
  195. else
  196. {
  197. /* all of the bits must fit within the long we have loaded*/
  198. //Assert(k<=14);
  199. //Assert(result<MAX_PREFIX_32);
  200. //Assert(result+1+k <= 32);
  201. tempbits += result;
  202. tempbits += 1;
  203. if (k != 1)
  204. {
  205. streamlong <<= result+1;
  206. v = get_next_fromlong(streamlong, k);
  207. tempbits += k;
  208. tempbits -= 1;
  209. result = result*m;
  210. if(v>=2)
  211. {
  212. result += (v-1);
  213. tempbits += 1;
  214. }
  215. }
  216. }
  217. *bitPos = tempbits;
  218. return result;
  219. }
  220. int32_t dyn_decomp( AGParamRecPtr params, BitBuffer * bitstream, int32_t * pc, int32_t numSamples, int32_t maxSize, uint32_t * outNumBits )
  221. {
  222. uint8_t *in;
  223. int32_t *outPtr = pc;
  224. uint32_t bitPos, startPos, maxPos;
  225. uint32_t j, m, k, n, c, mz;
  226. int32_t del, zmode;
  227. uint32_t mb;
  228. uint32_t pb_local = params->pb;
  229. uint32_t kb_local = params->kb;
  230. uint32_t wb_local = params->wb;
  231. int32_t status;
  232. RequireAction( (bitstream != nil) && (pc != nil) && (outNumBits != nil), return kALAC_ParamError; );
  233. *outNumBits = 0;
  234. in = bitstream->cur;
  235. startPos = bitstream->bitIndex;
  236. maxPos = bitstream->byteSize * 8;
  237. bitPos = startPos;
  238. mb = params->mb0;
  239. zmode = 0;
  240. c = 0;
  241. status = ALAC_noErr;
  242. while (c < numSamples)
  243. {
  244. // bail if we've run off the end of the buffer
  245. RequireAction( bitPos < maxPos, status = kALAC_ParamError; goto Exit; );
  246. m = (mb)>>QBSHIFT;
  247. k = lg3a(m);
  248. k = arithmin(k, kb_local);
  249. m = (1<<k)-1;
  250. n = dyn_get_32bit( in, &bitPos, m, k, maxSize );
  251. // least significant bit is sign bit
  252. {
  253. uint32_t ndecode = n + zmode;
  254. int32_t multiplier = (- (ndecode&1));
  255. multiplier |= 1;
  256. del = ((ndecode+1) >> 1) * (multiplier);
  257. }
  258. *outPtr++ = del;
  259. c++;
  260. mb = pb_local*(n+zmode) + mb - ((pb_local*mb)>>QBSHIFT);
  261. // update mean tracking
  262. if (n > N_MAX_MEAN_CLAMP)
  263. mb = N_MEAN_CLAMP_VAL;
  264. zmode = 0;
  265. if (((mb << MMULSHIFT) < QB) && (c < numSamples))
  266. {
  267. zmode = 1;
  268. k = lead(mb) - BITOFF+((mb+MOFF)>>MDENSHIFT);
  269. mz = ((1<<k)-1) & wb_local;
  270. n = dyn_get(in, &bitPos, mz, k);
  271. RequireAction(c+n <= numSamples, status = kALAC_ParamError; goto Exit; );
  272. for(j=0; j < n; j++)
  273. {
  274. *outPtr++ = 0;
  275. ++c;
  276. }
  277. if(n >= 65535)
  278. zmode = 0;
  279. mb = 0;
  280. }
  281. }
  282. Exit:
  283. *outNumBits = (bitPos - startPos);
  284. BitBufferAdvance( bitstream, *outNumBits );
  285. RequireAction( bitstream->cur <= bitstream->end, status = kALAC_ParamError; );
  286. return status;
  287. }