Coverage Report

Created: 2026-12-01 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libAACdec/src/stereo.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/**************************** AAC decoder library ******************************
96
97
   Author(s):   Josef Hoepfl
98
99
   Description: joint stereo processing
100
101
*******************************************************************************/
102
103
#include "stereo.h"
104
105
#include "aac_rom.h"
106
#include "FDK_bitstream.h"
107
#include "channelinfo.h"
108
#include "FDK_audio.h"
109
110
enum { L = 0, R = 1 };
111
112
#include "block.h"
113
114
int CJointStereo_Read(HANDLE_FDK_BITSTREAM bs,
115
                      CJointStereoData *pJointStereoData,
116
                      const int windowGroups,
117
                      const int scaleFactorBandsTransmitted,
118
                      const int max_sfb_ste_clear,
119
                      CJointStereoPersistentData *pJointStereoPersistentData,
120
                      CCplxPredictionData *cplxPredictionData,
121
                      int cplxPredictionActiv, int scaleFactorBandsTotal,
122
201k
                      int windowSequence, const UINT flags) {
123
201k
  int group, band;
124
125
201k
  pJointStereoData->MsMaskPresent = (UCHAR)FDKreadBits(bs, 2);
126
127
201k
  FDKmemclear(pJointStereoData->MsUsed,
128
201k
              scaleFactorBandsTransmitted * sizeof(UCHAR));
129
130
201k
  pJointStereoData->cplx_pred_flag = 0;
131
201k
  if (cplxPredictionActiv) {
132
68.9k
    cplxPredictionData->pred_dir = 0;
133
68.9k
    cplxPredictionData->complex_coef = 0;
134
68.9k
    cplxPredictionData->use_prev_frame = 0;
135
68.9k
    cplxPredictionData->igf_pred_dir = 0;
136
68.9k
  }
137
138
201k
  switch (pJointStereoData->MsMaskPresent) {
139
76.4k
    case 0: /* no M/S */
140
      /* all flags are already cleared */
141
76.4k
      break;
142
143
23.2k
    case 1: /* read ms_used */
144
55.3k
      for (group = 0; group < windowGroups; group++) {
145
108k
        for (band = 0; band < scaleFactorBandsTransmitted; band++) {
146
76.9k
          pJointStereoData->MsUsed[band] |= (FDKreadBits(bs, 1) << group);
147
76.9k
        }
148
32.0k
      }
149
23.2k
      break;
150
151
31.4k
    case 2: /* full spectrum M/S */
152
125k
      for (band = 0; band < scaleFactorBandsTransmitted; band++) {
153
94.0k
        pJointStereoData->MsUsed[band] = 255; /* set all flags to 1 */
154
94.0k
      }
155
31.4k
      break;
156
157
70.7k
    case 3:
158
      /* M/S coding is disabled, complex stereo prediction is enabled */
159
70.7k
      if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) {
160
64.4k
        if (cplxPredictionActiv) { /* 'if (stereoConfigIndex == 0)' */
161
162
64.4k
          pJointStereoData->cplx_pred_flag = 1;
163
164
          /* cplx_pred_data()  cp. ISO/IEC FDIS 23003-3:2011(E)  Table 26 */
165
64.4k
          int cplx_pred_all = 0; /* local use only */
166
64.4k
          cplx_pred_all = FDKreadBits(bs, 1);
167
168
64.4k
          if (cplx_pred_all) {
169
182k
            for (group = 0; group < windowGroups; group++) {
170
157k
              UCHAR groupmask = ((UCHAR)1 << group);
171
238k
              for (band = 0; band < scaleFactorBandsTransmitted; band++) {
172
81.0k
                pJointStereoData->MsUsed[band] |= groupmask;
173
81.0k
              }
174
157k
            }
175
39.8k
          } else {
176
107k
            for (group = 0; group < windowGroups; group++) {
177
172k
              for (band = 0; band < scaleFactorBandsTransmitted;
178
105k
                   band += SFB_PER_PRED_BAND) {
179
105k
                pJointStereoData->MsUsed[band] |= (FDKreadBits(bs, 1) << group);
180
105k
                if ((band + 1) < scaleFactorBandsTotal) {
181
105k
                  pJointStereoData->MsUsed[band + 1] |=
182
105k
                      (pJointStereoData->MsUsed[band] & ((UCHAR)1 << group));
183
105k
                }
184
105k
              }
185
67.2k
            }
186
39.8k
          }
187
64.4k
        } else {
188
5
          return -1;
189
5
        }
190
64.4k
      }
191
70.7k
      break;
192
201k
  }
