2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
24 Contains: ALAC mixing/matrixing encode routines.
26 Copyright: (c) 2004-2011 Apple, Inc.
29 #include "matrixlib.h"
30 #include "ALACAudioTypes.h"
32 // up to 24-bit "offset" macros for the individual bytes of a 20/24-bit word
33 #if TARGET_RT_BIG_ENDIAN
44 There is no plain middle-side option; instead there are various mixing
45 modes including middle-side, each lossless, as embodied in the mix()
46 and unmix() functions. These functions exploit a generalized middle-side
49 u := [(rL + (m-r)R)/m];
52 where [ ] denotes integer floor. The (lossless) inverse is
60 void mix16( int16_t * in
, uint32_t stride
, int32_t * u
, int32_t * v
, int32_t numSamples
, int32_t mixbits
, int32_t mixres
)
67 int32_t mod
= 1 << mixbits
;
72 for ( j
= 0; j
< numSamples
; j
++ )
79 u
[j
] = (mixres
* l
+ m2
* r
) >> mixbits
;
85 /* Conventional separated stereo. */
86 for ( j
= 0; j
< numSamples
; j
++ )
88 u
[j
] = (int32_t) ip
[0];
89 v
[j
] = (int32_t) ip
[1];
96 // - the 20 bits of data are left-justified in 3 bytes of storage but right-aligned for input/output predictor buffers
98 void mix20( uint8_t * in
, uint32_t stride
, int32_t * u
, int32_t * v
, int32_t numSamples
, int32_t mixbits
, int32_t mixres
)
106 /* matrixed stereo */
107 int32_t mod
= 1 << mixbits
;
108 int32_t m2
= mod
- mixres
;
110 for ( j
= 0; j
< numSamples
; j
++ )
112 l
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
116 r
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
118 ip
+= (stride
- 1) * 3;
120 u
[j
] = (mixres
* l
+ m2
* r
) >> mixbits
;
126 /* Conventional separated stereo. */
127 for ( j
= 0; j
< numSamples
; j
++ )
129 l
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
130 u
[j
] = (l
<< 8) >> 12;
133 r
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
134 v
[j
] = (r
<< 8) >> 12;
135 ip
+= (stride
- 1) * 3;
141 // - the 24 bits of data are right-justified in the input/output predictor buffers
143 void mix24( uint8_t * in
, uint32_t stride
, int32_t * u
, int32_t * v
, int32_t numSamples
,
144 int32_t mixbits
, int32_t mixres
, uint16_t * shiftUV
, int32_t bytesShifted
)
148 int32_t shift
= bytesShifted
* 8;
149 uint32_t mask
= (1ul << shift
) - 1;
154 /* matrixed stereo */
155 int32_t mod
= 1 << mixbits
;
156 int32_t m2
= mod
- mixres
;
158 if ( bytesShifted
!= 0 )
160 for ( j
= 0, k
= 0; j
< numSamples
; j
++, k
+= 2 )
162 l
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
166 r
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
168 ip
+= (stride
- 1) * 3;
170 shiftUV
[k
+ 0] = (uint16_t)(l
& mask
);
171 shiftUV
[k
+ 1] = (uint16_t)(r
& mask
);
176 u
[j
] = (mixres
* l
+ m2
* r
) >> mixbits
;
182 for ( j
= 0; j
< numSamples
; j
++ )
184 l
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
188 r
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
190 ip
+= (stride
- 1) * 3;
192 u
[j
] = (mixres
* l
+ m2
* r
) >> mixbits
;
199 /* Conventional separated stereo. */
200 if ( bytesShifted
!= 0 )
202 for ( j
= 0, k
= 0; j
< numSamples
; j
++, k
+= 2 )
204 l
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
208 r
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
210 ip
+= (stride
- 1) * 3;
212 shiftUV
[k
+ 0] = (uint16_t)(l
& mask
);
213 shiftUV
[k
+ 1] = (uint16_t)(r
& mask
);
224 for ( j
= 0; j
< numSamples
; j
++ )
226 l
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
227 u
[j
] = (l
<< 8) >> 8;
230 r
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
231 v
[j
] = (r
<< 8) >> 8;
232 ip
+= (stride
- 1) * 3;
239 // - note that these really expect the internal data width to be < 32 but the arrays are 32-bit
240 // - otherwise, the calculations might overflow into the 33rd bit and be lost
241 // - therefore, these routines deal with the specified "unused lower" bytes in the "shift" buffers
243 void mix32( int32_t * in
, uint32_t stride
, int32_t * u
, int32_t * v
, int32_t numSamples
,
244 int32_t mixbits
, int32_t mixres
, uint16_t * shiftUV
, int32_t bytesShifted
)
247 int32_t shift
= bytesShifted
* 8;
248 uint32_t mask
= (1ul << shift
) - 1;
254 int32_t mod
= 1 << mixbits
;
257 //Assert( bytesShifted != 0 );
259 /* matrixed stereo with shift */
261 for ( j
= 0, k
= 0; j
< numSamples
; j
++, k
+= 2 )
267 shiftUV
[k
+ 0] = (uint16_t)(l
& mask
);
268 shiftUV
[k
+ 1] = (uint16_t)(r
& mask
);
273 u
[j
] = (mixres
* l
+ m2
* r
) >> mixbits
;
279 if ( bytesShifted
== 0 )
281 /* de-interleaving w/o shift */
282 for ( j
= 0; j
< numSamples
; j
++ )
291 /* de-interleaving with shift */
292 for ( j
= 0, k
= 0; j
< numSamples
; j
++, k
+= 2 )
298 shiftUV
[k
+ 0] = (uint16_t)(l
& mask
);
299 shiftUV
[k
+ 1] = (uint16_t)(r
& mask
);
311 // 20/24-bit <-> 32-bit helper routines (not really matrixing but convenient to put here)
313 void copy20ToPredictor( uint8_t * in
, uint32_t stride
, int32_t * out
, int32_t numSamples
)
318 for ( j
= 0; j
< numSamples
; j
++ )
322 // 20-bit values are left-aligned in the 24-bit input buffer but right-aligned in the 32-bit output buffer
323 val
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
324 out
[j
] = (val
<< 8) >> 12;
329 void copy24ToPredictor( uint8_t * in
, uint32_t stride
, int32_t * out
, int32_t numSamples
)
334 for ( j
= 0; j
< numSamples
; j
++ )
338 val
= (int32_t)( ((uint32_t)ip
[HBYTE
] << 16) | ((uint32_t)ip
[MBYTE
] << 8) | (uint32_t)ip
[LBYTE
] );
339 out
[j
] = (val
<< 8) >> 8;