Coverage Report

Created: 2026-12-01 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsndfile/src/ALAC/matrix_dec.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2011 Apple Inc. All rights reserved.
3
 * Copyright (C) 2012-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
4
 *
5
 * @APPLE_APACHE_LICENSE_HEADER_START@
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License") ;
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 * @APPLE_APACHE_LICENSE_HEADER_END@
20
 */
21
22
/*
23
  File:   matrix_dec.c
24
25
  Contains: ALAC mixing/matrixing decode routines.
26
27
  Copyright:  (c) 2004-2011 Apple, Inc.
28
*/
29
30
#include "config.h"
31
32
#include "matrixlib.h"
33
#include "ALACAudioTypes.h"
34
#include "shift.h"
35
36
// up to 24-bit "offset" macros for the individual bytes of a 20/24-bit word
37
#if TARGET_RT_BIG_ENDIAN
38
  #define LBYTE 2
39
  #define MBYTE 1
40
  #define HBYTE 0
41
#else
42
  #define LBYTE 0
43
  #define MBYTE 1
44
  #define HBYTE 2
45
#endif
46
47
/*
48
    There is no plain middle-side option ; instead there are various mixing
49
    modes including middle-side, each lossless, as embodied in the mix ()
50
    and unmix () functions.  These functions exploit a generalized middle-side
51
    transformation:
52
53
    u := [(rL + (m-r)R)/m] ;
54
    v := L - R ;
55
56
    where [ ] denotes integer floor.  The (lossless) inverse is
57
58
    L = u + v - [rV/m] ;
59
    R = L - v ;
60
*/
61
62
// 16-bit routines
63
64
void
65
unmix16 (const int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres)
66
877
{
67
877
  int32_t   j ;
68
69
877
  if (mixres != 0)
70
152
  {
71
    /* matrixed stereo */
72
3.10k
    for (j = 0 ; j < numSamples ; j++)
73
2.95k
    {
74
2.95k
      int32_t   l, r ;
75
76
2.95k
      l = u [j] + v [j] - ((mixres * v [j]) >> mixbits) ;
77
2.95k
      r = l - v [j] ;
78
79
2.95k
      out [0] = arith_shift_left (l, 16) ;
80
2.95k
      out [1] = arith_shift_left (r, 16) ;
81
2.95k
      out += stride ;
82
2.95k
    }
83
152
  }
84
725
  else
85
725
  {
86
    /* Conventional separated stereo. */
87
79.4k
    for (j = 0 ; j < numSamples ; j++)
88
78.7k
    {
89
78.7k
      out [0] = arith_shift_left (u [j], 16) ;
90
78.7k
      out [1] = arith_shift_left (v [j], 16) ;
91
78.7k
      out += stride ;
92
78.7k
    }
93
725
  }
94
877
}
95
96
// 20-bit routines
97
// - the 20 bits of data are left-justified in 3 bytes of storage but right-aligned for input/output predictor buffers
98
99
void
100
unmix20 (const int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t numSamples, int32_t mixbits, int32_t mixres)
101
772
{
102
772
  int32_t   j ;
103
104
772
  if (mixres != 0)
105
179
  {
106
    /* matrixed stereo */
107
3.14k
    for (j = 0 ; j < numSamples ; j++)
108
2.96k
    {
109
2.96k
      int32_t   l, r ;
110
111
2.96k
      l = u [j] + v [j] - ((mixres * v [j]) >> mixbits) ;
112
2.96k
      r = l - v [j] ;
113
114
2.96k
      out [0] = arith_shift_left (l, 12) ;
115
2.96k
      out [1] = arith_shift_left (r, 12) ;
116
2.96k
      out += stride ;
117
2.96k
    }
118
179
  }
119
593
  else
120
593
  {
121
    /* Conventional separated stereo. */
122
68.4k
    for (j = 0 ; j < numSamples ; j++)
123
67.8k
    {
124
67.8k
      out [0] = arith_shift_left (u [j], 12) ;
125
67.8k
      out [1] = arith_shift_left (v [j], 12) ;
126
67.8k
      out += stride ;
127
67.8k
    }
128
593
  }
129
772
}
130
131
// 24-bit routines
132
// - the 24 bits of data are right-justified in the input/output predictor buffers
133
134
void
135
unmix24 (const int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t numSamples,
136
        int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted)