193
194
201k
  if (cplxPredictionActiv) {
195
    /* If all sfb are MS-ed then no complex prediction */
196
68.9k
    if (pJointStereoData->MsMaskPresent == 3) {
197
64.4k
      if (pJointStereoData->cplx_pred_flag) {
198
64.4k
        int delta_code_time = 0;
199
200
        /* set pointer to Huffman codebooks */
201
64.4k
        const CodeBookDescription *hcb = &AACcodeBookDescriptionTable[BOOKSCL];
202
        /* set predictors to zero in case of a transition from long to short
203
         * window sequences and vice versa */
204
64.4k
        if (((windowSequence == BLOCK_SHORT) &&
205
28.1k
             (pJointStereoPersistentData->winSeqPrev != BLOCK_SHORT)) ||
206
62.0k
            ((pJointStereoPersistentData->winSeqPrev == BLOCK_SHORT) &&
207
27.4k
             (windowSequence != BLOCK_SHORT))) {
208
3.98k
          FDKmemclear(pJointStereoPersistentData->alpha_q_re_prev,
209
3.98k
                      JointStereoMaximumGroups * JointStereoMaximumBands *
210
3.98k
                          sizeof(SHORT));
211
3.98k
          FDKmemclear(pJointStereoPersistentData->alpha_q_im_prev,
212
3.98k
                      JointStereoMaximumGroups * JointStereoMaximumBands *
213
3.98k
                          sizeof(SHORT));
214
3.98k
        }
215
64.4k
        {
216
64.4k
          FDKmemclear(cplxPredictionData->alpha_q_re,
217
64.4k
                      JointStereoMaximumGroups * JointStereoMaximumBands *
218
64.4k
                          sizeof(SHORT));
219
64.4k
          FDKmemclear(cplxPredictionData->alpha_q_im,
220
64.4k
                      JointStereoMaximumGroups * JointStereoMaximumBands *
221
64.4k
                          sizeof(SHORT));
222
64.4k
        }
223
224
        /* 0 = mid->side prediction, 1 = side->mid prediction */
225
64.4k
        cplxPredictionData->pred_dir = FDKreadBits(bs, 1);
226
64.4k
        cplxPredictionData->complex_coef = FDKreadBits(bs, 1);
227
228
64.4k
        if (cplxPredictionData->complex_coef) {
229
50.2k
          if (flags & AC_INDEP) {
230
48.2k
            cplxPredictionData->use_prev_frame = 0;
231
48.2k
          } else {
232
2.00k
            cplxPredictionData->use_prev_frame = FDKreadBits(bs, 1);
233
2.00k
          }
234
50.2k
        }
235
236
64.4k
        if (flags & AC_INDEP) {
237
62.2k
          delta_code_time = 0;
238
62.2k
        } else {
239
2.18k
          delta_code_time = FDKreadBits(bs, 1);
240
2.18k
        }
241
242
64.4k
        {
243
64.4k
          int last_alpha_q_re = 0, last_alpha_q_im = 0;
244
245
289k
          for (group = 0; group < windowGroups; group++) {
246
371k
            for (band = 0; band < scaleFactorBandsTransmitted;
247
224k
                 band += SFB_PER_PRED_BAND) {
248
146k
              if (delta_code_time == 1) {
249
16.2k
                if (group > 0) {
250
13.1k
                  last_alpha_q_re =
251
13.1k
                      cplxPredictionData->alpha_q_re[group - 1][band];
252
13.1k
                  last_alpha_q_im =
253
13.1k
                      cplxPredictionData->alpha_q_im[group - 1][band];
254
13.1k
                } else if ((windowSequence == BLOCK_SHORT) &&
255
2.34k
                           (pJointStereoPersistentData->winSeqPrev ==
256
2.34k
                            BLOCK_SHORT)) {
257
                  /* Included for error-robustness */
258
327
                  if (pJointStereoPersistentData->winGroupsPrev == 0) return -1;
259
260
326
                  last_alpha_q_re =
261
326
                      pJointStereoPersistentData->alpha_q_re_prev
262
326
                          [pJointStereoPersistentData->winGroupsPrev - 1][band];
263
326
                  last_alpha_q_im =
264
326
                      pJointStereoPersistentData->alpha_q_im_prev
265
326
                          [pJointStereoPersistentData->winGroupsPrev - 1][band];
266
2.73k
                } else {
267
2.73k
                  last_alpha_q_re =
268
2.73k
                      pJointStereoPersistentData->alpha_q_re_prev[group][band];
269
2.73k
                  last_alpha_q_im =
270
2.73k
                      pJointStereoPersistentData->alpha_q_im_prev[group][band];
271
2.73k
                }
272
273
130k
              } else {
274
130k
                if (band > 0) {
275
67.7k
                  last_alpha_q_re =
276
67.7k
                      cplxPredictionData->alpha_q_re[group][band - 1];
277
67.7k
                  last_alpha_q_im =
278
67.7k
                      cplxPredictionData->alpha_q_im[group][band - 1];
279
67.7k
                } else {
280
62.7k
                  last_alpha_q_re = 0;
281
62.7k
                  last_alpha_q_im = 0;
282
62.7k
                }
283
284
130k
              } /* if (delta_code_time == 1) */
285
286
146k
              if (pJointStereoData->MsUsed[band] & ((UCHAR)1 << group)) {
287
92.8k
                int dpcm_alpha_re, dpcm_alpha_im;
288
289
92.8k
                dpcm_alpha_re = CBlock_DecodeHuffmanWord(bs, hcb);
290
92.8k
                dpcm_alpha_re -= 60;
291
92.8k
                dpcm_alpha_re *= -1;
292
293
92.8k
                cplxPredictionData->alpha_q_re[group][band] =
294
92.8k
                    dpcm_alpha_re + last_alpha_q_re;
295
296
92.8k
                if (cplxPredictionData->complex_coef) {
297
76.7k
                  dpcm_alpha_im = CBlock_DecodeHuffmanWord(bs, hcb);
298
76.7k
                  dpcm_alpha_im -= 60;
299
76.7k
                  dpcm_alpha_im *= -1;
300
301
76.7k
                  cplxPredictionData->alpha_q_im[group][band] =
302
76.7k
                      dpcm_alpha_im + last_alpha_q_im;
303
76.7k
                } else {
304
16.1k
                  cplxPredictionData->alpha_q_im[group][band] = 0;
305
16.1k
                }
306
307
92.8k
              } else {
308
53.9k
                cplxPredictionData->alpha_q_re[group][band] = 0;
309
53.9k
                cplxPredictionData->alpha_q_im[group][band] = 0;
310
53.9k
              } /* if (pJointStereoData->MsUsed[band] & ((UCHAR)1 << group)) */
311
312
146k
              if ((band + 1) <
313
146k
                  scaleFactorBandsTransmitted) { /* <= this should be the
314
                                                    correct way (cp.
315
                                                    ISO_IEC_FDIS_23003-0(E) */
316
                /*    7.7.2.3.2 Decoding of prediction coefficients) */
317
90.9k
                cplxPredictionData->alpha_q_re[group][band + 1] =
318
90.9k
                    cplxPredictionData->alpha_q_re[group][band];
319
90.9k
                cplxPredictionData->alpha_q_im[group][band + 1] =
320
90.9k
                    cplxPredictionData->alpha_q_im[group][band];
321
90.9k
              } /* if ((band+1)<scaleFactorBandsTotal) */
322
323
146k
              pJointStereoPersistentData->alpha_q_re_prev[group][band] =
324
146k
                  cplxPredictionData->alpha_q_re[group][band];
325
146k
              pJointStereoPersistentData->alpha_q_im_prev[group][band] =
326
146k
                  cplxPredictionData->alpha_q_im[group][band];
327
146k
            }
328
329
14.3M
            for (band = scaleFactorBandsTransmitted; band < max_sfb_ste_clear;
330
14.1M
                 band++) {
331
14.1M
              cplxPredictionData->alpha_q_re[group][band] = 0;
332
14.1M
              cplxPredictionData->alpha_q_im[group][band] = 0;
333
14.1M
              pJointStereoPersistentData->alpha_q_re_prev[group][band] = 0;
334
14.1M
              pJointStereoPersistentData->alpha_q_im_prev[group][band] = 0;
335
14.1M
            }
336
224k
          }
337
64.4k
        }
338
64.4k
      }
339
64.4k
    } else {
340
27.6k
      for (group = 0; group < windowGroups; group++) {
341
1.50M
        for (band = 0; band < max_sfb_ste_clear; band++) {
342
1.48M
          pJointStereoPersistentData->alpha_q_re_prev[group][band] = 0;
343
1.48M
          pJointStereoPersistentData->alpha_q_im_prev[group][band] = 0;
344
1.48M
        }
345
23.2k
      }
346
4.47k
    }
347
348
68.9k
    pJointStereoPersistentData->winGroupsPrev = windowGroups;
349
68.9k
  }
350
351
201k
  return 0;
352
201k
}
353
354
static void CJointStereo_filterAndAdd(
355
    FIXP_DBL *in, int len, int windowLen, const FIXP_FILT *coeff, FIXP_DBL *out,
356
    UCHAR isCurrent /* output values with even index get a
357
                       positve addon (=1) or a negative addon
358
                       (=0) */
359
423k
) {
360
423k
  int i, j;
361
362
423k
  int indices_1[] = {2, 1, 0, 1, 2, 3};
363
423k
  int indices_2[] = {1, 0, 0, 2, 3, 4};
364
423k
  int indices_3[] = {0, 0, 1, 3, 4, 5};
365
366
423k
  int subtr_1[] = {6, 5, 4, 2, 1, 1};
367
423k
  int subtr_2[] = {5, 4, 3, 1, 1, 2};
368
423k
  int subtr_3[] = {4, 3, 2, 1, 2, 3};
369
370
423k
  if (isCurrent == 1) {
371
    /* exploit the symmetry of the table: coeff[6] = - coeff[0],
372
                                          coeff[5] = - coeff[1],
373
                                          coeff[4] = - coeff[2],
374
                                          coeff[3] = 0
375
    */
376
377
943k
    for (i = 0; i < 3; i++) {
378
707k
      out[0] -= (FIXP_DBL)fMultDiv2(coeff[i], in[indices_1[i]]) >> SR_FNA_OUT;
379
707k
      out[0] +=
380
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[indices_1[5 - i]]) >> SR_FNA_OUT;
381
707k
    }
382
383
943k
    for (i = 0; i < 3; i++) {
384
707k
      out[1] -= (FIXP_DBL)fMultDiv2(coeff[i], in[indices_2[i]]) >> SR_FNA_OUT;
385
707k
      out[1] +=
386
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[indices_2[5 - i]]) >> SR_FNA_OUT;
387
707k
    }
388
389
943k
    for (i = 0; i < 3; i++) {
390
707k
      out[2] -= (FIXP_DBL)fMultDiv2(coeff[i], in[indices_3[i]]) >> SR_FNA_OUT;
391
707k
      out[2] +=
392
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[indices_3[5 - i]]) >> SR_FNA_OUT;
393
707k
    }
