matrix_dec.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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: matrix_dec.c
  22. Contains: ALAC mixing/matrixing decode routines.
  23. Copyright: (c) 2004-2011 Apple, Inc.
  24. */
  25. #include "matrixlib.h"
  26. #include "ALACAudioTypes.h"
  27. // up to 24-bit "offset" macros for the individual bytes of a 20/24-bit word
  28. #if TARGET_RT_BIG_ENDIAN
  29. #define LBYTE 2
  30. #define MBYTE 1
  31. #define HBYTE 0
  32. #else
  33. #define LBYTE 0
  34. #define MBYTE 1
  35. #define HBYTE 2
  36. #endif
  37. /*
  38. There is no plain middle-side option; instead there are various mixing
  39. modes including middle-side, each lossless, as embodied in the mix()
  40. and unmix() functions. These functions exploit a generalized middle-side
  41. transformation:
  42. u := [(rL + (m-r)R)/m];
  43. v := L - R;
  44. where [ ] denotes integer floor. The (lossless) inverse is
  45. L = u + v - [rV/m];
  46. R = L - v;
  47. */
  48. // 16-bit routines
  49. void unmix16( int32_t * u, int32_t * v, int16_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres )
  50. {
  51. int16_t * op = out;
  52. int32_t j;
  53. if ( mixres != 0 )
  54. {
  55. /* matrixed stereo */
  56. for ( j = 0; j < numSamples; j++ )
  57. {
  58. int32_t l, r;
  59. l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
  60. r = l - v[j];
  61. op[0] = (int16_t) l;
  62. op[1] = (int16_t) r;
  63. op += stride;
  64. }
  65. }
  66. else
  67. {
  68. /* Conventional separated stereo. */
  69. for ( j = 0; j < numSamples; j++ )
  70. {
  71. op[0] = (int16_t) u[j];
  72. op[1] = (int16_t) v[j];
  73. op += stride;
  74. }
  75. }
  76. }
  77. // 20-bit routines
  78. // - the 20 bits of data are left-justified in 3 bytes of storage but right-aligned for input/output predictor buffers
  79. void unmix20( int32_t * u, int32_t * v, uint8_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres )
  80. {
  81. uint8_t * op = out;
  82. int32_t j;
  83. if ( mixres != 0 )
  84. {
  85. /* matrixed stereo */
  86. for ( j = 0; j < numSamples; j++ )
  87. {
  88. int32_t l, r;
  89. l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
  90. r = l - v[j];
  91. l <<= 4;
  92. r <<= 4;
  93. op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
  94. op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
  95. op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
  96. op += 3;
  97. op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
  98. op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
  99. op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
  100. op += (stride - 1) * 3;
  101. }
  102. }
  103. else
  104. {
  105. /* Conventional separated stereo. */
  106. for ( j = 0; j < numSamples; j++ )
  107. {
  108. int32_t val;
  109. val = u[j] << 4;
  110. op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
  111. op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
  112. op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
  113. op += 3;
  114. val = v[j] << 4;
  115. op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
  116. op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
  117. op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
  118. op += (stride - 1) * 3;
  119. }
  120. }
  121. }
  122. // 24-bit routines
  123. // - the 24 bits of data are right-justified in the input/output predictor buffers
  124. void unmix24( int32_t * u, int32_t * v, uint8_t * out, uint32_t stride, int32_t numSamples,
  125. int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
  126. {
  127. uint8_t * op = out;
  128. int32_t shift = bytesShifted * 8;
  129. int32_t l, r;
  130. int32_t j, k;
  131. if ( mixres != 0 )
  132. {
  133. /* matrixed stereo */
  134. if ( bytesShifted != 0 )
  135. {
  136. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  137. {
  138. l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
  139. r = l - v[j];
  140. l = (l << shift) | (uint32_t) shiftUV[k + 0];
  141. r = (r << shift) | (uint32_t) shiftUV[k + 1];
  142. op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
  143. op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
  144. op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
  145. op += 3;
  146. op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
  147. op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
  148. op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
  149. op += (stride - 1) * 3;
  150. }
  151. }
  152. else
  153. {
  154. for ( j = 0; j < numSamples; j++ )
  155. {
  156. l = u[j] + v[j] - ((mixres * v[j]) >> mixbits);
  157. r = l - v[j];
  158. op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
  159. op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
  160. op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
  161. op += 3;
  162. op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
  163. op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
  164. op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
  165. op += (stride - 1) * 3;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. /* Conventional separated stereo. */
  172. if ( bytesShifted != 0 )
  173. {
  174. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  175. {
  176. l = u[j];
  177. r = v[j];
  178. l = (l << shift) | (uint32_t) shiftUV[k + 0];
  179. r = (r << shift) | (uint32_t) shiftUV[k + 1];
  180. op[HBYTE] = (uint8_t)((l >> 16) & 0xffu);
  181. op[MBYTE] = (uint8_t)((l >> 8) & 0xffu);
  182. op[LBYTE] = (uint8_t)((l >> 0) & 0xffu);
  183. op += 3;
  184. op[HBYTE] = (uint8_t)((r >> 16) & 0xffu);
  185. op[MBYTE] = (uint8_t)((r >> 8) & 0xffu);
  186. op[LBYTE] = (uint8_t)((r >> 0) & 0xffu);
  187. op += (stride - 1) * 3;
  188. }
  189. }
  190. else
  191. {
  192. for ( j = 0; j < numSamples; j++ )
  193. {
  194. int32_t val;
  195. val = u[j];
  196. op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
  197. op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
  198. op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
  199. op += 3;
  200. val = v[j];
  201. op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
  202. op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
  203. op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
  204. op += (stride - 1) * 3;
  205. }
  206. }
  207. }
  208. }
  209. // 32-bit routines
  210. // - note that these really expect the internal data width to be < 32 but the arrays are 32-bit
  211. // - otherwise, the calculations might overflow into the 33rd bit and be lost
  212. // - therefore, these routines deal with the specified "unused lower" bytes in the "shift" buffers
  213. void unmix32( int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t numSamples,
  214. int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
  215. {
  216. int32_t * op = out;
  217. int32_t shift = bytesShifted * 8;
  218. int32_t l, r;
  219. int32_t j, k;
  220. if ( mixres != 0 )
  221. {
  222. //Assert( bytesShifted != 0 );
  223. /* matrixed stereo with shift */
  224. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  225. {
  226. int32_t lt, rt;
  227. lt = u[j];
  228. rt = v[j];
  229. l = lt + rt - ((mixres * rt) >> mixbits);
  230. r = l - rt;
  231. op[0] = (l << shift) | (uint32_t) shiftUV[k + 0];
  232. op[1] = (r << shift) | (uint32_t) shiftUV[k + 1];
  233. op += stride;
  234. }
  235. }
  236. else
  237. {
  238. if ( bytesShifted == 0 )
  239. {
  240. /* interleaving w/o shift */
  241. for ( j = 0; j < numSamples; j++ )
  242. {
  243. op[0] = u[j];
  244. op[1] = v[j];
  245. op += stride;
  246. }
  247. }
  248. else
  249. {
  250. /* interleaving with shift */
  251. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  252. {
  253. op[0] = (u[j] << shift) | (uint32_t) shiftUV[k + 0];
  254. op[1] = (v[j] << shift) | (uint32_t) shiftUV[k + 1];
  255. op += stride;
  256. }
  257. }
  258. }
  259. }
  260. // 20/24-bit <-> 32-bit helper routines (not really matrixing but convenient to put here)
  261. void copyPredictorTo24( int32_t * in, uint8_t * out, uint32_t stride, int32_t numSamples )
  262. {
  263. uint8_t * op = out;
  264. int32_t j;
  265. for ( j = 0; j < numSamples; j++ )
  266. {
  267. int32_t val = in[j];
  268. op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
  269. op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
  270. op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
  271. op += (stride * 3);
  272. }
  273. }
  274. void copyPredictorTo24Shift( int32_t * in, uint16_t * shift, uint8_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted )
  275. {
  276. uint8_t * op = out;
  277. int32_t shiftVal = bytesShifted * 8;
  278. int32_t j;
  279. //Assert( bytesShifted != 0 );
  280. for ( j = 0; j < numSamples; j++ )
  281. {
  282. int32_t val = in[j];
  283. val = (val << shiftVal) | (uint32_t) shift[j];
  284. op[HBYTE] = (uint8_t)((val >> 16) & 0xffu);
  285. op[MBYTE] = (uint8_t)((val >> 8) & 0xffu);
  286. op[LBYTE] = (uint8_t)((val >> 0) & 0xffu);
  287. op += (stride * 3);
  288. }
  289. }
  290. void copyPredictorTo20( int32_t * in, uint8_t * out, uint32_t stride, int32_t numSamples )
  291. {
  292. uint8_t * op = out;
  293. int32_t j;
  294. // 32-bit predictor values are right-aligned but 20-bit output values should be left-aligned
  295. // in the 24-bit output buffer
  296. for ( j = 0; j < numSamples; j++ )
  297. {
  298. int32_t val = in[j];
  299. op[HBYTE] = (uint8_t)((val >> 12) & 0xffu);
  300. op[MBYTE] = (uint8_t)((val >> 4) & 0xffu);
  301. op[LBYTE] = (uint8_t)((val << 4) & 0xffu);
  302. op += (stride * 3);
  303. }
  304. }
  305. void copyPredictorTo32( int32_t * in, int32_t * out, uint32_t stride, int32_t numSamples )
  306. {
  307. int32_t i, j;
  308. // this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
  309. for ( i = 0, j = 0; i < numSamples; i++, j += stride )
  310. out[j] = in[i];
  311. }
  312. void copyPredictorTo32Shift( int32_t * in, uint16_t * shift, int32_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted )
  313. {
  314. int32_t * op = out;
  315. uint32_t shiftVal = bytesShifted * 8;
  316. int32_t j;
  317. //Assert( bytesShifted != 0 );
  318. // this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
  319. for ( j = 0; j < numSamples; j++ )
  320. {
  321. op[0] = (in[j] << shiftVal) | (uint32_t) shift[j];
  322. op += stride;
  323. }
  324. }