137
998
{
138
998
  int32_t   shift = bytesShifted * 8 ;
139
998
  int32_t   l, r ;
140
998
  int32_t     j, k ;
141
142
998
  if (mixres != 0)
143
271
  {
144
    /* matrixed stereo */
145
271
    if (bytesShifted != 0)
146
133
    {
147
2.90k
      for (j = 0, k = 0 ; j < numSamples ; j++, k += 2)
148
2.77k
      {
149
2.77k
        l = u [j] + v [j] - ((mixres * v [j]) >> mixbits) ;
150
2.77k
        r = l - v [j] ;
151
152
2.77k
        l = arith_shift_left (l, shift) | (uint32_t) shiftUV [k + 0] ;
153
2.77k
        r = arith_shift_left (r, shift) | (uint32_t) shiftUV [k + 1] ;
154
155
2.77k
        out [0] = arith_shift_left (l, 8) ;
156
2.77k
        out [1] = arith_shift_left (r, 8) ;
157
2.77k
        out += stride ;
158
2.77k
      }
159
133
    }
160
138
    else
161
138
    {
162
3.96k
      for (j = 0 ; j < numSamples ; j++)
163
3.82k
      {
164
3.82k
        l = u [j] + v [j] - ((mixres * v [j]) >> mixbits) ;
165
3.82k
        r = l - v [j] ;
166
167
3.82k
        out [0] = arith_shift_left (l, 8) ;
168
3.82k
        out [1] = arith_shift_left (r, 8) ;
169
3.82k
        out += stride ;
170
3.82k
      }
171
138
    }
172
271
  }
173
727
  else
174
727
  {
175
    /* Conventional separated stereo. */
176
727
    if (bytesShifted != 0)
177
134
    {
178
6.85k
      for (j = 0, k = 0 ; j < numSamples ; j++, k += 2)
179
6.71k
      {
180
6.71k
        l = u [j] ;
181
6.71k
        r = v [j] ;
182
183
6.71k
        l = arith_shift_left (l, shift) | (uint32_t) shiftUV [k + 0] ;
184
6.71k
        r = arith_shift_left (r, shift) | (uint32_t) shiftUV [k + 1] ;
185
186
6.71k
        out [0] = arith_shift_left (l, 8) ;
187
6.71k
        out [1] = arith_shift_left (r, 8) ;
188
6.71k
        out += stride ;
189
6.71k
      }
190
134
    }
191
593
    else
192
593
    {
193
22.0k
      for (j = 0 ; j < numSamples ; j++)
194
21.4k
      {
195
21.4k
        out [0] = arith_shift_left (u [j], 8) ;
196
21.4k
        out [1] = arith_shift_left (v [j], 8) ;
197
21.4k
        out += stride ;
198
21.4k
      }
199
593
    }
200
727
  }
201
998
}
202
203
// 32-bit routines
204
// - note that these really expect the internal data width to be < 32 but the arrays are 32-bit
205
// - otherwise, the calculations might overflow into the 33rd bit and be lost
206
// - therefore, these routines deal with the specified "unused lower" bytes in the "shift" buffers
207
208
void
209
unmix32 (const int32_t * u, int32_t * v, int32_t * out, uint32_t stride, int32_t numSamples,
210
        int32_t mixbits, int32_t mixres, uint16_t * shiftUV, int32_t bytesShifted)