394
395
2.03M
    for (j = 3; j < (len - 3); j++) {
396
7.19M
      for (i = 0; i < 3; i++) {
397
5.39M
        out[j] -= (FIXP_DBL)fMultDiv2(coeff[i], in[j - 3 + i]) >> SR_FNA_OUT;
398
5.39M
        out[j] += (FIXP_DBL)fMultDiv2(coeff[i], in[j + 3 - i]) >> SR_FNA_OUT;
399
5.39M
      }
400
1.79M
    }
401
402
943k
    for (i = 0; i < 3; i++) {
403
707k
      out[len - 3] -=
404
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_1[i]]) >> SR_FNA_OUT;
405
707k
      out[len - 3] +=
406
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_1[5 - i]]) >> SR_FNA_OUT;
407
707k
    }
408
409
943k
    for (i = 0; i < 3; i++) {
410
707k
      out[len - 2] -=
411
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_2[i]]) >> SR_FNA_OUT;
412
707k
      out[len - 2] +=
413
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_2[5 - i]]) >> SR_FNA_OUT;
414
707k
    }
415
416
943k
    for (i = 0; i < 3; i++) {
417
707k
      out[len - 1] -=
418
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_3[i]]) >> SR_FNA_OUT;
419
707k
      out[len - 1] +=
420
707k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_3[5 - i]]) >> SR_FNA_OUT;
421
707k
    }
422
423
235k
  } else {
424
    /* exploit the symmetry of the table: coeff[6] = coeff[0],
425
                                          coeff[5] = coeff[1],
426
                                          coeff[4] = coeff[2]
427
    */
428
429
750k
    for (i = 0; i < 3; i++) {
430
563k
      out[0] -= (FIXP_DBL)fMultDiv2(coeff[i], in[indices_1[i]] >> SR_FNA_OUT);
431
563k
      out[0] -=
432
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[indices_1[5 - i]] >> SR_FNA_OUT);
433
563k
    }
434
187k
    out[0] -= (FIXP_DBL)fMultDiv2(coeff[3], in[0] >> SR_FNA_OUT);
435
436
750k
    for (i = 0; i < 3; i++) {
437
563k
      out[1] += (FIXP_DBL)fMultDiv2(coeff[i], in[indices_2[i]] >> SR_FNA_OUT);
438
563k
      out[1] +=
439
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[indices_2[5 - i]] >> SR_FNA_OUT);
440
563k
    }
441
187k
    out[1] += (FIXP_DBL)fMultDiv2(coeff[3], in[1] >> SR_FNA_OUT);
442
443
750k
    for (i = 0; i < 3; i++) {
444
563k
      out[2] -= (FIXP_DBL)fMultDiv2(coeff[i], in[indices_3[i]] >> SR_FNA_OUT);
445
563k
      out[2] -=
446
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[indices_3[5 - i]] >> SR_FNA_OUT);
447
563k
    }
448
187k
    out[2] -= (FIXP_DBL)fMultDiv2(coeff[3], in[2] >> SR_FNA_OUT);
449
450
529k
    for (j = 3; j < (len - 4); j++) {
451
1.36M
      for (i = 0; i < 3; i++) {
452
1.02M
        out[j] += (FIXP_DBL)fMultDiv2(coeff[i], in[j - 3 + i] >> SR_FNA_OUT);
453
1.02M
        out[j] += (FIXP_DBL)fMultDiv2(coeff[i], in[j + 3 - i] >> SR_FNA_OUT);
454
1.02M
      }
455
341k
      out[j] += (FIXP_DBL)fMultDiv2(coeff[3], in[j] >> SR_FNA_OUT);
456
457
341k
      j++;
458
459
1.36M
      for (i = 0; i < 3; i++) {
460
1.02M
        out[j] -= (FIXP_DBL)fMultDiv2(coeff[i], in[j - 3 + i] >> SR_FNA_OUT);
461
1.02M
        out[j] -= (FIXP_DBL)fMultDiv2(coeff[i], in[j + 3 - i] >> SR_FNA_OUT);
462
1.02M
      }
463
341k
      out[j] -= (FIXP_DBL)fMultDiv2(coeff[3], in[j] >> SR_FNA_OUT);
464
341k
    }
465
466
750k
    for (i = 0; i < 3; i++) {
467
563k
      out[len - 3] +=
468
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_1[i]] >> SR_FNA_OUT);
469
563k
      out[len - 3] +=
470
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_1[5 - i]] >> SR_FNA_OUT);
471
563k
    }
472
187k
    out[len - 3] += (FIXP_DBL)fMultDiv2(coeff[3], in[len - 3] >> SR_FNA_OUT);
473
474
750k
    for (i = 0; i < 3; i++) {
475
563k
      out[len - 2] -=
476
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_2[i]] >> SR_FNA_OUT);
477
563k
      out[len - 2] -=
478
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_2[5 - i]] >> SR_FNA_OUT);
479
563k
    }
480
187k
    out[len - 2] -= (FIXP_DBL)fMultDiv2(coeff[3], in[len - 2] >> SR_FNA_OUT);
481
482
750k
    for (i = 0; i < 3; i++) {
483
563k
      out[len - 1] +=
484
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_3[i]] >> SR_FNA_OUT);
485
563k
      out[len - 1] +=
486
563k
          (FIXP_DBL)fMultDiv2(coeff[i], in[len - subtr_3[5 - i]] >> SR_FNA_OUT);
487
563k
    }
488
187k
    out[len - 1] += (FIXP_DBL)fMultDiv2(coeff[3], in[len - 1] >> SR_FNA_OUT);
489
187k
  }
