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/**************************** AAC decoder library ******************************
96
97   Author(s):   Josef Hoepfl
98
99   Description: individual channel stream info
100
101*******************************************************************************/
102
103#ifndef CHANNELINFO_H
104#define CHANNELINFO_H
105
106#include "common_fix.h"
107
108#include "aac_rom.h"
109#include "aacdecoder_lib.h"
110#include "FDK_bitstream.h"
111#include "overlapadd.h"
112
113#include "mdct.h"
114#include "stereo.h"
115#include "pulsedata.h"
116#include "aacdec_tns.h"
117
118#include "aacdec_pns.h"
119
120#include "aacdec_hcr_types.h"
121#include "rvlc_info.h"
122
123#include "usacdec_acelp.h"
124#include "usacdec_const.h"
125#include "usacdec_rom.h"
126
127#include "ac_arith_coder.h"
128
129#include "conceal_types.h"
130
131#include "aacdec_drc_types.h"
132
133#define WB_SECTION_SIZE (1024 * 2)
134
135#define DRM_BS_BUFFER_SIZE                                                 \
136  (512) /* size of the dynamic buffer which is used to reverse the bits of \
137           the DRM SBR payload */
138
139/* Output rendering mode */
140typedef enum {
141  AACDEC_RENDER_INVALID = 0,
142  AACDEC_RENDER_IMDCT,
143  AACDEC_RENDER_ELDFB,
144  AACDEC_RENDER_LPD,
145  AACDEC_RENDER_INTIMDCT
146} AACDEC_RENDER_MODE;
147
148enum { MAX_QUANTIZED_VALUE = 8191 };
149
150typedef enum { FD_LONG, FD_SHORT, LPD } USAC_COREMODE;
151
152typedef struct {
153  const SHORT *ScaleFactorBands_Long;
154  const SHORT *ScaleFactorBands_Short;
155  UCHAR NumberOfScaleFactorBands_Long;
156  UCHAR NumberOfScaleFactorBands_Short;
157  UINT samplingRateIndex;
158  UINT samplingRate;
159} SamplingRateInfo;
160
161typedef struct {
162  UCHAR CommonWindow;
163  UCHAR GlobalGain;
164
165} CRawDataInfo;
166
167typedef struct {
168  UCHAR WindowGroupLength[8];
169  UCHAR WindowGroups;
170  UCHAR Valid;
171
172  UCHAR WindowShape;         /* 0: sine window, 1: KBD, 2: low overlap */
173  BLOCK_TYPE WindowSequence; /* mdct.h; 0: long, 1: start, 2: short, 3: stop */
174  UCHAR MaxSfBands;
175  UCHAR max_sfb_ste;
176  UCHAR ScaleFactorGrouping;
177
178  UCHAR TotalSfBands;
179
180} CIcsInfo;
181
182enum {
183  ZERO_HCB = 0,
184  ESCBOOK = 11,
185  NSPECBOOKS = ESCBOOK + 1,
186  BOOKSCL = NSPECBOOKS,
187  NOISE_HCB = 13,
188  INTENSITY_HCB2 = 14,
189  INTENSITY_HCB = 15,
190  LAST_HCB
191};
192
193/* This struct holds the persistent data shared by both channels of a CPE.
194   It needs to be allocated for each CPE. */
195typedef struct {
196  CJointStereoPersistentData jointStereoPersistentData;
197} CpePersistentData;
198
199/*
200 * This struct must be allocated one for every channel and must be persistent.
201 */
202typedef struct {
203  FIXP_DBL *pOverlapBuffer;
204  mdct_t IMdct;
205
206  CArcoData *hArCo;
207
208  INT pnsCurrentSeed;
209
210  /* LPD memory */
211  FIXP_DBL old_synth[PIT_MAX_MAX - L_SUBFR];
212  INT old_T_pf[SYN_SFD];
213  FIXP_DBL old_gain_pf[SYN_SFD];
214  FIXP_DBL mem_bpf[L_FILT + L_SUBFR];
215  UCHAR
216  old_bpf_control_info; /* (1: enable, 0: disable) bpf for past superframe
217                         */
218
219  USAC_COREMODE last_core_mode; /* core mode used by the decoder in previous
220                                   frame. (not signalled by the bitstream, see
221                                   CAacDecoderChannelInfo::core_mode_last !! )
222                                 */
223  UCHAR last_lpd_mode;      /* LPD mode used by the decoder in last LPD subframe
224                                (not signalled by the bitstream, see
225                               CAacDecoderChannelInfo::lpd_mode_last !! ) */
226  UCHAR last_last_lpd_mode; /* LPD mode used in second last LPD subframe
227                                (not signalled by the bitstream) */
228  UCHAR last_lpc_lost;      /* Flag indicating that the previous LPC is lost */
229
230  FIXP_LPC
231  lpc4_lsf[M_LP_FILTER_ORDER]; /* Last LPC4 coefficients in LSF domain. */
232  FIXP_LPC lsf_adaptive_mean[M_LP_FILTER_ORDER]; /* Adaptive mean of LPC
233                                                    coefficients in LSF domain
234                                                    for concealment. */
235  FIXP_LPC lp_coeff_old[2][M_LP_FILTER_ORDER];   /* Last LPC coefficients in LP
236                                    domain. lp_coeff_old[0] is lpc4 (coeffs for
237                                    right folding point of last tcx frame),
238                                    lp_coeff_old[1] are coeffs for left folding
239                                    point of last tcx frame */
240  INT lp_coeff_old_exp[2];
241
242  FIXP_SGL
243  oldStability; /* LPC coeff stability value from last frame (required for
244                   TCX concealment). */
245  UINT numLostLpdFrames; /* Number of consecutive lost subframes. */
246
247  /* TCX memory */
248  FIXP_DBL last_tcx_gain;
249  INT last_tcx_gain_e;
250  FIXP_DBL last_alfd_gains[32]; /* Scaled by one bit. */
251  SHORT last_tcx_pitch;
252  UCHAR last_tcx_noise_factor;
253
254  /* ACELP memory */
255  CAcelpStaticMem acelp;
256
257  ULONG nfRandomSeed; /* seed value for USAC noise filling random generator */
258
259  CDrcChannelData drcData;
260  CConcealmentInfo concealmentInfo;
261
262  CpePersistentData *pCpeStaticData;
263
264} CAacDecoderStaticChannelInfo;
265
266/*
267 * This union must be allocated for every element (up to 2 channels).
268 */
269typedef struct {
270  /* Common bit stream data */
271  SHORT aScaleFactor[(
272      8 * 16)]; /* Spectral scale factors for each sfb in each window. */
273  SHORT aSfbScale[(8 * 16)]; /* could be free after ApplyTools() */
274  UCHAR
275  aCodeBook[(8 * 16)]; /* section data: codebook for each window and sfb. */
276  UCHAR band_is_noise[(8 * 16)];
277  CTnsData TnsData;
278  CRawDataInfo RawDataInfo;
279
280  shouldBeUnion {
281    struct {
282      CPulseData PulseData;
283      SHORT aNumLineInSec4Hcr[MAX_SFB_HCR]; /* needed once for all channels
284                                               except for Drm syntax */
285      UCHAR
286      aCodeBooks4Hcr[MAX_SFB_HCR]; /* needed once for all channels except for
287                                      Drm syntax. Same as "aCodeBook" ? */
288      SHORT lenOfReorderedSpectralData;
289      SCHAR lenOfLongestCodeword;
290      SCHAR numberSection;
291      SCHAR rvlcCurrentScaleFactorOK;
292      SCHAR rvlcIntensityUsed;
293    } aac;
294    struct {
295      UCHAR fd_noise_level_and_offset;
296      UCHAR tns_active;
297      UCHAR tns_on_lr;
298      UCHAR tcx_noise_factor[4];
299      UCHAR tcx_global_gain[4];
300    } usac;
301  }
302  specificTo;
303
304} CAacDecoderDynamicData;
305
306typedef shouldBeUnion {
307  UCHAR DrmBsBuffer[DRM_BS_BUFFER_SIZE];
308
309  /* Common signal data, can be used once the bit stream data from above is not
310   * used anymore. */
311  FIXP_DBL mdctOutTemp[1024];
312
313  FIXP_DBL synth_buf[(PIT_MAX_MAX + SYN_DELAY + L_FRAME_PLUS)];
314
315  FIXP_DBL workBuffer[WB_SECTION_SIZE];
316}
317CWorkBufferCore1;
318
319/* Common data referenced by all channels */
320typedef struct {
321  CAacDecoderDynamicData pAacDecoderDynamicData[2];
322
323  CPnsInterChannelData pnsInterChannelData;
324  INT pnsRandomSeed[(8 * 16)];
325
326  CJointStereoData jointStereoData; /* One for one element */
327
328  shouldBeUnion {
329    struct {
330      CErHcrInfo erHcrInfo;
331      CErRvlcInfo erRvlcInfo;
332      SHORT aRvlcScfEsc[RVLC_MAX_SFB]; /* needed once for all channels */
333      SHORT aRvlcScfFwd[RVLC_MAX_SFB]; /* needed once for all channels */
334      SHORT aRvlcScfBwd[RVLC_MAX_SFB]; /* needed once for all channels */
335    } aac;
336  }
337  overlay;
338
339} CAacDecoderCommonData;
340
341typedef struct {
342  CWorkBufferCore1 *pWorkBufferCore1;
343  CCplxPredictionData *cplxPredictionData;
344} CAacDecoderCommonStaticData;
345
346/*
347 * This struct must be allocated one for every channel of every element and must
348 * be persistent. Among its members, the following memory areas can be
349 * overwritten under the given conditions:
350 *  - pSpectralCoefficient The memory pointed to can be overwritten after time
351 * signal rendering.
352 *  - data can be overwritten after time signal rendering.
353 *  - pDynData memory pointed to can be overwritten after each
354 * CChannelElement_Decode() call.
355 *  - pComData->overlay memory pointed to can be overwritten after each
356 * CChannelElement_Decode() call..
357 */
358typedef struct {
359  shouldBeUnion {
360    struct {
361      FIXP_DBL fac_data0[LFAC];
362      UCHAR fac_data_e[4];
363      FIXP_DBL
364      *fac_data[4]; /* Pointers to unused parts of pSpectralCoefficient */
365
366      UCHAR core_mode; /* current core mode */
367      USAC_COREMODE
368      core_mode_last;      /* previous core mode, signalled in the bitstream
369                              (not done by the decoder, see
370                              CAacDecoderStaticChannelInfo::last_core_mode !!)*/
371      UCHAR lpd_mode_last; /* previous LPD mode, signalled in the bitstream
372                              (not done by the decoder, see
373                              CAacDecoderStaticChannelInfo::last_core_mode !!)*/
374      UCHAR mod[4];
375      UCHAR bpf_control_info; /* (1: enable, 0: disable) bpf for current
376                                 superframe */
377
378      FIXP_LPC lsp_coeff[5][M_LP_FILTER_ORDER]; /* linear prediction
379                                                   coefficients in LSP domain */
380      FIXP_LPC
381      lp_coeff[5][M_LP_FILTER_ORDER]; /* linear prediction coefficients in
382                                         LP domain */
383      INT lp_coeff_exp[5];
384      FIXP_LPC lsf_adaptive_mean_cand
385          [M_LP_FILTER_ORDER]; /* concealment: is copied to
386                  CAacDecoderStaticChannelInfo->lsf_adaptive_mean once frame is
387                  assumed to be correct*/
388      FIXP_SGL aStability[4];  /* LPC coeff stability values required for ACELP
389                                  and TCX (concealment) */
390
391      CAcelpChannelData acelp[4];
392
393      FIXP_DBL tcx_gain[4];
394      SCHAR tcx_gain_e[4];
395    } usac;
396
397    struct {
398      CPnsData PnsData; /* Not required for USAC */
399    } aac;
400  }
401  data;
402
403  SPECTRAL_PTR pSpectralCoefficient; /* Spectral coefficients of each window */
404  SHORT specScale[8]; /* Scale shift values of each spectrum window */
405  CIcsInfo icsInfo;
406  INT granuleLength; /* Size of smallest spectrum piece */
407  UCHAR ElementInstanceTag;
408
409  AACDEC_RENDER_MODE renderMode; /* Output signal rendering mode */
410
411  CAacDecoderDynamicData *
412      pDynData; /* Data required for one element and discarded after decoding */
413  CAacDecoderCommonData
414      *pComData; /* Data required for one channel at a time during decode */
415  CAacDecoderCommonStaticData *pComStaticData; /* Persistent data required for
416                                                  one channel at a time during
417                                                  decode */
418
419  int currAliasingSymmetry; /* required for RSVD60 MCT */
420
421} CAacDecoderChannelInfo;
422
423/* channelinfo.cpp */
424
425AAC_DECODER_ERROR getSamplingRateInfo(SamplingRateInfo *t, UINT samplesPerFrame,
426                                      UINT samplingRateIndex,
427                                      UINT samplingRate);
428
429/**
430 * \brief Read max SFB from bit stream and assign TotalSfBands according
431 *        to the window sequence and sample rate.
432 * \param hBs bit stream handle as data source
433 * \param pIcsInfo IcsInfo structure to read the window sequence and store
434 * MaxSfBands and TotalSfBands
435 * \param pSamplingRateInfo read only
436 */
437AAC_DECODER_ERROR IcsReadMaxSfb(HANDLE_FDK_BITSTREAM hBs, CIcsInfo *pIcsInfo,
438                                const SamplingRateInfo *pSamplingRateInfo);
439
440AAC_DECODER_ERROR IcsRead(HANDLE_FDK_BITSTREAM bs, CIcsInfo *pIcsInfo,
441                          const SamplingRateInfo *SamplingRateInfoTable,
442                          const UINT flags);
443
444/* stereo.cpp, only called from this file */
445
446/*!
447  \brief Applies MS stereo.
448
449  The function applies MS stereo.
450
451  \param pAacDecoderChannelInfo aac channel info.
452  \param pScaleFactorBandOffsets pointer to scalefactor band offsets.
453  \param pWindowGroupLength pointer to window group length array.
454  \param windowGroups number of window groups.
455  \param scaleFactorBandsTransmittedL number of transmitted scalefactor bands in
456  left channel. \param scaleFactorBandsTransmittedR number of transmitted
457  scalefactor bands in right channel. May differ from
458  scaleFactorBandsTransmittedL only for USAC. \return  none
459*/
460void CJointStereo_ApplyMS(
461    CAacDecoderChannelInfo *pAacDecoderChannelInfo[2],
462    CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[2],
463    FIXP_DBL *spectrumL, FIXP_DBL *spectrumR, SHORT *SFBleftScale,
464    SHORT *SFBrightScale, SHORT *specScaleL, SHORT *specScaleR,
465    const SHORT *pScaleFactorBandOffsets, const UCHAR *pWindowGroupLength,
466    const int windowGroups, const int max_sfb_ste_outside,
467    const int scaleFactorBandsTransmittedL,
468    const int scaleFactorBandsTransmittedR, FIXP_DBL *store_dmx_re_prev,
469    SHORT *store_dmx_re_prev_e, const int mainband_flag);
470
471/*!
472  \brief Applies intensity stereo
473
474  The function applies intensity stereo.
475
476  \param pAacDecoderChannelInfo aac channel info.
477  \param pScaleFactorBandOffsets pointer to scalefactor band offsets.
478  \param pWindowGroupLength pointer to window group length array.
479  \param windowGroups number of window groups.
480  \param scaleFactorBandsTransmitted number of transmitted scalefactor bands.
481  \return  none
482*/
483void CJointStereo_ApplyIS(CAacDecoderChannelInfo *pAacDecoderChannelInfo[2],
484                          const short *pScaleFactorBandOffsets,
485                          const UCHAR *pWindowGroupLength,
486                          const int windowGroups,
487                          const int scaleFactorBandsTransmitted);
488
489/* aacdec_pns.cpp */
490int CPns_IsPnsUsed(const CPnsData *pPnsData, const int group, const int band);
491
492void CPns_SetCorrelation(CPnsData *pPnsData, const int group, const int band,
493                         const int outofphase);
494
495/****************** inline functions ******************/
496
497inline UCHAR IsValid(const CIcsInfo *pIcsInfo) { return pIcsInfo->Valid; }
498
499inline UCHAR IsLongBlock(const CIcsInfo *pIcsInfo) {
500  return (pIcsInfo->WindowSequence != BLOCK_SHORT);
501}
502
503inline UCHAR GetWindowShape(const CIcsInfo *pIcsInfo) {
504  return pIcsInfo->WindowShape;
505}
506
507inline BLOCK_TYPE GetWindowSequence(const CIcsInfo *pIcsInfo) {
508  return pIcsInfo->WindowSequence;
509}
510
511inline const SHORT *GetScaleFactorBandOffsets(
512    const CIcsInfo *pIcsInfo, const SamplingRateInfo *samplingRateInfo) {
513  if (IsLongBlock(pIcsInfo)) {
514    return samplingRateInfo->ScaleFactorBands_Long;
515  } else {
516    return samplingRateInfo->ScaleFactorBands_Short;
517  }
518}
519
520inline UCHAR GetNumberOfScaleFactorBands(
521    const CIcsInfo *pIcsInfo, const SamplingRateInfo *samplingRateInfo) {
522  if (IsLongBlock(pIcsInfo)) {
523    return samplingRateInfo->NumberOfScaleFactorBands_Long;
524  } else {
525    return samplingRateInfo->NumberOfScaleFactorBands_Short;
526  }
527}
528
529inline int GetWindowsPerFrame(const CIcsInfo *pIcsInfo) {
530  return (pIcsInfo->WindowSequence == BLOCK_SHORT) ? 8 : 1;
531}
532
533inline UCHAR GetWindowGroups(const CIcsInfo *pIcsInfo) {
534  return pIcsInfo->WindowGroups;
535}
536
537inline UCHAR GetWindowGroupLength(const CIcsInfo *pIcsInfo, const INT index) {
538  return pIcsInfo->WindowGroupLength[index];
539}
540
541inline const UCHAR *GetWindowGroupLengthTable(const CIcsInfo *pIcsInfo) {
542  return pIcsInfo->WindowGroupLength;
543}
544
545inline UCHAR GetScaleFactorBandsTransmitted(const CIcsInfo *pIcsInfo) {
546  return pIcsInfo->MaxSfBands;
547}
548
549inline UCHAR GetScaleMaxFactorBandsTransmitted(const CIcsInfo *pIcsInfo0,
550                                               const CIcsInfo *pIcsInfo1) {
551  return fMax(pIcsInfo0->MaxSfBands, pIcsInfo1->MaxSfBands);
552}
553
554inline UCHAR GetScaleFactorBandsTotal(const CIcsInfo *pIcsInfo) {
555  return pIcsInfo->TotalSfBands;
556}
557
558/* Note: This function applies to AAC-LC only ! */
559inline UCHAR GetMaximumTnsBands(const CIcsInfo *pIcsInfo,
560                                const int samplingRateIndex) {
561  return tns_max_bands_tbl[samplingRateIndex][!IsLongBlock(pIcsInfo)];
562}
563
564#endif /* #ifndef CHANNELINFO_H */
565