211
1.31k
{
212
1.31k
  int32_t   shift = bytesShifted * 8 ;
213
1.31k
  int32_t   l, r ;
214
1.31k
  int32_t   j, k ;
215
216
1.31k
  if (mixres != 0)
217
107
  {
218
    //Assert (bytesShifted != 0) ;
219
220
    /* matrixed stereo with shift */
221
3.51k
    for (j = 0, k = 0 ; j < numSamples ; j++, k += 2)
222
3.40k
    {
223
3.40k
      int32_t   lt, rt ;
224
225
3.40k
      lt = u [j] ;
226
3.40k
      rt = v [j] ;
227
228
3.40k
      l = lt + rt - ((mixres * rt) >> mixbits) ;
229
3.40k
      r = l - rt ;
230
231
3.40k
      out [0] = arith_shift_left (l, shift) | (uint32_t) shiftUV [k + 0] ;
232
3.40k
      out [1] = arith_shift_left (r, shift) | (uint32_t) shiftUV [k + 1] ;
233
3.40k
      out += stride ;
234
3.40k
    }
235
107
  }
236
1.20k
  else
237
1.20k
  {
238
1.20k
    if (bytesShifted == 0)
239
1.10k
    {
240
      /* interleaving w/o shift */
241
92.3k
      for (j = 0 ; j < numSamples ; j++)
242
91.2k
      {
243
91.2k
        out [0] = u [j] ;
244
91.2k
        out [1] = v [j] ;
245
91.2k
        out += stride ;
246
91.2k
      }
247
1.10k
    }
248
99
    else
249
99
    {
250
      /* interleaving with shift */
251
2.61k
      for (j = 0, k = 0 ; j < numSamples ; j++, k += 2)
252
2.51k
      {
253
2.51k
        out [0] = arith_shift_left (u [j], shift) | (uint32_t) shiftUV [k + 0] ;
254
2.51k
        out [1] = arith_shift_left (v [j], shift) | (uint32_t) shiftUV [k + 1] ;
255
2.51k
        out += stride ;
256
2.51k
      }
257
99
    }
258
1.20k
  }
259
1.31k
}
260
261
// 20/24-bit <-> 32-bit helper routines (not really matrixing but convenient to put here)
262
263
void
264
copyPredictorTo24 (const int32_t * in, int32_t * out, uint32_t stride, int32_t numSamples)
265
3.70k
{
266
3.70k
  int32_t   j ;
267
268
355k
  for (j = 0 ; j < numSamples ; j++)
269
352k
  {
270
352k
    out [0] = arith_shift_left (in [j], 8) ;
271
352k
    out += stride ;
272
352k
  }
273
3.70k
}
274
275
void
276
copyPredictorTo24Shift (const int32_t * in, uint16_t * shift, int32_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted)
277
424
{
278
424
  int32_t   shiftVal = bytesShifted * 8 ;
279
424
  int32_t   j ;
280
281
  //Assert (bytesShifted != 0) ;
282
283
2.45k
  for (j = 0 ; j < numSamples ; j++)
284
2.03k
  {
285
2.03k
    int32_t   val = in [j] ;
286
287
2.03k
    val = arith_shift_left (val, shiftVal) | (uint32_t) shift [j] ;
288
2.03k
    out [0] = arith_shift_left (val, 8) ;
289
2.03k
    out += stride ;
290
2.03k
  }
291
424
}
292
293
void
294
copyPredictorTo20 (const int32_t * in, int32_t * out, uint32_t stride, int32_t numSamples)
295
2.56k
{
296
2.56k
  int32_t   j ;
297
298
  // 32-bit predictor values are right-aligned but 20-bit output values should be left-aligned
299
  // in the 24-bit output buffer
300
78.6k
  for (j = 0 ; j < numSamples ; j++)
301
76.1k
  {
302
76.1k
    out [0] = arith_shift_left (in [j], 12) ;
303
76.1k
    out += stride ;
304
76.1k
  }
305
2.56k
}
306
307
void
308
copyPredictorTo32 (const int32_t * in, int32_t * out, uint32_t stride, int32_t numSamples)
309
3.72k
{
310
3.72k
  int32_t     i, j ;
311
312
  // this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
313
156k
  for (i = 0, j = 0 ; i < numSamples ; i++, j += stride)
314
152k
    out [j] = arith_shift_left (in [i], 8) ;
315
3.72k
}
316
317
void
318
copyPredictorTo32Shift (const int32_t * in, uint16_t * shift, int32_t * out, uint32_t stride, int32_t numSamples, int32_t bytesShifted)
319
375
{
320
375
  int32_t *   op = out ;
321
375
  uint32_t    shiftVal = bytesShifted * 8 ;
322
375
  int32_t     j ;
323
324
  //Assert (bytesShifted != 0) ;
325
326
  // this is only a subroutine to abstract the "iPod can only output 16-bit data" problem
327
1.53k
  for (j = 0 ; j < numSamples ; j++)
328
1.16k
  {
329
1.16k
    op [0] = arith_shift_left (in [j], shiftVal) | (uint32_t) shift [j] ;
330
1.16k
    op += stride ;
331
1.16k
  }
332
375
}