490
423k
}
491
492
static inline void CJointStereo_GenerateMSOutput(FIXP_DBL *pSpecLCurrBand,
493
                                                 FIXP_DBL *pSpecRCurrBand,
494
                                                 UINT leftScale,
495
                                                 UINT rightScale,
496
205k
                                                 UINT nSfbBands) {
497
205k
  unsigned int i;
498
499
205k
  FIXP_DBL leftCoefficient0;
500
205k
  FIXP_DBL leftCoefficient1;
501
205k
  FIXP_DBL leftCoefficient2;
502
205k
  FIXP_DBL leftCoefficient3;
503
504
205k
  FIXP_DBL rightCoefficient0;
505
205k
  FIXP_DBL rightCoefficient1;
506
205k
  FIXP_DBL rightCoefficient2;
507
205k
  FIXP_DBL rightCoefficient3;
508
509
506k
  for (i = nSfbBands; i > 0; i -= 4) {
510
301k
    leftCoefficient0 = pSpecLCurrBand[i - 4];
511
301k
    leftCoefficient1 = pSpecLCurrBand[i - 3];
512
301k
    leftCoefficient2 = pSpecLCurrBand[i - 2];
513
301k
    leftCoefficient3 = pSpecLCurrBand[i - 1];
514
515
301k
    rightCoefficient0 = pSpecRCurrBand[i - 4];
516
301k
    rightCoefficient1 = pSpecRCurrBand[i - 3];
517
301k
    rightCoefficient2 = pSpecRCurrBand[i - 2];
518
301k
    rightCoefficient3 = pSpecRCurrBand[i - 1];
519
520
    /* MS output generation */
521
301k
    leftCoefficient0 >>= leftScale;
522
301k
    leftCoefficient1 >>= leftScale;
523
301k
    leftCoefficient2 >>= leftScale;
524
301k
    leftCoefficient3 >>= leftScale;
525
526
301k
    rightCoefficient0 >>= rightScale;
527
301k
    rightCoefficient1 >>= rightScale;
528
301k
    rightCoefficient2 >>= rightScale;
529
301k
    rightCoefficient3 >>= rightScale;
530
531
301k
    pSpecLCurrBand[i - 4] = leftCoefficient0 + rightCoefficient0;
532
301k
    pSpecLCurrBand[i - 3] = leftCoefficient1 + rightCoefficient1;
533
301k
    pSpecLCurrBand[i - 2] = leftCoefficient2 + rightCoefficient2;
534
301k
    pSpecLCurrBand[i - 1] = leftCoefficient3 + rightCoefficient3;
535
536
301k
    pSpecRCurrBand[i - 4] = leftCoefficient0 - rightCoefficient0;
537
301k
    pSpecRCurrBand[i - 3] = leftCoefficient1 - rightCoefficient1;
538
301k
    pSpecRCurrBand[i - 2] = leftCoefficient2 - rightCoefficient2;
539
301k
    pSpecRCurrBand[i - 1] = leftCoefficient3 - rightCoefficient3;
540
301k
  }
541
205k
}
542
543
void CJointStereo_ApplyMS(
544
    CAacDecoderChannelInfo *pAacDecoderChannelInfo[2],
545
    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[2],
546
    FIXP_DBL *spectrumL, FIXP_DBL *spectrumR, SHORT *SFBleftScale,
547
    SHORT *SFBrightScale, SHORT *specScaleL, SHORT *specScaleR,
548
    const SHORT *pScaleFactorBandOffsets, const UCHAR *pWindowGroupLength,
549
    const int windowGroups, const int max_sfb_ste_outside,
550
    const int scaleFactorBandsTransmittedL,
551
    const int scaleFactorBandsTransmittedR, FIXP_DBL *store_dmx_re_prev,
552
198k
    SHORT *store_dmx_re_prev_e, const int mainband_flag) {
553
198k
  int window, group, band;
554
198k
  UCHAR groupMask;
555
198k
  CJointStereoData *pJointStereoData =
556
198k
      &pAacDecoderChannelInfo[L]->pComData->jointStereoData;
557
198k
  CCplxPredictionData *cplxPredictionData =
558
198k
      pAacDecoderChannelInfo[L]->pComStaticData->cplxPredictionData;
559
560
198k
  int max_sfb_ste =
561
198k
      fMax(scaleFactorBandsTransmittedL, scaleFactorBandsTransmittedR);
562
198k
  int min_sfb_ste =
563
198k
      fMin(scaleFactorBandsTransmittedL, scaleFactorBandsTransmittedR);
564
198k
  int scaleFactorBandsTransmitted = min_sfb_ste;
565
566
198k
  if (pJointStereoData->cplx_pred_flag) {
567
64.2k
    int windowLen, groupwin, frameMaxScale;
568
64.2k
    CJointStereoPersistentData *pJointStereoPersistentData =
569
64.2k
        &pAacDecoderStaticChannelInfo[L]
570
64.2k
             ->pCpeStaticData->jointStereoPersistentData;
571
64.2k
    FIXP_DBL *const staticSpectralCoeffsL =
572
64.2k
        pAacDecoderStaticChannelInfo[L]
573
64.2k
            ->pCpeStaticData->jointStereoPersistentData.spectralCoeffs[L];
574
64.2k
    FIXP_DBL *const staticSpectralCoeffsR =
575
64.2k
        pAacDecoderStaticChannelInfo[L]
576
64.2k
            ->pCpeStaticData->jointStereoPersistentData.spectralCoeffs[R];
577
64.2k
    SHORT *const staticSpecScaleL =
578
64.2k
        pAacDecoderStaticChannelInfo[L]
579
64.2k
            ->pCpeStaticData->jointStereoPersistentData.specScale[L];
580
64.2k
    SHORT *const staticSpecScaleR =
581
64.2k
        pAacDecoderStaticChannelInfo[L]
582
64.2k
            ->pCpeStaticData->jointStereoPersistentData.specScale[R];
583
584
64.2k
    FIXP_DBL *dmx_re =
585
64.2k
        pAacDecoderStaticChannelInfo[L]
586
64.2k
            ->pCpeStaticData->jointStereoPersistentData.scratchBuffer;
587
64.2k
    FIXP_DBL *dmx_re_prev =
588
64.2k
        pAacDecoderStaticChannelInfo[L]
589
64.2k
            ->pCpeStaticData->jointStereoPersistentData.scratchBuffer +
590
64.2k
        1024;
591
592
    /* When MS is applied over the main band this value gets computed. Otherwise
593
     * (for the tiles) it uses the assigned value */
594
64.2k
    SHORT dmx_re_prev_e = *store_dmx_re_prev_e;
595
596
64.2k
    const FIXP_FILT *pCoeff;
597
64.2k
    const FIXP_FILT *pCoeffPrev;
598
64.2k
    int coeffPointerOffset;
599
600
64.2k
    int previousShape = (int)pJointStereoPersistentData->winShapePrev;
601
64.2k
    int currentShape = (int)pAacDecoderChannelInfo[L]->icsInfo.WindowShape;
602
603
    /* complex stereo prediction */
604
605
    /* 0. preparations */
606
607
    /* 0.0. get scratch buffer for downmix MDST */
608
64.2k
    C_AALLOC_SCRATCH_START(dmx_im, FIXP_DBL, 1024);
609
610
    /* 0.1. window lengths */
611
612
    /* get length of short window for current configuration */
613
64.2k
    windowLen =
614
64.2k
        pAacDecoderChannelInfo[L]->granuleLength; /* framelength 768 => 96,
615
                                                     framelength 1024 => 128 */
616
617
    /* if this is no short-block set length for long-block */
618
64.2k
    if (pAacDecoderChannelInfo[L]->icsInfo.WindowSequence != BLOCK_SHORT) {
619
36.2k
      windowLen *= 8;
620
36.2k
    }
621
622
    /* 0.2. set pointer to filter-coefficients for MDST excitation including
623
     * previous frame portions */
624
    /*      cp. ISO/IEC FDIS 23003-3:2011(E) table 125 */
625
626
    /* set pointer to default-position */
627
64.2k
    pCoeffPrev = mdst_filt_coef_prev[previousShape];
628
629
64.2k
    if (cplxPredictionData->complex_coef == 1) {
630
50.1k
      switch (pAacDecoderChannelInfo[L]
631
50.1k
                  ->icsInfo.WindowSequence) { /* current window sequence */
632
26.5k
        case BLOCK_SHORT:
633
38.1k
        case BLOCK_LONG:
634
38.1k
          pCoeffPrev = mdst_filt_coef_prev[previousShape];
635
38.1k
          break;
636
637
11.4k
        case BLOCK_START:
638
11.4k
          if ((pJointStereoPersistentData->winSeqPrev == BLOCK_SHORT) ||
639
11.3k
              (pJointStereoPersistentData->winSeqPrev == BLOCK_START)) {
640
            /* a stop-start-sequence can only follow on an eight-short-sequence
641
             * or a start-sequence */
642
9.56k
            pCoeffPrev = mdst_filt_coef_prev[2 + previousShape];
643
9.56k
          } else {
644
1.87k
            pCoeffPrev = mdst_filt_coef_prev[previousShape];
645
1.87k
          }
646
11.4k
          break;
647
648
532
        case BLOCK_STOP:
649
532
          pCoeffPrev = mdst_filt_coef_prev[2 + previousShape];
650
532
          break;
651
652
0
        default:
653
0
          pCoeffPrev = mdst_filt_coef_prev[previousShape];
654
0
          break;
655
50.1k
      }
656
50.1k
    }
657
658
    /* 0.3. set pointer to filter-coefficients for MDST excitation */
659
660
    /* define offset of pointer to filter-coefficients for MDST exitation
661
     * employing only the current frame */
662
64.2k
    if ((previousShape == SHAPE_SINE) && (currentShape == SHAPE_SINE)) {
663
57.4k
      coeffPointerOffset = 0;
664
57.4k
    } else if ((previousShape == SHAPE_SINE) && (currentShape == SHAPE_KBD)) {
665
1.13k
      coeffPointerOffset = 2;
666
5.64k
    } else if ((previousShape == SHAPE_KBD) && (currentShape == SHAPE_KBD)) {
667
1.28k
      coeffPointerOffset = 1;
668
1.28k
    } else /* if ( (previousShape == SHAPE_KBD) && (currentShape == SHAPE_SINE)
669
              ) */
670
4.36k
    {
671
4.36k
      coeffPointerOffset = 3;
672
4.36k
    }
673
674
    /* set pointer to filter-coefficient table cp. ISO/IEC FDIS 23003-3:2011(E)
675
     * table 124 */
676
64.2k
    switch (pAacDecoderChannelInfo[L]
677
64.2k
                ->icsInfo.WindowSequence) { /* current window sequence */
678
28.0k
      case BLOCK_SHORT:
679
42.7k
      case BLOCK_LONG:
680
42.7k
        pCoeff = mdst_filt_coef_curr[coeffPointerOffset];
681
42.7k
        break;
682
683
20.9k
      case BLOCK_START:
684
20.9k
        if ((pJointStereoPersistentData->winSeqPrev == BLOCK_SHORT) ||
685
20.8k
            (pJointStereoPersistentData->winSeqPrev == BLOCK_START)) {
686
          /* a stop-start-sequence can only follow on an eight-short-sequence or
687
           * a start-sequence */
688
17.7k
          pCoeff = mdst_filt_coef_curr[12 + coeffPointerOffset];
689
17.7k
        } else {
690
3.18k
          pCoeff = mdst_filt_coef_curr[4 + coeffPointerOffset];
691
3.18k
        }
692
20.9k
        break;
693
694
599
      case BLOCK_STOP:
695
599
        pCoeff = mdst_filt_coef_curr[8 + coeffPointerOffset];
696
599
        break;
697
698
0
      default:
699
0
        pCoeff = mdst_filt_coef_curr[coeffPointerOffset];
700
64.2k
    }
701
702
    /* 0.4. find maximum common (l/r) band-scaling-factor for whole sequence
703
     * (all windows) */
704
64.2k
    frameMaxScale = 0;
705
288k
    for (window = 0, group = 0; group < windowGroups; group++) {
706
484k
      for (groupwin = 0; groupwin < pWindowGroupLength[group];
707
260k
           groupwin++, window++) {
708
260k
        SHORT *leftScale = &SFBleftScale[window * 16];
709
260k
        SHORT *rightScale = &SFBrightScale[window * 16];
710
260k
        int windowMaxScale = 0;
711
712
        /* find maximum scaling factor of all bands in this window */
713
293k
        for (band = 0; band < min_sfb_ste; band++) {
714
33.0k
          int lScale = leftScale[band];
715
33.0k
          int rScale = rightScale[band];
716
33.0k
          int commonScale = ((lScale > rScale) ? lScale : rScale);
717
33.0k
          windowMaxScale =
718
33.0k
              (windowMaxScale < commonScale) ? commonScale : windowMaxScale;
719
33.0k
        }
720
260k
        if (scaleFactorBandsTransmittedL >
721
260k
            min_sfb_ste) { /* i.e. scaleFactorBandsTransmittedL == max_sfb_ste
722
                            */
723
8.19k
          for (; band < max_sfb_ste; band++) {
724
7.11k
            int lScale = leftScale[band];
725
7.11k
            windowMaxScale =
726
7.11k
                (windowMaxScale < lScale) ? lScale : windowMaxScale;
727
7.11k
          }
728
259k
        } else {
729
259k
          if (scaleFactorBandsTransmittedR >
730
259k
              min_sfb_ste) { /* i.e. scaleFactorBandsTransmittedR == max_sfb_ste
731
                              */
732
264k
            for (; band < max_sfb_ste; band++) {
733
216k
              int rScale = rightScale[band];
734
216k
              windowMaxScale =
735
216k
                  (windowMaxScale < rScale) ? rScale : windowMaxScale;
736
216k
            }
737
47.3k
          }
738
259k
        }
739
740
        /* find maximum common SF of all windows */
741
260k
        frameMaxScale =
742
260k
            (frameMaxScale < windowMaxScale) ? windowMaxScale : frameMaxScale;
743
260k
      }
744
224k
    }
745
746
    /* add some headroom for overflow protection during filter and add operation
747
     */
748
64.2k
    frameMaxScale += 2;
749
750
    /* process on window-basis (i.e. iterate over all groups and corresponding
751
     * windows) */
752
288k
    for (window = 0, group = 0; group < windowGroups; group++) {
753
224k
      groupMask = 1 << group;
754
755
484k
      for (groupwin = 0; groupwin < pWindowGroupLength[group];
756
260k
           groupwin++, window++) {
757
        /* initialize the MDST with zeros */
758
260k
        FDKmemclear(&dmx_im[windowLen * window], windowLen * sizeof(FIXP_DBL));
759
760
        /* 1. calculate the previous downmix MDCT. We do this once just for the
761
         * Main band. */
762
260k
        if (cplxPredictionData->complex_coef == 1) {
763
235k
          if ((cplxPredictionData->use_prev_frame == 1) && (mainband_flag)) {
764
            /* if this is a long-block or the first window of a short-block
765
               calculate the downmix MDCT of the previous frame.
766
               use_prev_frame is assumed not to change during a frame!
767
            */
768
769
            /* first determine shiftfactors to scale left and right channel */
770
13.2k
            if ((pAacDecoderChannelInfo[L]->icsInfo.WindowSequence !=
771
13.2k
                 BLOCK_SHORT) ||
772
12.9k
                (window == 0)) {
773
1.90k
              int index_offset = 0;
774
1.90k
              int srLeftChan = 0;
775
1.90k
              int srRightChan = 0;
776
1.90k
              if (pAacDecoderChannelInfo[L]->icsInfo.WindowSequence ==
777
1.90k
                  BLOCK_SHORT) {
778
                /* use the last window of the previous frame for MDCT
779
                 * calculation if this is a short-block. */
780
1.62k
                index_offset = windowLen * 7;
781
1.62k
                if (staticSpecScaleL[7] > staticSpecScaleR[7]) {
782
82
                  srRightChan = staticSpecScaleL[7] - staticSpecScaleR[7];
783
82
                  dmx_re_prev_e = staticSpecScaleL[7];
784
1.54k
                } else {
785
1.54k
                  srLeftChan = staticSpecScaleR[7] - staticSpecScaleL[7];
786
1.54k
                  dmx_re_prev_e = staticSpecScaleR[7];
787
1.54k
                }
788
1.62k
              } else {
789
281
                if (staticSpecScaleL[0] > staticSpecScaleR[0]) {
790
73
                  srRightChan = staticSpecScaleL[0] - staticSpecScaleR[0];
791
73
                  dmx_re_prev_e = staticSpecScaleL[0];
792
208
                } else {
793
208
                  srLeftChan = staticSpecScaleR[0] - staticSpecScaleL[0];
794
208
                  dmx_re_prev_e = staticSpecScaleR[0];
795
208
                }
796
281
              }
797
798
              /* now scale channels and determine downmix MDCT of previous frame
799
               */
800
1.90k
              if (pAacDecoderStaticChannelInfo[L]
801
1.90k
                      ->pCpeStaticData->jointStereoPersistentData
802
1.90k
                      .clearSpectralCoeffs == 1) {
803
0
                FDKmemclear(dmx_re_prev, windowLen * sizeof(FIXP_DBL));
804
0
                dmx_re_prev_e = 0;
805
1.90k
              } else {
806
1.90k
                if (cplxPredictionData->pred_dir == 0) {
807
235k
                  for (int i = 0; i < windowLen; i++) {
808
234k
                    dmx_re_prev[i] =
809
234k
                        ((staticSpectralCoeffsL[index_offset + i] >>
810
234k
                          fMin(DFRACT_BITS - 1, srLeftChan + 1)) +
811
234k
                         (staticSpectralCoeffsR[index_offset + i] >>
812
234k
                          fMin(DFRACT_BITS - 1, srRightChan + 1)));
813
234k
                  }
814
1.32k
                } else {
815
143k
                  for (int i = 0; i < windowLen; i++) {
816
143k
                    dmx_re_prev[i] =
817
143k
                        ((staticSpectralCoeffsL[index_offset + i] >>
818
143k
                          fMin(DFRACT_BITS - 1, srLeftChan + 1)) -
819
143k
                         (staticSpectralCoeffsR[index_offset + i] >>
820
143k
                          fMin(DFRACT_BITS - 1, srRightChan + 1)));
821
143k
                  }
822
574
                }
823
1.90k
              }
824
825
              /* In case that we use INF we have to preserve the state of the
826
              "dmx_re_prev" (original or computed). This is necessary because we
827
              have to apply MS over the separate IGF tiles. */
828
1.90k
              FDKmemcpy(store_dmx_re_prev, &dmx_re_prev[0],
829
1.90k
                        windowLen * sizeof(FIXP_DBL));
830
831
              /* Particular exponent of the computed/original "dmx_re_prev" must
832
               * be kept for the tile MS calculations if necessary.*/
833
1.90k
              *store_dmx_re_prev_e = dmx_re_prev_e;
834
835
1.90k
            } /* if ( (pAacDecoderChannelInfo[L]->icsInfo.WindowSequence !=
836
                 BLOCK_SHORT) || (window == 0) ) */
837
838
13.2k
          } /* if ( pJointStereoData->use_prev_frame == 1 ) */
839
840
235k
        } /* if ( pJointStereoData->complex_coef == 1 ) */
841
842
        /* 2. calculate downmix MDCT of current frame */
843
844
        /* set pointer to scale-factor-bands of current window */
845
260k
        SHORT *leftScale = &SFBleftScale[window * 16];
846
260k
        SHORT *rightScale = &SFBrightScale[window * 16];
847
848
260k
        specScaleL[window] = specScaleR[window] = frameMaxScale;
849
850
        /* adapt scaling-factors to previous frame */
851
260k
        if (cplxPredictionData->use_prev_frame == 1) {
852
13.2k
          if (window == 0) {
853
1.90k
            if (dmx_re_prev_e < frameMaxScale) {
854
1.54k
              if (mainband_flag == 0) {
855
0
                scaleValues(
856
0
                    dmx_re_prev, store_dmx_re_prev, windowLen,
857
0
                    -fMin(DFRACT_BITS - 1, (frameMaxScale - dmx_re_prev_e)));
858
1.54k
              } else {
859
1.54k
                scaleValues(
860
1.54k
                    dmx_re_prev, windowLen,
861
1.54k
                    -fMin(DFRACT_BITS - 1, (frameMaxScale - dmx_re_prev_e)));
862
1.54k
              }
863
1.54k
            } else {
864
363
              if (mainband_flag == 0) {
865
0
                FDKmemcpy(dmx_re_prev, store_dmx_re_prev,
866
0
                          windowLen * sizeof(FIXP_DBL));
867
0
              }
868
363
              specScaleL[0] = dmx_re_prev_e;
869
363
              specScaleR[0] = dmx_re_prev_e;
870
363
            }
871
11.3k
          } else { /* window != 0 */
872
11.3k
            FDK_ASSERT(pAacDecoderChannelInfo[L]->icsInfo.WindowSequence ==
873
11.3k
                       BLOCK_SHORT);
874
11.3k
            if (specScaleL[window - 1] < frameMaxScale) {
875
0
              scaleValues(&dmx_re[windowLen * (window - 1)], windowLen,
876
0
                          -fMin(DFRACT_BITS - 1,
877
0
                                (frameMaxScale - specScaleL[window - 1])));
878
11.3k
            } else {
879
11.3k
              specScaleL[window] = specScaleL[window - 1];
880
11.3k
              specScaleR[window] = specScaleR[window - 1];
881
11.3k
            }
882
11.3k
          }
883
13.2k
        } /* if ( pJointStereoData->use_prev_frame == 1 ) */
884
885
        /* scaling factors of both channels ought to be equal now */
886
260k
        FDK_ASSERT(specScaleL[window] == specScaleR[window]);
887
888
        /* rescale signal and calculate downmix MDCT */
889
517k
        for (band = 0; band < max_sfb_ste; band++) {
890
          /* first adapt scaling of current band to scaling of current window =>
891
           * shift signal right */
892
257k
          int lScale = leftScale[band];
893
257k
          int rScale = rightScale[band];
894
895
257k
          lScale = fMin(DFRACT_BITS - 1, specScaleL[window] - lScale);
896
257k
          rScale = fMin(DFRACT_BITS - 1,
897
257k
                        specScaleL[window] - rScale); /* L or R doesn't
898
                                                         matter,
899
                                                         specScales are
900
                                                         equal at this
901
                                                         point */
902
903
          /* Write back to sfb scale to cover the case when max_sfb_ste <
904
           * max_sfb */
905
257k
          leftScale[band] = rightScale[band] = specScaleL[window];
906
907
257k
          for (int i = pScaleFactorBandOffsets[band];
908
2.36M
               i < pScaleFactorBandOffsets[band + 1]; i++) {
909
2.11M
            spectrumL[windowLen * window + i] >>= lScale;
910
2.11M
            spectrumR[windowLen * window + i] >>= rScale;
911
2.11M
          }
912
913
          /* now calculate downmix MDCT */
914
257k
          if (pJointStereoData->MsUsed[band] & groupMask) {
915
183k
            for (int i = pScaleFactorBandOffsets[band];
916
1.60M
                 i < pScaleFactorBandOffsets[band + 1]; i++) {
917
1.42M
              dmx_re[windowLen * window + i] =
918
1.42M
                  spectrumL[windowLen * window + i];
919
1.42M
            }
920
183k
          } else {
921
73.3k
            if (cplxPredictionData->pred_dir == 0) {
922
8.02k
              for (int i = pScaleFactorBandOffsets[band];
923
91.9k
                   i < pScaleFactorBandOffsets[band + 1]; i++) {
924
83.9k
                dmx_re[windowLen * window + i] =
925
83.9k
                    (spectrumL[windowLen * window + i] +
926
83.9k
                     spectrumR[windowLen * window + i]) >>
927
83.9k
                    1;
928
83.9k
              }
929
65.3k
            } else {
930
65.3k
              for (int i = pScaleFactorBandOffsets[band];
931
670k
                   i < pScaleFactorBandOffsets[band + 1]; i++) {
932
605k
                dmx_re[windowLen * window + i] =
933
605k
                    (spectrumL[windowLen * window + i] -
934
605k
                     spectrumR[windowLen * window + i]) >>
935
605k
                    1;
936
605k
              }
937
65.3k
            }
938
73.3k
          }
939
940
257k
        } /* for ( band=0; band<max_sfb_ste; band++ ) */
941
        /* Clean until the end */
942
260k
        for (int i = pScaleFactorBandOffsets[max_sfb_ste_outside];
943
48.5M
             i < windowLen; i++) {
944
48.2M
          dmx_re[windowLen * window + i] = (FIXP_DBL)0;
945
48.2M
        }
946
947
        /* 3. calculate MDST-portion corresponding to the current frame. */
948
260k
        if (cplxPredictionData->complex_coef == 1) {
949
235k
          {
950
            /* 3.1 move pointer in filter-coefficient table in case of short
951
             * window sequence */
952
            /*     (other coefficients are utilized for the last 7 short
953
             * windows)            */
954
235k
            if ((pAacDecoderChannelInfo[L]->icsInfo.WindowSequence ==
955
235k
                 BLOCK_SHORT) &&
956
212k
                (window != 0)) {
957
185k
              pCoeff = mdst_filt_coef_curr[currentShape];
958
185k
              pCoeffPrev = mdst_filt_coef_prev[currentShape];
959
185k
            }
960
961
            /* The length of the filter processing must be extended because of
962
             * filter boundary problems */
963
235k
            int extended_band = fMin(
964
235k
                pScaleFactorBandOffsets[max_sfb_ste_outside] + 7, windowLen);
965
966
            /* 3.2. estimate downmix MDST from current frame downmix MDCT */
967
235k
            if ((pAacDecoderChannelInfo[L]->icsInfo.WindowSequence ==
968
235k
                 BLOCK_SHORT) &&
969
212k
                (window != 0)) {
970
185k
              CJointStereo_filterAndAdd(&dmx_re[windowLen * window],
971
185k
                                        extended_band, windowLen, pCoeff,
972
185k
                                        &dmx_im[windowLen * window], 1);
973
974
185k
              CJointStereo_filterAndAdd(&dmx_re[windowLen * (window - 1)],
975
185k
                                        extended_band, windowLen, pCoeffPrev,
976
185k
                                        &dmx_im[windowLen * window], 0);
977
185k
            } else {
978
50.1k
              CJointStereo_filterAndAdd(dmx_re, extended_band, windowLen,
979
50.1k
                                        pCoeff, dmx_im, 1);
980
981
50.1k
              if (cplxPredictionData->use_prev_frame == 1) {
982
1.90k
                CJointStereo_filterAndAdd(dmx_re_prev, extended_band, windowLen,
983
1.90k
                                          pCoeffPrev,
984
1.90k
                                          &dmx_im[windowLen * window], 0);
985
1.90k
              }
986
50.1k
            }
987
988
235k
          } /* if(pAacDecoderChannelInfo[L]->transform_splitting_active) */
989
235k
        }   /* if ( pJointStereoData->complex_coef == 1 ) */
990
991
        /* 4. upmix process */
992
260k
        LONG pred_dir = cplxPredictionData->pred_dir ? -1 : 1;
993
        /* 0.1 in Q-3.34 */
994
260k
        const FIXP_DBL pointOne = 0x66666666; /* 0.8 */
995
        /* Shift value for the downmix */
996
260k
        const INT shift_dmx = SF_FNA_COEFFS + 1;
997
998
517k
        for (band = 0; band < max_sfb_ste_outside; band++) {
999
257k
          if (pJointStereoData->MsUsed[band] & groupMask) {
1000
183k
            FIXP_SGL tempRe =
1001
183k
                (FIXP_SGL)cplxPredictionData->alpha_q_re[group][band];
1002
183k
            FIXP_SGL tempIm =
1003
183k
                (FIXP_SGL)cplxPredictionData->alpha_q_im[group][band];
1004
1005
            /* Find the minimum common headroom for alpha_re and alpha_im */
1006
183k
            int alpha_re_headroom = CountLeadingBits((INT)tempRe) - 16;
1007
183k
            if (tempRe == (FIXP_SGL)0) alpha_re_headroom = 15;
1008
183k
            int alpha_im_headroom = CountLeadingBits((INT)tempIm) - 16;
1009
183k
            if (tempIm == (FIXP_SGL)0) alpha_im_headroom = 15;
1010
183k
            int val = fMin(alpha_re_headroom, alpha_im_headroom);
1011
1012
            /* Multiply alpha by 0.1 with maximum precision */
1013
183k
            FDK_ASSERT(val >= 0);
1014
183k
            FIXP_DBL alpha_re_tmp = fMult((FIXP_SGL)(tempRe << val), pointOne);
1015
183k
            FIXP_DBL alpha_im_tmp = fMult((FIXP_SGL)(tempIm << val), pointOne);
1016
1017
            /* Calculate alpha exponent */
1018
            /* (Q-3.34 * Q15.0) shifted left by "val" */
1019
183k
            int alpha_re_exp = -3 + 15 - val;
1020
1021
183k
            int help3_shift = alpha_re_exp + 1;
1022
1023
183k
            FIXP_DBL *p2CoeffL = &(
1024
183k
                spectrumL[windowLen * window + pScaleFactorBandOffsets[band]]);
1025
183k
            FIXP_DBL *p2CoeffR = &(
1026
183k
                spectrumR[windowLen * window + pScaleFactorBandOffsets[band]]);
1027
183k
            FIXP_DBL *p2dmxIm =
1028
183k
                &(dmx_im[windowLen * window + pScaleFactorBandOffsets[band]]);
1029
183k
            FIXP_DBL *p2dmxRe =
1030
183k
                &(dmx_re[windowLen * window + pScaleFactorBandOffsets[band]]);
1031
1032
183k
            for (int i = pScaleFactorBandOffsets[band];
1033
1.60M
                 i < pScaleFactorBandOffsets[band + 1]; i++) {
1034
              /* Calculating helper term:
1035
                    side = specR[i] - alpha_re[i] * dmx_re[i] - alpha_im[i] *
1036
                dmx_im[i];
1037
1038
                Here "dmx_re" may be the same as "specL" or alternatively keep
1039
                the downmix. "dmx_re" and "specL" are two different pointers
1040
                pointing to separate arrays, which may or may not contain the
1041
                same data (with different scaling).
1042
1043
                specL[i] =   + (specL[i] + side);
1044
                specR[i] = -/+ (specL[i] - side);
1045
              */
1046
1.42M
              FIXP_DBL side, left, right;
1047
1048
1.42M
              side = fMultAddDiv2(fMultDiv2(alpha_re_tmp, *p2dmxRe++),
1049
1.42M
                                  alpha_im_tmp, (*p2dmxIm++) << shift_dmx);
1050
1.42M
              side = ((*p2CoeffR) >> 2) -
1051
1.42M
                     (FIXP_DBL)SATURATE_SHIFT(side, -(help3_shift - 2),
1052
1.42M
                                              DFRACT_BITS - 2);
1053
1054
1.42M
              left = ((*p2CoeffL) >> 2) + side;
1055
1.42M
              right = ((*p2CoeffL) >> 2) - side;
1056
1.42M
              right = (FIXP_DBL)((LONG)right * pred_dir);
1057
1058
1.42M
              *p2CoeffL++ = SATURATE_LEFT_SHIFT_ALT(left, 2, DFRACT_BITS);
1059
1.42M
              *p2CoeffR++ = SATURATE_LEFT_SHIFT_ALT(right, 2, DFRACT_BITS);
1060
1.42M
            }
1061
183k
          }
1062
1063
257k
        } /* for ( band=0; band < max_sfb_ste; band++ ) */
1064
260k
      }   /* for ( groupwin=0; groupwin<pWindowGroupLength[group]; groupwin++,
1065
             window++ ) */
1066
1067
224k
    } /* for ( window = 0, group = 0; group < windowGroups; group++ ) */
1068
1069
    /* free scratch buffer */
1070
64.2k
    C_AALLOC_SCRATCH_END(dmx_im, FIXP_DBL, 1024);
1071
1072
134k
  } else {
1073
    /* MS stereo */
1074
1075
391k
    for (window = 0, group = 0; group < windowGroups; group++) {
1076
257k
      groupMask = 1 << group;
1077
1078
597k
      for (int groupwin = 0; groupwin < pWindowGroupLength[group];
1079
340k
           groupwin++, window++) {
1080
340k
        FIXP_DBL *leftSpectrum, *rightSpectrum;
1081
340k
        SHORT *leftScale = &SFBleftScale[window * 16];
1082
340k
        SHORT *rightScale = &SFBrightScale[window * 16];
1083
1084
340k
        leftSpectrum =
1085
340k
            SPEC(spectrumL, window, pAacDecoderChannelInfo[L]->granuleLength);
1086
340k
        rightSpectrum =
1087
340k
            SPEC(spectrumR, window, pAacDecoderChannelInfo[R]->granuleLength);
1088
1089
995k
        for (band = 0; band < max_sfb_ste_outside; band++) {
1090
655k
          if (pJointStereoData->MsUsed[band] & groupMask) {
1091
205k
            int lScale = leftScale[band];
1092
205k
            int rScale = rightScale[band];
1093
205k
            int commonScale = lScale > rScale ? lScale : rScale;
1094
205k
            unsigned int offsetCurrBand, offsetNextBand;
1095
1096
            /* ISO/IEC 14496-3 Chapter 4.6.8.1.1 :
1097
               M/S joint channel coding can only be used if common_window is 1.
1098
             */
1099
205k
            FDK_ASSERT(GetWindowSequence(&pAacDecoderChannelInfo[L]->icsInfo) ==
1100
205k
                       GetWindowSequence(&pAacDecoderChannelInfo[R]->icsInfo));
1101
205k
            FDK_ASSERT(GetWindowShape(&pAacDecoderChannelInfo[L]->icsInfo) ==
1102
205k
                       GetWindowShape(&pAacDecoderChannelInfo[R]->icsInfo));
1103
1104
205k
            commonScale++;
1105
205k
            leftScale[band] = commonScale;
1106
205k
            rightScale[band] = commonScale;
1107
1108
205k
            lScale = fMin(DFRACT_BITS - 1, commonScale - lScale);
1109
205k
            rScale = fMin(DFRACT_BITS - 1, commonScale - rScale);
1110
1111
205k
            FDK_ASSERT(lScale >= 0 && rScale >= 0);
1112
1113
205k
            offsetCurrBand = pScaleFactorBandOffsets[band];
1114
205k
            offsetNextBand = pScaleFactorBandOffsets[band + 1];
1115
1116
205k
            CJointStereo_GenerateMSOutput(&(leftSpectrum[offsetCurrBand]),
1117
205k
                                          &(rightSpectrum[offsetCurrBand]),
1118
205k
                                          lScale, rScale,
1119
205k
                                          offsetNextBand - offsetCurrBand);
1120
205k
          }
1121
655k
        }
1122
340k
        if (scaleFactorBandsTransmittedL > scaleFactorBandsTransmitted) {
1123
875
          for (; band < scaleFactorBandsTransmittedL; band++) {
1124
0
            if (pJointStereoData->MsUsed[band] & groupMask) {
1125
0
              rightScale[band] = leftScale[band];
1126
1127
0
              for (int index = pScaleFactorBandOffsets[band];
1128
0
                   index < pScaleFactorBandOffsets[band + 1]; index++) {
1129
0
                FIXP_DBL leftCoefficient = leftSpectrum[index];
1130
                /* FIXP_DBL rightCoefficient = (FIXP_DBL)0; */
1131
0
                rightSpectrum[index] = leftCoefficient;
1132
0
              }
1133
0
            }
1134
0
          }
1135
339k
        } else if (scaleFactorBandsTransmittedR > scaleFactorBandsTransmitted) {
1136
1.97k
          for (; band < scaleFactorBandsTransmittedR; band++) {
1137
0
            if (pJointStereoData->MsUsed[band] & groupMask) {
1138
0
              leftScale[band] = rightScale[band];
1139
1140
0
              for (int index = pScaleFactorBandOffsets[band];
1141
0
                   index < pScaleFactorBandOffsets[band + 1]; index++) {
1142
                /* FIXP_DBL leftCoefficient  = (FIXP_DBL)0; */
1143
0
                FIXP_DBL rightCoefficient = rightSpectrum[index];
1144
1145
0
                leftSpectrum[index] = rightCoefficient;
1146
0
                rightSpectrum[index] = -rightCoefficient;
1147
0
              }
1148
0
            }
1149
0
          }
1150
1.97k
        }
1151
340k
      }
1152
257k
    }
1153
1154
    /* Reset MsUsed flags if no explicit signalling was transmitted. Necessary
1155
       for intensity coding. PNS correlation signalling was mapped before
1156
       calling CJointStereo_ApplyMS(). */
1157
134k
    if (pJointStereoData->MsMaskPresent == 2) {
1158
31.1k
      FDKmemclear(pJointStereoData->MsUsed,
1159
31.1k
                  JointStereoMaximumBands * sizeof(UCHAR));
1160
31.1k
    }
1161
134k
  }
1162
198k
}
1163
1164
void CJointStereo_ApplyIS(CAacDecoderChannelInfo *pAacDecoderChannelInfo[2],
1165
                          const SHORT *pScaleFactorBandOffsets,
1166
                          const UCHAR *pWindowGroupLength,
1167
                          const int windowGroups,
1168
117k
                          const int scaleFactorBandsTransmitted) {
1169
117k
  CJointStereoData *pJointStereoData =
1170
117k
      &pAacDecoderChannelInfo[L]->pComData->jointStereoData;
1171
1172
338k
  for (int window = 0, group = 0; group < windowGroups; group++) {
1173
221k
    UCHAR *CodeBook;
1174
221k
    SHORT *ScaleFactor;
1175
221k
    UCHAR groupMask = 1 << group;
1176
1177
221k
    CodeBook = &pAacDecoderChannelInfo[R]->pDynData->aCodeBook[group * 16];
1178
221k
    ScaleFactor =
1179
221k
        &pAacDecoderChannelInfo[R]->pDynData->aScaleFactor[group * 16];
1180
1181
521k
    for (int groupwin = 0; groupwin < pWindowGroupLength[group];
1182
299k
         groupwin++, window++) {
1183
299k
      FIXP_DBL *leftSpectrum, *rightSpectrum;
1184
299k
      SHORT *leftScale =
1185
299k
          &pAacDecoderChannelInfo[L]->pDynData->aSfbScale[window * 16];
1186
299k
      SHORT *rightScale =
1187
299k
          &pAacDecoderChannelInfo[R]->pDynData->aSfbScale[window * 16];
1188
299k
      int band;
1189
1190
299k
      leftSpectrum = SPEC(pAacDecoderChannelInfo[L]->pSpectralCoefficient,
1191
299k
                          window, pAacDecoderChannelInfo[L]->granuleLength);
1192
299k
      rightSpectrum = SPEC(pAacDecoderChannelInfo[R]->pSpectralCoefficient,
1193
299k
                           window, pAacDecoderChannelInfo[R]->granuleLength);
1194
1195
889k
      for (band = 0; band < scaleFactorBandsTransmitted; band++) {
1196
590k
        if ((CodeBook[band] == INTENSITY_HCB) ||
1197
566k
            (CodeBook[band] == INTENSITY_HCB2)) {
1198
62.3k
          int bandScale = -(ScaleFactor[band] + 100);
1199
1200
62.3k
          int msb = bandScale >> 2;
1201
62.3k
          int lsb = bandScale & 0x03;
1202
1203
          /* exponent of MantissaTable[lsb][0] is 1, thus msb+1 below. */
1204
62.3k
          FIXP_DBL scale = MantissaTable[lsb][0];
1205
1206
          /* ISO/IEC 14496-3 Chapter 4.6.8.2.3 :
1207
             The use of intensity stereo coding is signaled by the use of the
1208
             pseudo codebooks INTENSITY_HCB and INTENSITY_HCB2 (15 and 14) only
1209
             in the right channel of a channel_pair_element() having a common
1210
             ics_info() (common_window == 1). */
1211
62.3k
          FDK_ASSERT(GetWindowSequence(&pAacDecoderChannelInfo[L]->icsInfo) ==
1212
62.3k
                     GetWindowSequence(&pAacDecoderChannelInfo[R]->icsInfo));
1213
62.3k
          FDK_ASSERT(GetWindowShape(&pAacDecoderChannelInfo[L]->icsInfo) ==
1214
62.3k
                     GetWindowShape(&pAacDecoderChannelInfo[R]->icsInfo));
1215
1216
62.3k
          rightScale[band] = leftScale[band] + msb + 1;
1217
1218
62.3k
          if (pJointStereoData->MsUsed[band] & groupMask) {
1219
21.6k
            if (CodeBook[band] == INTENSITY_HCB) /* _NOT_ in-phase */
1220
4.06k
            {
1221
4.06k
              scale = -scale;
1222
4.06k
            }
1223
40.6k
          } else {
1224
40.6k
            if (CodeBook[band] == INTENSITY_HCB2) /* out-of-phase */
1225
21.2k
            {
1226
21.2k
              scale = -scale;
1227
21.2k
            }
1228
40.6k
          }
1229
1230
62.3k
          for (int index = pScaleFactorBandOffsets[band];
1231
379k
               index < pScaleFactorBandOffsets[band + 1]; index++) {
1232
317k
            rightSpectrum[index] = fMult(leftSpectrum[index], scale);
1233
317k
          }
1234
62.3k
        }
1235
590k
      }
1236
299k
    }
1237
221k
  }
1238
117k
}