1/* -----------------------------------------------------------------------------
2Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4© Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5Forschung e.V. All rights reserved.
6
7 1.    INTRODUCTION
8The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10scheme for digital audio. This FDK AAC Codec software is intended to be used on
11a wide variety of Android devices.
12
13AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14general perceptual audio codecs. AAC-ELD is considered the best-performing
15full-bandwidth communications codec by independent studies and is widely
16deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17specifications.
18
19Patent licenses for necessary patent claims for the FDK AAC Codec (including
20those of Fraunhofer) may be obtained through Via Licensing
21(www.vialicensing.com) or through the respective patent owners individually for
22the purpose of encoding or decoding bit streams in products that are compliant
23with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24Android devices already license these patent claims through Via Licensing or
25directly from the patent owners, and therefore FDK AAC Codec software may
26already be covered under those patent licenses when it is used for those
27licensed purposes only.
28
29Commercially-licensed AAC software libraries, including floating-point versions
30with enhanced sound quality, are also available from Fraunhofer. Users are
31encouraged to check the Fraunhofer website for additional applications
32information and documentation.
33
342.    COPYRIGHT LICENSE
35
36Redistribution and use in source and binary forms, with or without modification,
37are permitted without payment of copyright license fees provided that you
38satisfy the following conditions:
39
40You must retain the complete text of this software license in redistributions of
41the FDK AAC Codec or your modifications thereto in source code form.
42
43You must retain the complete text of this software license in the documentation
44and/or other materials provided with redistributions of the FDK AAC Codec or
45your modifications thereto in binary form. You must make available free of
46charge copies of the complete source code of the FDK AAC Codec and your
47modifications thereto to recipients of copies in binary form.
48
49The name of Fraunhofer may not be used to endorse or promote products derived
50from this library without prior written permission.
51
52You may not charge copyright license fees for anyone to use, copy or distribute
53the FDK AAC Codec software or your modifications thereto.
54
55Your modified versions of the FDK AAC Codec must carry prominent notices stating
56that you changed the software and the date of any change. For modified versions
57of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59AAC Codec Library for Android."
60
613.    NO PATENT LICENSE
62
63NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65Fraunhofer provides no warranty of patent non-infringement with respect to this
66software.
67
68You may use this FDK AAC Codec software or modifications thereto only for
69purposes that are authorized by appropriate patent licenses.
70
714.    DISCLAIMER
72
73This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75including but not limited to the implied warranties of merchantability and
76fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78or consequential damages, including but not limited to procurement of substitute
79goods or services; loss of use, data, or profits, or business interruption,
80however caused and on any theory of liability, whether in contract, strict
81liability, or tort (including negligence), arising in any way out of the use of
82this software, even if advised of the possibility of such damage.
83
845.    CONTACT INFORMATION
85
86Fraunhofer Institute for Integrated Circuits IIS
87Attention: Audio and Multimedia Departments - FDK AAC LL
88Am Wolfsmantel 33
8991058 Erlangen, Germany
90
91www.iis.fraunhofer.de/amm
92amm-info@iis.fraunhofer.de
93----------------------------------------------------------------------------- */
94
95/******************* Library for basic calculation routines ********************
96
97   Author(s):   Markus Lohwasser, Josef Hoepfl, Manuel Jander
98
99   Description: QMF filterbank
100
101*******************************************************************************/
102
103#ifndef QMF_PCM_H
104#define QMF_PCM_H
105
106/*
107   All Synthesis functions dependent on datatype INT_PCM_QMFOUT
108   Should only be included by qmf.cpp, but not compiled separately, please
109   exclude compilation from project, if done otherwise. Is optional included
110   twice to duplicate all functions with two different pre-definitions, as:
111        #define INT_PCM_QMFOUT LONG
112    and ...
113        #define INT_PCM_QMFOUT SHORT
114    needed to run QMF synthesis in both 16bit and 32bit sample output format.
115*/
116
117#define QSSCALE (0)
118#define FX_DBL2FX_QSS(x) (x)
119#define FX_QSS2FX_DBL(x) (x)
120
121/*!
122  \brief Perform Synthesis Prototype Filtering on a single slot of input data.
123
124  The filter takes 2 * qmf->no_channels of input data and
125  generates qmf->no_channels time domain output samples.
126*/
127/* static */
128#ifndef FUNCTION_qmfSynPrototypeFirSlot
129void qmfSynPrototypeFirSlot(
130#else
131void qmfSynPrototypeFirSlot_fallback(
132#endif
133    HANDLE_QMF_FILTER_BANK qmf,
134    FIXP_DBL *RESTRICT realSlot,      /*!< Input: Pointer to real Slot */
135    FIXP_DBL *RESTRICT imagSlot,      /*!< Input: Pointer to imag Slot */
136    INT_PCM_QMFOUT *RESTRICT timeOut, /*!< Time domain data */
137    int stride) {
138  FIXP_QSS *FilterStates = (FIXP_QSS *)qmf->FilterStates;
139  int no_channels = qmf->no_channels;
140  const FIXP_PFT *p_Filter = qmf->p_filter;
141  int p_stride = qmf->p_stride;
142  int j;
143  FIXP_QSS *RESTRICT sta = FilterStates;
144  const FIXP_PFT *RESTRICT p_flt, *RESTRICT p_fltm;
145  int scale = (DFRACT_BITS - SAMPLE_BITS_QMFOUT) - 1 - qmf->outScalefactor -
146              qmf->outGain_e;
147
148  p_flt =
149      p_Filter + p_stride * QMF_NO_POLY; /*                     5th of 330 */
150  p_fltm = p_Filter + (qmf->FilterSize / 2) -
151           p_stride * QMF_NO_POLY; /* 5 + (320 - 2*5) = 315th of 330 */
152
153  FIXP_SGL gain = FX_DBL2FX_SGL(qmf->outGain_m);
154
155  FIXP_DBL rnd_val = 0;
156
157  if (scale > 0) {
158    if (scale < (DFRACT_BITS - 1))
159      rnd_val = FIXP_DBL(1 << (scale - 1));
160    else
161      scale = (DFRACT_BITS - 1);
162  } else {
163    scale = fMax(scale, -(DFRACT_BITS - 1));
164  }
165
166  for (j = no_channels - 1; j >= 0; j--) {
167    FIXP_DBL imag = imagSlot[j]; /* no_channels-1 .. 0 */
168    FIXP_DBL real = realSlot[j]; /* no_channels-1 .. 0 */
169    {
170      INT_PCM_QMFOUT tmp;
171      FIXP_DBL Are = fMultAddDiv2(FX_QSS2FX_DBL(sta[0]), p_fltm[0], real);
172
173      /* This PCM formatting performs:
174         - multiplication with 16-bit gain, if not -1.0f
175         - rounding, if shift right is applied
176         - apply shift left (or right) with saturation to 32 (or 16) bits
177         - store output with --stride in 32 (or 16) bit format
178      */
179      if (gain != (FIXP_SGL)(-32768)) /* -1.0f */
180      {
181        Are = fMult(Are, gain);
182      }
183      if (scale >= 0) {
184        FDK_ASSERT(
185            Are <=
186            (Are + rnd_val)); /* Round-addition must not overflow, might be
187                                 equal for rnd_val=0 */
188        tmp = (INT_PCM_QMFOUT)(
189            SATURATE_RIGHT_SHIFT(Are + rnd_val, scale, SAMPLE_BITS_QMFOUT));
190      } else {
191        tmp = (INT_PCM_QMFOUT)(
192            SATURATE_LEFT_SHIFT(Are, -scale, SAMPLE_BITS_QMFOUT));
193      }
194
195      { timeOut[(j)*stride] = tmp; }
196    }
197
198    sta[0] = FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[1]), p_flt[4], imag));
199    sta[1] =
200        FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[2]), p_fltm[1], real));
201    sta[2] = FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[3]), p_flt[3], imag));
202    sta[3] =
203        FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[4]), p_fltm[2], real));
204    sta[4] = FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[5]), p_flt[2], imag));
205    sta[5] =
206        FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[6]), p_fltm[3], real));
207    sta[6] = FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[7]), p_flt[1], imag));
208    sta[7] =
209        FX_DBL2FX_QSS(fMultAddDiv2(FX_QSS2FX_DBL(sta[8]), p_fltm[4], real));
210    sta[8] = FX_DBL2FX_QSS(fMultDiv2(p_flt[0], imag));
211    p_flt += (p_stride * QMF_NO_POLY);
212    p_fltm -= (p_stride * QMF_NO_POLY);
213    sta += 9;  // = (2*QMF_NO_POLY-1);
214  }
215}
216
217#ifndef FUNCTION_qmfSynPrototypeFirSlot_NonSymmetric
218/*!
219  \brief Perform Synthesis Prototype Filtering on a single slot of input data.
220
221  The filter takes 2 * qmf->no_channels of input data and
222  generates qmf->no_channels time domain output samples.
223*/
224static void qmfSynPrototypeFirSlot_NonSymmetric(
225    HANDLE_QMF_FILTER_BANK qmf,
226    FIXP_DBL *RESTRICT realSlot,      /*!< Input: Pointer to real Slot */
227    FIXP_DBL *RESTRICT imagSlot,      /*!< Input: Pointer to imag Slot */
228    INT_PCM_QMFOUT *RESTRICT timeOut, /*!< Time domain data */
229    int stride) {
230  FIXP_QSS *FilterStates = (FIXP_QSS *)qmf->FilterStates;
231  int no_channels = qmf->no_channels;
232  const FIXP_PFT *p_Filter = qmf->p_filter;
233  int p_stride = qmf->p_stride;
234  int j;
235  FIXP_QSS *RESTRICT sta = FilterStates;
236  const FIXP_PFT *RESTRICT p_flt, *RESTRICT p_fltm;
237  int scale = (DFRACT_BITS - SAMPLE_BITS_QMFOUT) - 1 - qmf->outScalefactor -
238              qmf->outGain_e;
239
240  p_flt = p_Filter; /*!< Pointer to first half of filter coefficients */
241  p_fltm =
242      &p_flt[qmf->FilterSize / 2]; /* at index 320, overall 640 coefficients */
243
244  FIXP_SGL gain = FX_DBL2FX_SGL(qmf->outGain_m);
245
246  FIXP_DBL rnd_val = (FIXP_DBL)0;
247
248  if (scale > 0) {
249    if (scale < (DFRACT_BITS - 1))
250      rnd_val = FIXP_DBL(1 << (scale - 1));
251    else
252      scale = (DFRACT_BITS - 1);
253  } else {
254    scale = fMax(scale, -(DFRACT_BITS - 1));
255  }
256
257  for (j = no_channels - 1; j >= 0; j--) {
258    FIXP_DBL imag = imagSlot[j]; /* no_channels-1 .. 0 */
259    FIXP_DBL real = realSlot[j]; /* no_channels-1 .. 0 */
260    {
261      INT_PCM_QMFOUT tmp;
262      FIXP_DBL Are = sta[0] + FX_DBL2FX_QSS(fMultDiv2(p_fltm[4], real));
263
264      /* This PCM formatting performs:
265         - multiplication with 16-bit gain, if not -1.0f
266         - rounding, if shift right is applied
267         - apply shift left (or right) with saturation to 32 (or 16) bits
268         - store output with --stride in 32 (or 16) bit format
269      */
270      if (gain != (FIXP_SGL)(-32768)) /* -1.0f */
271      {
272        Are = fMult(Are, gain);
273      }
274      if (scale > 0) {
275        FDK_ASSERT(Are <
276                   (Are + rnd_val)); /* Round-addition must not overflow */
277        tmp = (INT_PCM_QMFOUT)(
278            SATURATE_RIGHT_SHIFT(Are + rnd_val, scale, SAMPLE_BITS_QMFOUT));
279      } else {
280        tmp = (INT_PCM_QMFOUT)(
281            SATURATE_LEFT_SHIFT(Are, -scale, SAMPLE_BITS_QMFOUT));
282      }
283      timeOut[j * stride] = tmp;
284    }
285
286    sta[0] = sta[1] + FX_DBL2FX_QSS(fMultDiv2(p_flt[4], imag));
287    sta[1] = sta[2] + FX_DBL2FX_QSS(fMultDiv2(p_fltm[3], real));
288    sta[2] = sta[3] + FX_DBL2FX_QSS(fMultDiv2(p_flt[3], imag));
289
290    sta[3] = sta[4] + FX_DBL2FX_QSS(fMultDiv2(p_fltm[2], real));
291    sta[4] = sta[5] + FX_DBL2FX_QSS(fMultDiv2(p_flt[2], imag));
292    sta[5] = sta[6] + FX_DBL2FX_QSS(fMultDiv2(p_fltm[1], real));
293    sta[6] = sta[7] + FX_DBL2FX_QSS(fMultDiv2(p_flt[1], imag));
294
295    sta[7] = sta[8] + FX_DBL2FX_QSS(fMultDiv2(p_fltm[0], real));
296    sta[8] = FX_DBL2FX_QSS(fMultDiv2(p_flt[0], imag));
297
298    p_flt += (p_stride * QMF_NO_POLY);
299    p_fltm += (p_stride * QMF_NO_POLY);
300    sta += 9;  // = (2*QMF_NO_POLY-1);
301  }
302}
303#endif /* FUNCTION_qmfSynPrototypeFirSlot_NonSymmetric */
304
305void qmfSynthesisFilteringSlot(HANDLE_QMF_FILTER_BANK synQmf,
306                               const FIXP_DBL *realSlot,
307                               const FIXP_DBL *imagSlot,
308                               const int scaleFactorLowBand,
309                               const int scaleFactorHighBand,
310                               INT_PCM_QMFOUT *timeOut, const int stride,
311                               FIXP_DBL *pWorkBuffer) {
312  if (!(synQmf->flags & QMF_FLAG_LP))
313    qmfInverseModulationHQ(synQmf, realSlot, imagSlot, scaleFactorLowBand,
314                           scaleFactorHighBand, pWorkBuffer);
315  else {
316    if (synQmf->flags & QMF_FLAG_CLDFB) {
317      qmfInverseModulationLP_odd(synQmf, realSlot, scaleFactorLowBand,
318                                 scaleFactorHighBand, pWorkBuffer);
319    } else {
320      qmfInverseModulationLP_even(synQmf, realSlot, scaleFactorLowBand,
321                                  scaleFactorHighBand, pWorkBuffer);
322    }
323  }
324
325  if (synQmf->flags & QMF_FLAG_NONSYMMETRIC) {
326    qmfSynPrototypeFirSlot_NonSymmetric(synQmf, pWorkBuffer,
327                                        pWorkBuffer + synQmf->no_channels,
328                                        timeOut, stride);
329  } else {
330    qmfSynPrototypeFirSlot(synQmf, pWorkBuffer,
331                           pWorkBuffer + synQmf->no_channels, timeOut, stride);
332  }
333}
334
335/*!
336 *
337 * \brief Perform complex-valued subband synthesis of the
338 *        low band and the high band and store the
339 *        time domain data in timeOut
340 *
341 * First step: Calculate the proper scaling factor of current
342 * spectral data in qmfReal/qmfImag, old spectral data in the overlap
343 * range and filter states.
344 *
345 * Second step: Perform Frequency-to-Time mapping with inverse
346 * Modulation slot-wise.
347 *
348 * Third step: Perform FIR-filter slot-wise. To save space for filter
349 * states, the MAC operations are executed directly on the filter states
350 * instead of accumulating several products in the accumulator. The
351 * buffer shift at the end of the function should be replaced by a
352 * modulo operation, which is available on some DSPs.
353 *
354 * Last step: Copy the upper part of the spectral data to the overlap buffer.
355 *
356 * The qmf coefficient table is symmetric. The symmetry is exploited by
357 * shrinking the coefficient table to half the size. The addressing mode
358 * takes care of the symmetries.  If the #define #QMFTABLE_FULL is set,
359 * coefficient addressing works on the full table size. The code will be
360 * slightly faster and slightly more compact.
361 *
362 * Workbuffer requirement: 2 x sizeof(**QmfBufferReal) * synQmf->no_channels
363 * The workbuffer must be aligned
364 */
365void qmfSynthesisFiltering(
366    HANDLE_QMF_FILTER_BANK synQmf, /*!< Handle of Qmf Synthesis Bank  */
367    FIXP_DBL **QmfBufferReal,      /*!< Low and High band, real */
368    FIXP_DBL **QmfBufferImag,      /*!< Low and High band, imag */
369    const QMF_SCALE_FACTOR *scaleFactor,
370    const INT ov_len,        /*!< split Slot of overlap and actual slots */
371    INT_PCM_QMFOUT *timeOut, /*!< Pointer to output */
372    const INT stride,        /*!< stride factor of output */
373    FIXP_DBL *pWorkBuffer    /*!< pointer to temporal working buffer */
374) {
375  int i;
376  int L = synQmf->no_channels;
377  int scaleFactorHighBand;
378  int scaleFactorLowBand_ov, scaleFactorLowBand_no_ov;
379
380  FDK_ASSERT(synQmf->no_channels >= synQmf->lsb);
381  FDK_ASSERT(synQmf->no_channels >= synQmf->usb);
382
383  /* adapt scaling */
384  scaleFactorHighBand = -ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK -
385                        scaleFactor->hb_scale - synQmf->filterScale;
386  scaleFactorLowBand_ov = -ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK -
387                          scaleFactor->ov_lb_scale - synQmf->filterScale;
388  scaleFactorLowBand_no_ov = -ALGORITHMIC_SCALING_IN_ANALYSIS_FILTERBANK -
389                             scaleFactor->lb_scale - synQmf->filterScale;
390
391  for (i = 0; i < synQmf->no_col; i++) /* ----- no_col loop ----- */
392  {
393    const FIXP_DBL *QmfBufferImagSlot = NULL;
394
395    int scaleFactorLowBand =
396        (i < ov_len) ? scaleFactorLowBand_ov : scaleFactorLowBand_no_ov;
397
398    if (!(synQmf->flags & QMF_FLAG_LP)) QmfBufferImagSlot = QmfBufferImag[i];
399
400    qmfSynthesisFilteringSlot(synQmf, QmfBufferReal[i], QmfBufferImagSlot,
401                              scaleFactorLowBand, scaleFactorHighBand,
402                              timeOut + (i * L * stride), stride, pWorkBuffer);
403  } /* no_col loop  i  */
404}
405#endif /* QMF_PCM_H */
406