matrix_enc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_enc.c
  22. Contains: ALAC mixing/matrixing encode 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 mix16( int16_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples, int32_t mixbits, int32_t mixres )
  50. {
  51. int16_t * ip = in;
  52. int32_t j;
  53. if ( mixres != 0 )
  54. {
  55. int32_t mod = 1 << mixbits;
  56. int32_t m2;
  57. /* matrixed stereo */
  58. m2 = mod - mixres;
  59. for ( j = 0; j < numSamples; j++ )
  60. {
  61. int32_t l, r;
  62. l = (int32_t) ip[0];
  63. r = (int32_t) ip[1];
  64. ip += stride;
  65. u[j] = (mixres * l + m2 * r) >> mixbits;
  66. v[j] = l - r;
  67. }
  68. }
  69. else
  70. {
  71. /* Conventional separated stereo. */
  72. for ( j = 0; j < numSamples; j++ )
  73. {
  74. u[j] = (int32_t) ip[0];
  75. v[j] = (int32_t) ip[1];
  76. ip += stride;
  77. }
  78. }
  79. }
  80. // 20-bit routines
  81. // - the 20 bits of data are left-justified in 3 bytes of storage but right-aligned for input/output predictor buffers
  82. void mix20( uint8_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples, int32_t mixbits, int32_t mixres )
  83. {
  84. int32_t l, r;
  85. uint8_t * ip = in;
  86. int32_t j;
  87. if ( mixres != 0 )
  88. {
  89. /* matrixed stereo */
  90. int32_t mod = 1 << mixbits;
  91. int32_t m2 = mod - mixres;
  92. for ( j = 0; j < numSamples; j++ )
  93. {
  94. l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  95. l = (l << 8) >> 12;
  96. ip += 3;
  97. r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  98. r = (r << 8) >> 12;
  99. ip += (stride - 1) * 3;
  100. u[j] = (mixres * l + m2 * r) >> mixbits;
  101. v[j] = l - r;
  102. }
  103. }
  104. else
  105. {
  106. /* Conventional separated stereo. */
  107. for ( j = 0; j < numSamples; j++ )
  108. {
  109. l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  110. u[j] = (l << 8) >> 12;
  111. ip += 3;
  112. r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  113. v[j] = (r << 8) >> 12;
  114. ip += (stride - 1) * 3;
  115. }
  116. }
  117. }
  118. // 24-bit routines
  119. // - the 24 bits of data are right-justified in the input/output predictor buffers
  120. void mix24( uint8_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples,
  121. int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
  122. {
  123. int32_t l, r;
  124. uint8_t * ip = in;
  125. int32_t shift = bytesShifted * 8;
  126. uint32_t mask = (1ul << shift) - 1;
  127. int32_t j, k;
  128. if ( mixres != 0 )
  129. {
  130. /* matrixed stereo */
  131. int32_t mod = 1 << mixbits;
  132. int32_t m2 = mod - mixres;
  133. if ( bytesShifted != 0 )
  134. {
  135. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  136. {
  137. l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  138. l = (l << 8) >> 8;
  139. ip += 3;
  140. r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  141. r = (r << 8) >> 8;
  142. ip += (stride - 1) * 3;
  143. shiftUV[k + 0] = (uint16_t)(l & mask);
  144. shiftUV[k + 1] = (uint16_t)(r & mask);
  145. l >>= shift;
  146. r >>= shift;
  147. u[j] = (mixres * l + m2 * r) >> mixbits;
  148. v[j] = l - r;
  149. }
  150. }
  151. else
  152. {
  153. for ( j = 0; j < numSamples; j++ )
  154. {
  155. l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  156. l = (l << 8) >> 8;
  157. ip += 3;
  158. r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  159. r = (r << 8) >> 8;
  160. ip += (stride - 1) * 3;
  161. u[j] = (mixres * l + m2 * r) >> mixbits;
  162. v[j] = l - r;
  163. }
  164. }
  165. }
  166. else
  167. {
  168. /* Conventional separated stereo. */
  169. if ( bytesShifted != 0 )
  170. {
  171. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  172. {
  173. l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  174. l = (l << 8) >> 8;
  175. ip += 3;
  176. r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  177. r = (r << 8) >> 8;
  178. ip += (stride - 1) * 3;
  179. shiftUV[k + 0] = (uint16_t)(l & mask);
  180. shiftUV[k + 1] = (uint16_t)(r & mask);
  181. l >>= shift;
  182. r >>= shift;
  183. u[j] = l;
  184. v[j] = r;
  185. }
  186. }
  187. else
  188. {
  189. for ( j = 0; j < numSamples; j++ )
  190. {
  191. l = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  192. u[j] = (l << 8) >> 8;
  193. ip += 3;
  194. r = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  195. v[j] = (r << 8) >> 8;
  196. ip += (stride - 1) * 3;
  197. }
  198. }
  199. }
  200. }
  201. // 32-bit routines
  202. // - note that these really expect the internal data width to be < 32 but the arrays are 32-bit
  203. // - otherwise, the calculations might overflow into the 33rd bit and be lost
  204. // - therefore, these routines deal with the specified "unused lower" bytes in the "shift" buffers
  205. void mix32( int32_t * in, uint32_t stride, int32_t * u, int32_t * v, int32_t numSamples,
  206. int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted )
  207. {
  208. int32_t * ip = in;
  209. int32_t shift = bytesShifted * 8;
  210. uint32_t mask = (1ul << shift) - 1;
  211. int32_t l, r;
  212. int32_t j, k;
  213. if ( mixres != 0 )
  214. {
  215. int32_t mod = 1 << mixbits;
  216. int32_t m2;
  217. //Assert( bytesShifted != 0 );
  218. /* matrixed stereo with shift */
  219. m2 = mod - mixres;
  220. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  221. {
  222. l = ip[0];
  223. r = ip[1];
  224. ip += stride;
  225. shiftUV[k + 0] = (uint16_t)(l & mask);
  226. shiftUV[k + 1] = (uint16_t)(r & mask);
  227. l >>= shift;
  228. r >>= shift;
  229. u[j] = (mixres * l + m2 * r) >> mixbits;
  230. v[j] = l - r;
  231. }
  232. }
  233. else
  234. {
  235. if ( bytesShifted == 0 )
  236. {
  237. /* de-interleaving w/o shift */
  238. for ( j = 0; j < numSamples; j++ )
  239. {
  240. u[j] = ip[0];
  241. v[j] = ip[1];
  242. ip += stride;
  243. }
  244. }
  245. else
  246. {
  247. /* de-interleaving with shift */
  248. for ( j = 0, k = 0; j < numSamples; j++, k += 2 )
  249. {
  250. l = ip[0];
  251. r = ip[1];
  252. ip += stride;
  253. shiftUV[k + 0] = (uint16_t)(l & mask);
  254. shiftUV[k + 1] = (uint16_t)(r & mask);
  255. l >>= shift;
  256. r >>= shift;
  257. u[j] = l;
  258. v[j] = r;
  259. }
  260. }
  261. }
  262. }
  263. // 20/24-bit <-> 32-bit helper routines (not really matrixing but convenient to put here)
  264. void copy20ToPredictor( uint8_t * in, uint32_t stride, int32_t * out, int32_t numSamples )
  265. {
  266. uint8_t * ip = in;
  267. int32_t j;
  268. for ( j = 0; j < numSamples; j++ )
  269. {
  270. int32_t val;
  271. // 20-bit values are left-aligned in the 24-bit input buffer but right-aligned in the 32-bit output buffer
  272. val = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  273. out[j] = (val << 8) >> 12;
  274. ip += stride * 3;
  275. }
  276. }
  277. void copy24ToPredictor( uint8_t * in, uint32_t stride, int32_t * out, int32_t numSamples )
  278. {
  279. uint8_t * ip = in;
  280. int32_t j;
  281. for ( j = 0; j < numSamples; j++ )
  282. {
  283. int32_t val;
  284. val = (int32_t)( ((uint32_t)ip[HBYTE] << 16) | ((uint32_t)ip[MBYTE] << 8) | (uint32_t)ip[LBYTE] );
  285. out[j] = (val << 8) >> 8;
  286. ip += stride * 3;
  287. }
  288. }