PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/libPCMutils/src/pcmutils_lib.cpp

https://bitbucket.org/bohlooli/aac
C++ | 1278 lines | 825 code | 164 blank | 289 comment | 142 complexity | bea0d39743a9ff6b47baf9557e3b14c4 MD5 | raw file
  1. /* -----------------------------------------------------------------------------------------------------------
  2. Software License for The Fraunhofer FDK AAC Codec Library for Android
  3. © Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
  4. All rights reserved.
  5. 1. INTRODUCTION
  6. The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
  7. the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
  8. This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
  9. AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
  10. audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
  11. independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
  12. of the MPEG specifications.
  13. Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
  14. may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
  15. individually for the purpose of encoding or decoding bit streams in products that are compliant with
  16. the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
  17. these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
  18. software may already be covered under those patent licenses when it is used for those licensed purposes only.
  19. Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
  20. are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
  21. applications information and documentation.
  22. 2. COPYRIGHT LICENSE
  23. Redistribution and use in source and binary forms, with or without modification, are permitted without
  24. payment of copyright license fees provided that you satisfy the following conditions:
  25. You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
  26. your modifications thereto in source code form.
  27. You must retain the complete text of this software license in the documentation and/or other materials
  28. provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
  29. You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
  30. modifications thereto to recipients of copies in binary form.
  31. The name of Fraunhofer may not be used to endorse or promote products derived from this library without
  32. prior written permission.
  33. You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
  34. software or your modifications thereto.
  35. Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
  36. and the date of any change. For modified versions of the FDK AAC Codec, the term
  37. "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
  38. "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
  39. 3. NO PATENT LICENSE
  40. NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
  41. ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
  42. respect to this software.
  43. You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
  44. by appropriate patent licenses.
  45. 4. DISCLAIMER
  46. This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
  47. "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
  48. of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  49. CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
  50. including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
  51. or business interruption, however caused and on any theory of liability, whether in contract, strict
  52. liability, or tort (including negligence), arising in any way out of the use of this software, even if
  53. advised of the possibility of such damage.
  54. 5. CONTACT INFORMATION
  55. Fraunhofer Institute for Integrated Circuits IIS
  56. Attention: Audio and Multimedia Departments - FDK AAC LL
  57. Am Wolfsmantel 33
  58. 91058 Erlangen, Germany
  59. www.iis.fraunhofer.de/amm
  60. amm-info@iis.fraunhofer.de
  61. ----------------------------------------------------------------------------------------------------------- */
  62. /**************************** FDK PCM utils module **************************
  63. Author(s): Christian Griebel
  64. Description: Defines functions to interface with the PCM post processing
  65. module.
  66. *******************************************************************************/
  67. #include "pcmutils_lib.h"
  68. #include "genericStds.h"
  69. #include "fixpoint_math.h"
  70. /* Decoder library info */
  71. #define PCMDMX_LIB_VL0 2
  72. #define PCMDMX_LIB_VL1 3
  73. #define PCMDMX_LIB_VL2 1
  74. #define PCMDMX_LIB_TITLE "PCM Downmix Lib"
  75. #define PCMDMX_LIB_BUILD_DATE __DATE__
  76. #define PCMDMX_LIB_BUILD_TIME __TIME__
  77. /* Library settings */
  78. #define PCM_DMX_MAX_DELAY_FRAMES ( 1 )
  79. #define PCM_DMX_MAX_CHANNELS ( 8 )
  80. #define PCM_DMX_MAX_CHANNEL_GROUPS ( 4 )
  81. #define PCM_DMX_MAX_CHANNELS_PER_GROUP ( 3 ) /* The maximum over all groups */
  82. #define PCMDMX_DFLT_EXPIRY_FRAME ( 50 ) /* At least 500ms (FL 960 @ 96kHz) */
  83. /* Fixed and unique channel group indices.
  84. * The last group index has to be smaller than PCM_DMX_MAX_CHANNEL_GROUPS. */
  85. #define CH_GROUP_FRONT ( 0 )
  86. #define CH_GROUP_SIDE ( 1 )
  87. #define CH_GROUP_REAR ( 2 )
  88. #define CH_GROUP_LFE ( 3 )
  89. /* The ordering of the following fixed channel labels has to be in MPEG-4 style.
  90. * From the center to the back with left and right channel interleaved (starting with left).
  91. * The last channel label index has to be smaller than PCM_DMX_MAX_CHANNELS. */
  92. #define CENTER_FRONT_CHANNEL ( 0 ) /* C */
  93. #define LEFT_FRONT_CHANNEL ( 1 ) /* L */
  94. #define RIGHT_FRONT_CHANNEL ( 2 ) /* R */
  95. #define LEFT_OUTSIDE_CHANNEL ( 3 ) /* Lo */
  96. #define RIGHT_OUTSIDE_CHANNEL ( 4 ) /* Ro */
  97. #define LEFT_REAR_CHANNEL ( 5 ) /* Lr aka left back channel */
  98. #define RIGHT_REAR_CHANNEL ( 6 ) /* Rr aka right back channel */
  99. #define LOW_FREQUENCY_CHANNEL ( 7 ) /* Lf */
  100. /* More constants */
  101. #define ANC_DATA_SYNC_BYTE ( 0xBC ) /* ancillary data sync byte. */
  102. #define ATTENUATION_FACTOR_1 ( FL2FXCONST_SGL(0.70710678f) )
  103. #define TWO_CHANNEL ( 2 )
  104. /* Sanity checks on library setting: */
  105. /* List of packed channel modes */
  106. typedef enum
  107. { /* CH_MODE_<numFrontCh>_<numOutsideCh>_<numRearCh>_<numLfCh> */
  108. CH_MODE_UNDEFINED = 0x0000,
  109. /* 1 channel */
  110. CH_MODE_1_0_0_0 = 0x0001, /* chCfg 1 */
  111. /* 2 channels */
  112. CH_MODE_2_0_0_0 = 0x0002, /* chCfg 2 */
  113. /* 3 channels */
  114. CH_MODE_3_0_0_0 = 0x0003, /* chCfg 3 */
  115. CH_MODE_2_0_1_0 = 0x0102,
  116. CH_MODE_2_0_0_1 = 0x1002,
  117. /* 4 channels */
  118. CH_MODE_3_0_1_0 = 0x0103, /* chCfg 4 */
  119. CH_MODE_2_0_2_0 = 0x0202,
  120. CH_MODE_2_0_1_1 = 0x1102,
  121. /* 5 channels */
  122. CH_MODE_3_0_2_0 = 0x0203, /* chCfg 5 */
  123. CH_MODE_2_0_2_1 = 0x1202,
  124. CH_MODE_3_0_1_1 = 0x1103,
  125. CH_MODE_3_2_0_0 = 0x0023,
  126. /* 6 channels */
  127. CH_MODE_3_0_2_1 = 0x1203, /* chCfg 6 */
  128. CH_MODE_3_2_1_0 = 0x0123,
  129. /* 7 channels */
  130. CH_MODE_2_2_2_1 = 0x1222,
  131. CH_MODE_3_2_1_1 = 0x1123,
  132. CH_MODE_3_2_2_0 = 0x0223,
  133. /* 8 channels */
  134. CH_MODE_3_2_2_1 = 0x1222, /* chCfg 7 */
  135. CH_MODE_3_2_1_2 = 0x2123,
  136. CH_MODE_2_2_2_2 = 0x2222
  137. } PCM_DMX_CHANNEL_MODE;
  138. /* These are the channel configurations linked to
  139. the number of output channels give by the user: */
  140. static const PCM_DMX_CHANNEL_MODE outChModeTable[PCM_DMX_MAX_CHANNELS] =
  141. {
  142. CH_MODE_1_0_0_0, /* 1 channel */
  143. CH_MODE_2_0_0_0, /* 2 channels */
  144. CH_MODE_3_0_0_0, /* 3 channels */
  145. CH_MODE_3_0_1_0, /* 4 channels */
  146. CH_MODE_3_0_2_0, /* 5 channels */
  147. CH_MODE_3_0_2_1, /* 6 channels */
  148. CH_MODE_3_2_2_0, /* 7 channels */
  149. CH_MODE_3_2_2_1 /* 8 channels */
  150. };
  151. static const FIXP_SGL dvbDownmixFactors[8] =
  152. {
  153. FL2FXCONST_SGL(1.0f),
  154. FL2FXCONST_SGL(0.841f),
  155. FL2FXCONST_SGL(0.707f),
  156. FL2FXCONST_SGL(0.596f),
  157. FL2FXCONST_SGL(0.500f),
  158. FL2FXCONST_SGL(0.422f),
  159. FL2FXCONST_SGL(0.355f),
  160. FL2FXCONST_SGL(0.0f)
  161. };
  162. /* MPEG matrix mixdown:
  163. Set 1: L' = (1 + 2^-0.5 + A )^-1 * [L + C * 2^-0.5 + A * Ls];
  164. R' = (1 + 2^-0.5 + A )^-1 * [R + C * 2^-0.5 + A * Rs];
  165. Set 2: L' = (1 + 2^-0.5 + 2A )^-1 * [L + C * 2^-0.5 - A * (Ls + Rs)];
  166. R' = (1 + 2^-0.5 + 2A )^-1 * [R + C * 2^-0.5 + A * (Ls + Rs)];
  167. M = (3 + 2A)^-1 * [L + C + R + A*(Ls + Rs)];
  168. */
  169. static const FIXP_SGL mpegMixDownIdx2Coef[4] =
  170. {
  171. FL2FXCONST_SGL(0.70710678f),
  172. FL2FXCONST_SGL(0.5f),
  173. FL2FXCONST_SGL(0.35355339f),
  174. FL2FXCONST_SGL(0.0f)
  175. };
  176. static const FIXP_SGL mpegMixDownIdx2PreFact[4] =
  177. {
  178. FL2FXCONST_SGL(0.4142135623730950f),
  179. FL2FXCONST_SGL(0.4530818393219728f),
  180. FL2FXCONST_SGL(0.4852813742385703f),
  181. FL2FXCONST_SGL(0.5857864376269050f)
  182. };
  183. typedef struct
  184. {
  185. USHORT matrixMixdownIdx; /*!< MPEG mixdown index extracted from PCE. */
  186. USHORT pseudoSurroundEnable; /*!< Pseudo surround enable flag extracted from PCE. */
  187. USHORT mixdownAvailable; /*!< Will be set to 1 if we found a valid coefficient. */
  188. } MPEG_MIXDOWN_INFO;
  189. typedef struct
  190. {
  191. FIXP_SGL centerMixLevelValue; /*!< DVB mixdown level for the center channel extracted from anc data. */
  192. FIXP_SGL surroundMixLevelValue; /*!< DVB mixdown level for back channels extracted from anc data. */
  193. UCHAR mixLevelsAvail; /*!< Will be set to 1 if we found a valid coefficient. */
  194. } DVB_MIXDOWN_LEVELS;
  195. /* Modules main data structure: */
  196. struct PCM_DMX_INSTANCE
  197. {
  198. DVB_MIXDOWN_LEVELS dvbMixDownLevels[PCM_DMX_MAX_DELAY_FRAMES+1];
  199. MPEG_MIXDOWN_INFO mpegMixDownInfo[PCM_DMX_MAX_DELAY_FRAMES+1];
  200. DUAL_CHANNEL_MODE dualChannelMode;
  201. UINT expiryFrame;
  202. UINT expiryCount;
  203. SHORT numOutputChannels;
  204. UCHAR applyProcessing;
  205. UCHAR frameDelay;
  206. };
  207. /* Memory allocation macro */
  208. C_ALLOC_MEM_STATIC(PcmDmxInstance, struct PCM_DMX_INSTANCE, 1)
  209. /** Evaluate a given channel configuration and extract a packed channel mode and generate a channel offset table
  210. * This function is the inverse to the getChannelDescription() routine.
  211. * @param [in] The total number of channels of the given configuration.
  212. * @param [in] Array holding the corresponding channel types for each channel.
  213. * @param [in] Array holding the corresponding channel type indices for each channel.
  214. * @param [out] Array where the buffer offsets for each channel are stored into.
  215. * @returns Returns the packed channel mode.
  216. **/
  217. static
  218. PCM_DMX_CHANNEL_MODE getChannelMode (
  219. const INT numChannels, /* in */
  220. const AUDIO_CHANNEL_TYPE channelType[], /* in */
  221. const UCHAR channelIndices[], /* in */
  222. UCHAR offsetTable[PCM_DMX_MAX_CHANNELS] /* out */
  223. )
  224. {
  225. UINT chMode = CH_MODE_UNDEFINED;
  226. UCHAR chIdx[PCM_DMX_MAX_CHANNEL_GROUPS][PCM_DMX_MAX_CHANNELS_PER_GROUP];
  227. UCHAR numChInGrp[PCM_DMX_MAX_CHANNEL_GROUPS];
  228. int ch, grpIdx, err = 0;
  229. FDK_ASSERT(channelType != NULL);
  230. FDK_ASSERT(channelIndices != NULL);
  231. FDK_ASSERT(offsetTable != NULL);
  232. /* For details see ISO/IEC 13818-7:2005(E), 8.5.3 Channel configuration */
  233. FDKmemclear(numChInGrp, PCM_DMX_MAX_CHANNEL_GROUPS*sizeof(UCHAR));
  234. FDKmemset(offsetTable, 255, PCM_DMX_MAX_CHANNELS*sizeof(UCHAR));
  235. FDKmemset(chIdx, 255, PCM_DMX_MAX_CHANNEL_GROUPS*PCM_DMX_MAX_CHANNELS_PER_GROUP*sizeof(UCHAR));
  236. /* Categorize channels */
  237. for (ch = 0; ch < numChannels; ch += 1) {
  238. int i = 0, j, chGrpIdx = channelIndices[ch];
  239. switch (channelType[ch]) {
  240. case ACT_FRONT:
  241. case ACT_FRONT_TOP:
  242. grpIdx = CH_GROUP_FRONT;
  243. break;
  244. case ACT_SIDE:
  245. case ACT_SIDE_TOP:
  246. grpIdx = CH_GROUP_SIDE;
  247. break;
  248. case ACT_BACK:
  249. case ACT_BACK_TOP:
  250. grpIdx = CH_GROUP_REAR;
  251. break;
  252. case ACT_LFE:
  253. grpIdx = CH_GROUP_LFE;
  254. break;
  255. default:
  256. err = -1;
  257. continue;
  258. }
  259. if (numChInGrp[grpIdx] < PCM_DMX_MAX_CHANNELS_PER_GROUP) {
  260. /* Sort channels by index */
  261. while ( (i < numChInGrp[grpIdx]) && (chGrpIdx > channelIndices[chIdx[grpIdx][i]]) ) {
  262. i += 1;
  263. }
  264. for (j = numChInGrp[grpIdx]; j > i; j -= 1) {
  265. chIdx[grpIdx][j] = chIdx[grpIdx][j-1];
  266. }
  267. chIdx[grpIdx][i] = ch;
  268. numChInGrp[grpIdx] += 1;
  269. }
  270. }
  271. /* Compose channel offset table */
  272. /* Non-symmetric channels */
  273. if (numChInGrp[CH_GROUP_FRONT] & 0x1) {
  274. /* Odd number of front channels -> we have a center channel.
  275. In MPEG-4 the center has the index 0. */
  276. offsetTable[CENTER_FRONT_CHANNEL] = chIdx[CH_GROUP_FRONT][0];
  277. }
  278. for (grpIdx = 0; grpIdx < PCM_DMX_MAX_CHANNEL_GROUPS; grpIdx += 1) {
  279. int chMapPos, maxChannels = 0;
  280. ch = 0;
  281. switch (grpIdx) {
  282. case CH_GROUP_FRONT:
  283. chMapPos = LEFT_FRONT_CHANNEL;
  284. maxChannels = 3;
  285. ch = numChInGrp[grpIdx] & 0x1;
  286. break;
  287. case CH_GROUP_SIDE:
  288. chMapPos = LEFT_OUTSIDE_CHANNEL;
  289. maxChannels = 2;
  290. break;
  291. case CH_GROUP_REAR:
  292. chMapPos = LEFT_REAR_CHANNEL;
  293. maxChannels = 2;
  294. break;
  295. case CH_GROUP_LFE:
  296. chMapPos = LOW_FREQUENCY_CHANNEL;
  297. maxChannels = 1;
  298. break;
  299. default:
  300. err = -1;
  301. continue;
  302. }
  303. for ( ; ch < numChInGrp[grpIdx]; ch += 1) {
  304. if (ch < maxChannels) {
  305. offsetTable[chMapPos] = chIdx[grpIdx][ch];
  306. chMapPos += 1;
  307. } else {
  308. err = -1;
  309. }
  310. }
  311. }
  312. if (err == 0) {
  313. /* Compose the channel mode */
  314. chMode = (numChInGrp[CH_GROUP_LFE] & 0xF) << 12
  315. | (numChInGrp[CH_GROUP_REAR] & 0xF) << 8
  316. | (numChInGrp[CH_GROUP_SIDE] & 0xF) << 4
  317. | (numChInGrp[CH_GROUP_FRONT] & 0xF);
  318. }
  319. return (PCM_DMX_CHANNEL_MODE)chMode;
  320. }
  321. /** Generate a channel offset table and complete channel description for a given (packed) channel mode.
  322. * This function is the inverse to the getChannelMode() routine.
  323. * @param [in] The total number of channels of the given configuration.
  324. * @param [in] Array containing the channel mapping to be used (From MPEG PCE ordering to whatever is required).
  325. * @param [out] Array where corresponding channel types for each channels are stored into.
  326. * @param [out] Array where corresponding channel type indices for each output channel are stored into.
  327. * @param [out] Array where the buffer offsets for each channel are stored into.
  328. * @returns None.
  329. **/
  330. void getChannelDescription (
  331. const PCM_DMX_CHANNEL_MODE chMode, /* in */
  332. const UCHAR channelMapping[][PCM_DMX_MAX_CHANNELS], /* in */
  333. AUDIO_CHANNEL_TYPE channelType[], /* out */
  334. UCHAR channelIndices[], /* out */
  335. UCHAR offsetTable[PCM_DMX_MAX_CHANNELS] /* out */
  336. )
  337. {
  338. const UCHAR *pChannelMap;
  339. int grpIdx, ch = 0, numChannels = 0;
  340. UCHAR numChInGrp[PCM_DMX_MAX_CHANNEL_GROUPS];
  341. FDK_ASSERT(channelType != NULL);
  342. FDK_ASSERT(channelIndices != NULL);
  343. FDK_ASSERT(channelMapping != NULL);
  344. FDK_ASSERT(offsetTable != NULL);
  345. /* Init output arrays */
  346. FDKmemclear(channelType, PCM_DMX_MAX_CHANNELS*sizeof(AUDIO_CHANNEL_TYPE));
  347. FDKmemclear(channelIndices, PCM_DMX_MAX_CHANNELS*sizeof(UCHAR));
  348. FDKmemset(offsetTable, 255, PCM_DMX_MAX_CHANNELS*sizeof(UCHAR));
  349. /* Extract the number of channels per group */
  350. numChInGrp[CH_GROUP_FRONT] = chMode & 0xF;
  351. numChInGrp[CH_GROUP_SIDE] = (chMode >> 4) & 0xF;
  352. numChInGrp[CH_GROUP_REAR] = (chMode >> 8) & 0xF;
  353. numChInGrp[CH_GROUP_LFE] = (chMode >> 12) & 0xF;
  354. /* Summerize to get the total number of channels */
  355. for (grpIdx = 0; grpIdx < PCM_DMX_MAX_CHANNEL_GROUPS; grpIdx += 1) {
  356. numChannels += numChInGrp[grpIdx];
  357. }
  358. /* Get the appropriate channel map */
  359. pChannelMap = channelMapping[numChannels-1];
  360. /* Compose channel offset table */
  361. /* Non-symmetric channels */
  362. if (numChInGrp[CH_GROUP_FRONT] & 0x1) {
  363. /* Odd number of front channels -> we have a center channel.
  364. In MPEG-4 the center has the index 0. */
  365. offsetTable[CENTER_FRONT_CHANNEL] = pChannelMap[0];
  366. channelType[0] = ACT_FRONT;
  367. ch += 1;
  368. }
  369. for (grpIdx = 0; grpIdx < PCM_DMX_MAX_CHANNEL_GROUPS; grpIdx += 1) {
  370. AUDIO_CHANNEL_TYPE type;
  371. int chMapPos, maxChannels = 0;
  372. int chIdx = 0;
  373. switch (grpIdx) {
  374. case CH_GROUP_FRONT:
  375. type = ACT_FRONT;
  376. chMapPos = LEFT_FRONT_CHANNEL;
  377. maxChannels = 3;
  378. chIdx = numChInGrp[grpIdx] & 0x1;
  379. break;
  380. case CH_GROUP_SIDE:
  381. type = ACT_SIDE;
  382. chMapPos = LEFT_OUTSIDE_CHANNEL;
  383. maxChannels = 2;
  384. break;
  385. case CH_GROUP_REAR:
  386. type = ACT_BACK;
  387. chMapPos = LEFT_REAR_CHANNEL;
  388. maxChannels = 2;
  389. break;
  390. case CH_GROUP_LFE:
  391. type = ACT_LFE;
  392. chMapPos = LOW_FREQUENCY_CHANNEL;
  393. maxChannels = 1;
  394. break;
  395. default:
  396. break;
  397. }
  398. for ( ; (chIdx < numChInGrp[grpIdx]) && (chIdx < maxChannels); chIdx += 1) {
  399. offsetTable[chMapPos] = pChannelMap[ch];
  400. channelType[ch] = type;
  401. channelIndices[ch] = chIdx;
  402. chMapPos += 1;
  403. ch += 1;
  404. }
  405. }
  406. }
  407. /** Open and initialize an instance of the PCM downmix module
  408. * @param [out] Pointer to a buffer receiving the handle of the new instance.
  409. * @returns Returns an error code.
  410. **/
  411. PCMDMX_ERROR pcmDmx_Open (
  412. HANDLE_PCM_DOWNMIX *pSelf
  413. )
  414. {
  415. HANDLE_PCM_DOWNMIX self;
  416. if (pSelf == NULL) {
  417. return (PCMDMX_INVALID_HANDLE);
  418. }
  419. *pSelf = NULL;
  420. self = (HANDLE_PCM_DOWNMIX) GetPcmDmxInstance( 0 );
  421. if (self == NULL) {
  422. return (PCMDMX_OUT_OF_MEMORY);
  423. }
  424. /* Reset the full instance */
  425. pcmDmx_Reset( self, PCMDMX_RESET_FULL );
  426. *pSelf = self;
  427. return (PCMDMX_OK);
  428. }
  429. /** Reset all static values like e.g. mixdown coefficients.
  430. * @param [in] Handle of PCM downmix module instance.
  431. * @param [in] Flags telling which parts of the module shall be reset.
  432. * @returns Returns an error code.
  433. **/
  434. PCMDMX_ERROR pcmDmx_Reset (
  435. HANDLE_PCM_DOWNMIX self,
  436. UINT flags
  437. )
  438. {
  439. if (self == NULL) { return (PCMDMX_INVALID_HANDLE); }
  440. if (flags & PCMDMX_RESET_PARAMS) {
  441. self->dualChannelMode = STEREO_MODE;
  442. self->numOutputChannels = 0;
  443. self->applyProcessing = 0;
  444. self->frameDelay = 0;
  445. self->expiryFrame = PCMDMX_DFLT_EXPIRY_FRAME;
  446. }
  447. if (flags & PCMDMX_RESET_BS_DATA) {
  448. int slot;
  449. for (slot = 0; slot <= PCM_DMX_MAX_DELAY_FRAMES; slot += 1) {
  450. self->dvbMixDownLevels[slot].centerMixLevelValue = dvbDownmixFactors[2]; /* 0.707 */
  451. self->dvbMixDownLevels[slot].surroundMixLevelValue = dvbDownmixFactors[0]; /* 1.000 */
  452. self->dvbMixDownLevels[slot].mixLevelsAvail = 0;
  453. self->mpegMixDownInfo[slot].mixdownAvailable = 0;
  454. }
  455. /* Reset expiry counter */
  456. self->expiryCount = 0;
  457. }
  458. return (PCMDMX_OK);
  459. }
  460. /** Set one parameter for one instance of the PCM downmix module.
  461. * @param [in] Handle of PCM downmix module instance.
  462. * @param [in] Parameter to be set.
  463. * @param [in] Parameter value.
  464. * @returns Returns an error code.
  465. **/
  466. PCMDMX_ERROR pcmDmx_SetParam (
  467. HANDLE_PCM_DOWNMIX self,
  468. PCMDMX_PARAM param,
  469. UINT value
  470. )
  471. {
  472. switch (param)
  473. {
  474. case DMX_BS_DATA_EXPIRY_FRAME:
  475. if (self == NULL)
  476. return (PCMDMX_INVALID_HANDLE);
  477. self->expiryFrame = value;
  478. break;
  479. case DMX_BS_DATA_DELAY:
  480. if (value > PCM_DMX_MAX_DELAY_FRAMES) {
  481. return (PCMDMX_UNABLE_TO_SET_PARAM);
  482. }
  483. if (self == NULL) {
  484. return (PCMDMX_INVALID_HANDLE);
  485. }
  486. self->frameDelay = value;
  487. break;
  488. case NUMBER_OF_OUTPUT_CHANNELS:
  489. switch ((int)value) { /* supported output channels */
  490. case -1: case 0: case 1: case 2:
  491. case 6: case 8:
  492. break;
  493. default:
  494. return (PCMDMX_UNABLE_TO_SET_PARAM);
  495. }
  496. if (self == NULL)
  497. return (PCMDMX_INVALID_HANDLE);
  498. if ((int)value > 0) {
  499. self->numOutputChannels = (int)value;
  500. self->applyProcessing = 1;
  501. } else {
  502. self->numOutputChannels = 0;
  503. self->applyProcessing = 0;
  504. }
  505. break;
  506. case DUAL_CHANNEL_DOWNMIX_MODE:
  507. switch ((DUAL_CHANNEL_MODE)value) {
  508. case STEREO_MODE:
  509. case CH1_MODE:
  510. case CH2_MODE:
  511. case MIXED_MODE:
  512. break;
  513. default:
  514. return (PCMDMX_UNABLE_TO_SET_PARAM);
  515. }
  516. if (self == NULL)
  517. return (PCMDMX_INVALID_HANDLE);
  518. self->dualChannelMode = (DUAL_CHANNEL_MODE)value;
  519. self->applyProcessing = 1;
  520. break;
  521. default:
  522. return (PCMDMX_UNKNOWN_PARAM);
  523. }
  524. return (PCMDMX_OK);
  525. }
  526. /** Read the ancillary data transported in DSEs of DVB streams with MPEG-4 content
  527. * @param [in] Handle of PCM downmix module instance.
  528. * @param [in] Pointer to ancillary data buffer.
  529. * @param [in] Size of ancillary data.
  530. * @param [in] Flag indicating wheter the DVB ancillary data is from an MPEG-1/2 or an MPEG-4 stream.
  531. * @returns Returns an error code.
  532. **/
  533. PCMDMX_ERROR pcmDmx_ReadDvbAncData (
  534. HANDLE_PCM_DOWNMIX self,
  535. UCHAR *pAncDataBuf,
  536. UINT ancDataBytes,
  537. int isMpeg2
  538. )
  539. {
  540. DVB_MIXDOWN_LEVELS *pDownmixLevels = &self->dvbMixDownLevels[0];
  541. int offset = (isMpeg2) ? 2 : 0;
  542. UCHAR ancDataStatus;
  543. if (self == NULL) { return (PCMDMX_INVALID_HANDLE); }
  544. /* sanity checks */
  545. if (pAncDataBuf == NULL || ancDataBytes < (UCHAR)(3+offset)) {
  546. return (PCMDMX_CORRUPT_ANC_DATA);
  547. }
  548. /* check sync word */
  549. if (pAncDataBuf[offset] != ANC_DATA_SYNC_BYTE) {
  550. return (PCMDMX_CORRUPT_ANC_DATA);
  551. }
  552. offset += 2;
  553. ancDataStatus = pAncDataBuf[offset++];
  554. if (isMpeg2) {
  555. /* skip advanced_dynamic_range_control */
  556. if (ancDataStatus & 0x80) offset += 3;
  557. /* skip dialog_normalization */
  558. if (ancDataStatus & 0x40) offset += 1;
  559. /* skip reproduction_level */
  560. if (ancDataStatus & 0x20) offset += 1;
  561. }
  562. else {
  563. /* check reserved bits */
  564. if (ancDataStatus & 0xE8) { return (PCMDMX_CORRUPT_ANC_DATA); }
  565. }
  566. /* downmix_levels_MPEGX */
  567. if (ancDataStatus & 0x10)
  568. {
  569. int foundNewData = 0;
  570. UCHAR downmixData = pAncDataBuf[offset++];
  571. if (downmixData & 0x80) { /* center_mix_level_on */
  572. pDownmixLevels->centerMixLevelValue =
  573. dvbDownmixFactors[(downmixData >> 4) & 0x07];
  574. foundNewData = 1;
  575. } else {
  576. pDownmixLevels->centerMixLevelValue = dvbDownmixFactors[0];
  577. if (downmixData & 0x70) { return (PCMDMX_CORRUPT_ANC_DATA); }
  578. }
  579. if (downmixData & 0x08) { /* surround_mix_level_on */
  580. pDownmixLevels->surroundMixLevelValue =
  581. dvbDownmixFactors[downmixData & 0x07];
  582. foundNewData = 1;
  583. } else {
  584. pDownmixLevels->surroundMixLevelValue = dvbDownmixFactors[0];
  585. if (downmixData & 0x07) { return (PCMDMX_CORRUPT_ANC_DATA); }
  586. }
  587. pDownmixLevels->mixLevelsAvail = foundNewData;
  588. }
  589. /* Reset expiry counter */
  590. self->expiryCount = 0;
  591. return (PCMDMX_OK);
  592. }
  593. /** Set the matrix mixdown information extracted from the PCE of an AAC bitstream.
  594. * Note: Call only if matrix_mixdown_idx_present is true.
  595. * @param [in] Handle of PCM downmix module instance.
  596. * @param [in] The 2 bit matrix mixdown index extracted from PCE.
  597. * @param [in] The pseudo surround enable flag extracted from PCE.
  598. * @returns Returns an error code.
  599. **/
  600. PCMDMX_ERROR pcmDmx_SetMatrixMixdownFromPce (
  601. HANDLE_PCM_DOWNMIX self,
  602. int matrixMixdownPresent,
  603. int matrixMixdownIdx,
  604. int pseudoSurroundEnable
  605. )
  606. {
  607. MPEG_MIXDOWN_INFO *pMpegMixDownInfo;
  608. if (self == NULL) {
  609. return (PCMDMX_INVALID_HANDLE);
  610. }
  611. pMpegMixDownInfo = &self->mpegMixDownInfo[0];
  612. if (matrixMixdownPresent) {
  613. pMpegMixDownInfo->matrixMixdownIdx = matrixMixdownIdx & 0x03;
  614. pMpegMixDownInfo->pseudoSurroundEnable = pseudoSurroundEnable;
  615. }
  616. pMpegMixDownInfo->mixdownAvailable = matrixMixdownPresent;
  617. /* Reset expiry counter */
  618. self->expiryCount = 0;
  619. return (PCMDMX_OK);
  620. }
  621. /** Apply down or up mixing.
  622. * @param [in] Handle of PCM downmix module instance.
  623. * @param [inout] Pointer to time buffer. Depending on interface configuration, the content of pTimeData is ignored,
  624. * and the internal QMF buffer will be used as input data source. Otherwise, the MPEG Surround processing is
  625. * applied to the timesignal pTimeData. For both variants, the resulting MPEG Surround signal is written into pTimeData.
  626. * @param [in] Pointer where the amount of output samples is returned into.
  627. * @param [inout] Pointer where the amount of output channels is returned into.
  628. * @param [in] Flag which indicates if output time data are writtern interleaved or as subsequent blocks.
  629. * @param [inout] Array where the corresponding channel type for each output audio channel is stored into.
  630. * @param [inout] Array where the corresponding channel type index for each output audio channel is stored into.
  631. * @param [in] Array containing the output channel mapping to be used (From MPEG PCE ordering to whatever is required).
  632. * @returns Returns an error code.
  633. **/
  634. PCMDMX_ERROR pcmDmx_ApplyFrame (
  635. HANDLE_PCM_DOWNMIX self,
  636. INT_PCM *pPcmBuf,
  637. UINT frameSize,
  638. INT *nChannels,
  639. int fInterleaved,
  640. AUDIO_CHANNEL_TYPE channelType[],
  641. UCHAR channelIndices[],
  642. const UCHAR channelMapping[][8]
  643. )
  644. {
  645. PCMDMX_ERROR errorStatus = PCMDMX_OK;
  646. DUAL_CHANNEL_MODE dualChannelMode;
  647. PCM_DMX_CHANNEL_MODE inChMode;
  648. int numOutChannels;
  649. int numInChannels = *nChannels;
  650. int slot;
  651. UCHAR inOffsetTable[PCM_DMX_MAX_CHANNELS];
  652. MPEG_MIXDOWN_INFO mpegMixDownInfo;
  653. DVB_MIXDOWN_LEVELS dvbMixDownLevels;
  654. if (self == NULL) { return (PCMDMX_INVALID_HANDLE); }
  655. if ( (self->expiryFrame > 0)
  656. && (++self->expiryCount > self->expiryFrame) )
  657. { /* The metadata read from bitstream is too old. */
  658. errorStatus = pcmDmx_Reset(self, PCMDMX_RESET_BS_DATA);
  659. }
  660. FDKmemcpy(&mpegMixDownInfo, &self->mpegMixDownInfo[self->frameDelay], sizeof(MPEG_MIXDOWN_INFO));
  661. /* Maintain delay line */
  662. for (slot = self->frameDelay; slot > 0; slot -= 1) {
  663. FDKmemcpy(&self->mpegMixDownInfo[slot], &self->mpegMixDownInfo[slot-1], sizeof(MPEG_MIXDOWN_INFO));
  664. }
  665. FDKmemcpy(&dvbMixDownLevels, &self->dvbMixDownLevels[self->frameDelay], sizeof(DVB_MIXDOWN_LEVELS));
  666. /* Maintain delay line */
  667. for (slot = self->frameDelay; slot > 0; slot -= 1) {
  668. FDKmemcpy(&self->dvbMixDownLevels[slot], &self->dvbMixDownLevels[slot-1], sizeof(DVB_MIXDOWN_LEVELS));
  669. }
  670. if (self->applyProcessing == 0) { return (errorStatus); }
  671. if (pPcmBuf == NULL) { return (PCMDMX_INVALID_ARGUMENT); }
  672. if (frameSize == 0) { return (PCMDMX_INVALID_ARGUMENT); }
  673. if (numInChannels == 0) { return (PCMDMX_INVALID_ARGUMENT); }
  674. if (self->numOutputChannels <= 0) {
  675. numOutChannels = numInChannels;
  676. } else {
  677. numOutChannels = self->numOutputChannels;
  678. }
  679. dualChannelMode = self->dualChannelMode;
  680. /* Analyse input channel configuration and get channel offset
  681. * table that can be accessed with the fixed channel labels. */
  682. inChMode = getChannelMode(
  683. numInChannels,
  684. channelType,
  685. channelIndices,
  686. inOffsetTable
  687. );
  688. if (inChMode == CH_MODE_UNDEFINED) {
  689. /* We don't need to restore because the channel
  690. configuration has not been changed. Just exit. */
  691. return (PCMDMX_INVALID_CH_CONFIG);
  692. }
  693. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  694. if ( numInChannels > numOutChannels )
  695. { /* Apply downmix */
  696. INT_PCM *pInCF, *pInLF, *pInRF, *pInLO, *pInRO, *pInLR, *pInRR, *pOutL, *pOutR;
  697. FIXP_SGL flev, clev, slev;
  698. UINT sample;
  699. int inStride, outStride, offset;
  700. int useGuidedDownMix = 0;
  701. UCHAR outOffsetTable[PCM_DMX_MAX_CHANNELS];
  702. /* Set I/O strides and offsets */
  703. if (fInterleaved) {
  704. inStride = numInChannels;
  705. outStride = TWO_CHANNEL; /* The output of STAGE ONE is always STEREO !!!
  706. STAGE TWO creates a downmix to mono if required. */
  707. offset = 1; /* Channel specific offset factor */
  708. } else {
  709. inStride = 1;
  710. outStride = 1;
  711. offset = frameSize; /* Channel specific offset factor */
  712. }
  713. /* Get channel description and channel mapping for this
  714. * stages number of output channels (always STEREO). */
  715. getChannelDescription(
  716. CH_MODE_2_0_0_0,
  717. channelMapping,
  718. channelType,
  719. channelIndices,
  720. outOffsetTable
  721. );
  722. /* Now there is no way back because we modified the channel configuration! */
  723. /* Set channel pointer for input */
  724. pInCF = &pPcmBuf[inOffsetTable[CENTER_FRONT_CHANNEL]*offset];
  725. pInLF = &pPcmBuf[inOffsetTable[LEFT_FRONT_CHANNEL]*offset];
  726. pInRF = &pPcmBuf[inOffsetTable[RIGHT_FRONT_CHANNEL]*offset];
  727. pInLO = &pPcmBuf[inOffsetTable[LEFT_OUTSIDE_CHANNEL]*offset];
  728. pInRO = &pPcmBuf[inOffsetTable[RIGHT_OUTSIDE_CHANNEL]*offset];
  729. pInLR = &pPcmBuf[inOffsetTable[LEFT_REAR_CHANNEL]*offset];
  730. pInRR = &pPcmBuf[inOffsetTable[RIGHT_REAR_CHANNEL]*offset];
  731. /* Set channel pointer for output
  732. Caution: Different channel mapping compared to input */
  733. pOutL = &pPcmBuf[outOffsetTable[LEFT_FRONT_CHANNEL]*offset]; /* LEFT_FRONT_CHANNEL */
  734. pOutR = &pPcmBuf[outOffsetTable[RIGHT_FRONT_CHANNEL]*offset]; /* RIGHT_FRONT_CHANNEL */
  735. /* Set downmix levels: */
  736. flev = ATTENUATION_FACTOR_1; /* 0.707 */
  737. clev = ATTENUATION_FACTOR_1; /* 0.707 */
  738. slev = ATTENUATION_FACTOR_1; /* 0.707 */
  739. if ( dvbMixDownLevels.mixLevelsAvail ) {
  740. clev = dvbMixDownLevels.centerMixLevelValue;
  741. slev = dvbMixDownLevels.surroundMixLevelValue;
  742. useGuidedDownMix = 1;
  743. }
  744. /* FIRST STAGE:
  745. Always downmix to 2 channel output: */
  746. switch ( inChMode )
  747. {
  748. case CH_MODE_2_0_0_0:
  749. case CH_MODE_2_0_0_1:
  750. /* 2/0 input: */
  751. switch (dualChannelMode)
  752. {
  753. case CH1_MODE: /* L' = 0.707 * Ch1; R' = 0.707 * Ch1 */
  754. for (sample = 0; sample < frameSize; sample++) {
  755. *pOutL = *pOutR =
  756. (INT_PCM)SATURATE_RIGHT_SHIFT(fMult((FIXP_PCM)*pInLF, flev), DFRACT_BITS-SAMPLE_BITS, SAMPLE_BITS);
  757. pInLF += inStride;
  758. pOutL += outStride; pOutR += outStride;
  759. }
  760. break;
  761. case CH2_MODE: /* L' = 0.707 * Ch2; R' = 0.707 * Ch2 */
  762. for (sample = 0; sample < frameSize; sample++) {
  763. *pOutL = *pOutR =
  764. (INT_PCM)SATURATE_RIGHT_SHIFT(fMult((FIXP_PCM)*pInRF, flev), DFRACT_BITS-SAMPLE_BITS, SAMPLE_BITS);
  765. pInRF += inStride;
  766. pOutL += outStride; pOutR += outStride;
  767. }
  768. break;
  769. case MIXED_MODE: /* L' = 0.5*Ch1 + 0.5*Ch2; R' = 0.5*Ch1 + 0.5*Ch2 */
  770. for (sample = 0; sample < frameSize; sample++) {
  771. *pOutL = *pOutR = (*pInLF >> 1) + (*pInRF >> 1);
  772. pInLF += inStride; pInRF += inStride;
  773. pOutL += outStride; pOutR += outStride;
  774. }
  775. break;
  776. default:
  777. case STEREO_MODE:
  778. /* nothing to do */
  779. break;
  780. }
  781. break;
  782. case CH_MODE_3_0_0_0:
  783. /* 3/0 input: L' = L + 0.707*C; R' = R + 0.707*C; */
  784. for (sample = 0; sample < frameSize; sample++)
  785. {
  786. FIXP_DBL tCF = fMultDiv2((FIXP_PCM)*pInCF, clev);
  787. #if (SAMPLE_BITS == 32)
  788. /* left channel */
  789. *pOutL = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>1)+tCF, 1, SAMPLE_BITS);
  790. /* right channel */
  791. *pOutR = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>1)+tCF, 1, SAMPLE_BITS);
  792. #else
  793. /* left channel */
  794. *pOutL = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>1)+tCF, DFRACT_BITS-SAMPLE_BITS-1, SAMPLE_BITS);
  795. /* right channel */
  796. *pOutR = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>1)+tCF, DFRACT_BITS-SAMPLE_BITS-1, SAMPLE_BITS);
  797. #endif
  798. pInLF += inStride; pInRF += inStride; pInCF += inStride;
  799. pOutL += outStride; pOutR += outStride;
  800. }
  801. break;
  802. /* 2/1 input: not supported!
  803. case CH_MODE_2_0_1_0: */
  804. case CH_MODE_3_0_1_0:
  805. if (useGuidedDownMix) {
  806. /* 3/1 input: L' = L + clev*C + 0.707*slev*S; R' = R + clev*C + 0.707*slev*S; */
  807. slev = FX_DBL2FX_SGL(fMult(flev, slev)); /* 0.707*slef */
  808. for (sample = 0; sample < frameSize; sample++)
  809. {
  810. FIXP_DBL tCF = fMultDiv2((FIXP_PCM)*pInCF, clev) >> 1;
  811. FIXP_DBL tLR = fMultDiv2((FIXP_PCM)*pInLR, slev) >> 1;
  812. #if (SAMPLE_BITS == 32)
  813. /* left channel */
  814. *pOutL = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>2)+tCF+tLR, 2, SAMPLE_BITS);
  815. /* right channel */
  816. *pOutR = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>2)+tCF+tLR, 2, SAMPLE_BITS);
  817. #else
  818. /* left channel */
  819. *pOutL = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>2)+tCF-tLR, DFRACT_BITS-SAMPLE_BITS-2, SAMPLE_BITS);
  820. /* right channel */
  821. *pOutR = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>2)+tCF+tLR, DFRACT_BITS-SAMPLE_BITS-2, SAMPLE_BITS);
  822. #endif
  823. pInLF += inStride; pInRF += inStride; pInCF += inStride; pInLR += inStride;
  824. pOutL += outStride; pOutR += outStride;
  825. }
  826. } else {
  827. /* 3/1 input: L' = L + 0.707*C - 0.707*S; R' = R + 0.707*C + 0.707*S */
  828. for (sample = 0; sample < frameSize; sample++)
  829. {
  830. FIXP_DBL tCF = fMultDiv2((FIXP_PCM)*pInCF, clev) >> 1;
  831. FIXP_DBL tLR = fMultDiv2((FIXP_PCM)*pInLR, slev) >> 1;
  832. #if (SAMPLE_BITS == 32)
  833. /* left channel */
  834. *pOutL = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>2)+tCF-tLR, 2, SAMPLE_BITS);
  835. /* right channel */
  836. *pOutR = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>2)+tCF+tLR, 2, SAMPLE_BITS);
  837. #else
  838. /* left channel */
  839. *pOutL = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>2)+tCF-tLR, DFRACT_BITS-SAMPLE_BITS-2, SAMPLE_BITS);
  840. /* right channel */
  841. *pOutR = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>2)+tCF+tLR, DFRACT_BITS-SAMPLE_BITS-2, SAMPLE_BITS);
  842. #endif
  843. pInLF += inStride; pInRF += inStride; pInCF += inStride; pInLR += inStride;
  844. pOutL += outStride; pOutR += outStride;
  845. }
  846. }
  847. break;
  848. /* 2/2 input: not supported!
  849. case CH_MODE_2_0_2_0: */
  850. case CH_MODE_3_0_2_0: /* 5.0ch input */
  851. case CH_MODE_3_0_2_1: /* 5.1ch input */
  852. if (useGuidedDownMix) {
  853. /* 3/2 input: L' = L + clev*C + slev*Ls; R' = R + clev*C + slev*Rs; */
  854. for (sample = 0; sample < frameSize; sample++)
  855. {
  856. FIXP_DBL tCF = fMultDiv2((FIXP_PCM)*pInCF, clev) >> 1;
  857. FIXP_DBL tLR = fMultDiv2((FIXP_PCM)*pInLR, slev) >> 1;
  858. FIXP_DBL tRR = fMultDiv2((FIXP_PCM)*pInRR, slev) >> 1;
  859. #if (SAMPLE_BITS == 32)
  860. /* left channel */
  861. *pOutL = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>2)+tCF+tLR, 2, SAMPLE_BITS);
  862. /* right channel */
  863. *pOutR = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>2)+tCF+tRR, 2, SAMPLE_BITS);
  864. #else
  865. /* left channel */
  866. *pOutL = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>2)+tCF+tLR, DFRACT_BITS-SAMPLE_BITS-2, SAMPLE_BITS);
  867. /* right channel */
  868. *pOutR = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>2)+tCF+tRR, DFRACT_BITS-SAMPLE_BITS-2, SAMPLE_BITS);
  869. #endif
  870. pInLF += inStride; pInRF += inStride; pInCF += inStride; pInLR += inStride; pInRR += inStride;
  871. pOutL += outStride; pOutR += outStride;
  872. }
  873. }
  874. else if (mpegMixDownInfo.mixdownAvailable) {
  875. /* 3/2 input: L' = (1.707+A)^-1 * [L+0.707*C+A*Ls]; R'= (1.707+A)^-1 * [R+0.707*C+A*Rs]; */
  876. FIXP_SGL mtrxMixDwnCoef = mpegMixDownIdx2Coef[mpegMixDownInfo.matrixMixdownIdx];
  877. FIXP_SGL mtrxMixDwnPreFact = mpegMixDownIdx2PreFact[mpegMixDownInfo.matrixMixdownIdx];
  878. clev = FX_DBL2FX_SGL(fMult(mtrxMixDwnPreFact, flev /* 0.707 */));
  879. flev = mtrxMixDwnPreFact;
  880. slev = FX_DBL2FX_SGL(fMult(mtrxMixDwnPreFact, mtrxMixDwnCoef));
  881. for (sample = 0; sample < frameSize; sample++)
  882. {
  883. FIXP_DBL tCF = fMultDiv2((FIXP_PCM)*pInCF, clev);
  884. FIXP_DBL tLF = fMultDiv2((FIXP_PCM)*pInLF, flev);
  885. FIXP_DBL tRF = fMultDiv2((FIXP_PCM)*pInRF, flev);
  886. FIXP_DBL tLR = fMultDiv2((FIXP_PCM)*pInLR, slev);
  887. FIXP_DBL tRR = fMultDiv2((FIXP_PCM)*pInRR, slev);
  888. #if (SAMPLE_BITS == 32)
  889. /* left channel */
  890. *pOutL = (INT_PCM)SATURATE_LEFT_SHIFT(tLF+tCF+tLR, 1, SAMPLE_BITS);
  891. /* right channel */
  892. *pOutR = (INT_PCM)SATURATE_LEFT_SHIFT(tRF+tCF+tRR, 1, SAMPLE_BITS);
  893. #else
  894. /* left channel */
  895. *pOutL = (INT_PCM)SATURATE_RIGHT_SHIFT(tLF+tCF+tLR, DFRACT_BITS-SAMPLE_BITS-1, SAMPLE_BITS);
  896. /* right channel */
  897. *pOutR = (INT_PCM)SATURATE_RIGHT_SHIFT(tRF+tCF+tRR, DFRACT_BITS-SAMPLE_BITS-1, SAMPLE_BITS);
  898. #endif
  899. pInLF += inStride; pInRF += inStride; pInCF += inStride; pInLR += inStride; pInRR += inStride;
  900. pOutL += outStride; pOutR += outStride;
  901. }
  902. }
  903. else {
  904. /* 3/2 input: L' = L + 0.707*C - 0.707*Ls - 0.707*Rs; R' = R + 0.707*C + 0.707*Ls + 0.707*Rs */
  905. for (sample = 0; sample < frameSize; sample++)
  906. {
  907. FIXP_DBL tCF = fMultDiv2((FIXP_PCM)*pInCF, clev) >> 2;
  908. FIXP_DBL tLR = fMultDiv2((FIXP_PCM)*pInLR, slev) >> 2;
  909. FIXP_DBL tRR = fMultDiv2((FIXP_PCM)*pInRR, slev) >> 2;
  910. #if (SAMPLE_BITS == 32)
  911. /* left channel */
  912. *pOutL = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>3)+tCF-tLR-tRR, 3, SAMPLE_BITS);
  913. /* right channel */
  914. *pOutR = (INT_PCM)SATURATE_LEFT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>3)+tCF+tLR+tRR, 3, SAMPLE_BITS);
  915. #else
  916. /* left channel */
  917. *pOutL = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInLF)>>3)+tCF-tLR-tRR, DFRACT_BITS-SAMPLE_BITS-3, SAMPLE_BITS);
  918. /* right channel */
  919. *pOutR = (INT_PCM)SATURATE_RIGHT_SHIFT((FX_PCM2FX_DBL((FIXP_PCM)*pInRF)>>3)+tCF+tLR+tRR, DFRACT_BITS-SAMPLE_BITS-3, SAMPLE_BITS);
  920. #endif
  921. pInLF += inStride; pInRF += inStride; pInCF += inStride; pInLR += inStride; pInRR += inStride;
  922. pOutL += outStride; pOutR += outStride;
  923. }
  924. }
  925. break;
  926. default:
  927. errorStatus = PCMDMX_INVALID_MODE;
  928. break;
  929. }
  930. /* SECOND STAGE:
  931. If desired create a mono donwmix:
  932. Note: Input are always two channels! */
  933. if (numOutChannels == 1)
  934. {
  935. INT_PCM *pOutC;
  936. FIXP_SGL mlev;
  937. if (useGuidedDownMix) mlev = FL2FXCONST_SGL(1.0f); else mlev = flev;
  938. /* Output of STAGE ONE = Input of STAGE TWO */
  939. FDKmemcpy(inOffsetTable, outOffsetTable, PCM_DMX_MAX_CHANNELS*sizeof(UCHAR));
  940. /* Set I/O strides and offsets */
  941. inStride = outStride; /* output from STAGE ONE */
  942. outStride = numOutChannels; /* final output */
  943. /* Get channel description and channel mapping for this
  944. * stages number of output channels (always MONO). */
  945. getChannelDescription(
  946. CH_MODE_1_0_0_0,
  947. channelMapping,
  948. channelType,
  949. channelIndices,
  950. outOffsetTable
  951. );
  952. /* Set input channel pointer. */
  953. pInLF = &pPcmBuf[inOffsetTable[LEFT_FRONT_CHANNEL]*offset];
  954. pInRF = &pPcmBuf[inOffsetTable[RIGHT_FRONT_CHANNEL]*offset];
  955. /* Set output channel pointer */
  956. pOutC = &pPcmBuf[outOffsetTable[CENTER_FRONT_CHANNEL]*offset];
  957. /* C' = 0.707*L + 0.707*R */
  958. for (sample = 0; sample < frameSize; sample++) {
  959. #if (SAMPLE_BITS == 32)
  960. *pOutC =
  961. (INT_PCM)SATURATE_LEFT_SHIFT(fMultDiv2((FIXP_PCM)*pInLF,mlev)+fMultDiv2((FIXP_PCM)*pInRF,mlev), 1, SAMPLE_BITS);
  962. #else
  963. *pOutC =
  964. (INT_PCM)SATURATE_RIGHT_SHIFT(fMultDiv2((FIXP_PCM)*pInLF,mlev)+fMultDiv2((FIXP_PCM)*pInRF,mlev), DFRACT_BITS-SAMPLE_BITS-1, SAMPLE_BITS);
  965. #endif
  966. pInLF += inStride; pInRF += inStride;
  967. pOutC += 1;
  968. }
  969. /* Finished STAGE TWO */
  970. }
  971. /* Update the number of output channels */
  972. *nChannels = self->numOutputChannels;
  973. } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  974. else
  975. if ( numInChannels == numOutChannels )
  976. {
  977. /* Don't need to change the channel description here */
  978. switch (numInChannels)
  979. {
  980. case 2:
  981. { /* Set up channel pointer */
  982. INT_PCM *pInLF, *pInRF, *pOutL, *pOutR;
  983. FIXP_SGL flev;
  984. UINT sample;
  985. int inStride, outStride, offset;
  986. if (fInterleaved) {
  987. inStride = numInChannels;
  988. outStride = 2; /* fixed !!! (below stereo is donwmixed to mono if required */
  989. offset = 1; /* Channel specific offset factor */
  990. } else {
  991. inStride = 1;
  992. outStride = 1;
  993. offset = frameSize; /* Channel specific offset factor */
  994. }
  995. /* Set input channel pointer */
  996. pInLF = &pPcmBuf[inOffsetTable[LEFT_FRONT_CHANNEL]*offset];
  997. pInRF = &pPcmBuf[inOffsetTable[RIGHT_FRONT_CHANNEL]*offset];
  998. /* Set output channel pointer (same as input) */
  999. pOutL = pInLF;
  1000. pOutR = pInRF;
  1001. /* Set downmix levels: */
  1002. flev = ATTENUATION_FACTOR_1; /* 0.707 */
  1003. /* 2/0 input: */
  1004. switch (dualChannelMode)
  1005. {
  1006. case CH1_MODE: /* L' = 0.707 * Ch1; R' = 0.707 * Ch1 */
  1007. for (sample = 0; sample < frameSize; sample++) {
  1008. *pOutL = *pOutR =
  1009. (INT_PCM)SATURATE_RIGHT_SHIFT(fMult((FIXP_PCM)*pInLF, flev), DFRACT_BITS-SAMPLE_BITS, SAMPLE_BITS);
  1010. pInLF += inStride;
  1011. pOutL += outStride; pOutR += outStride;
  1012. }
  1013. break;
  1014. case CH2_MODE: /* L' = 0.707 * Ch2; R' = 0.707 * Ch2 */
  1015. for (sample = 0; sample < frameSize; sample++) {
  1016. *pOutL = *pOutR =
  1017. (INT_PCM)SATURATE_RIGHT_SHIFT(fMult((FIXP_PCM)*pInRF, flev), DFRACT_BITS-SAMPLE_BITS, SAMPLE_BITS);
  1018. pInRF += inStride;
  1019. pOutL += outStride; pOutR += outStride;
  1020. }
  1021. break;
  1022. case MIXED_MODE: /* L' = 0.5*Ch1 + 0.5*Ch2; R' = 0.5*Ch1 + 0.5*Ch2 */
  1023. for (sample = 0; sample < frameSize; sample++) {
  1024. *pOutL = *pOutR = (*pInLF >> 1) + (*pInRF >> 1);
  1025. pInLF += inStride; pInRF += inStride;
  1026. pOutL += outStride; pOutR += outStride;
  1027. }
  1028. break;
  1029. default:
  1030. case STEREO_MODE:
  1031. /* nothing to do */
  1032. break;
  1033. }
  1034. }
  1035. break;
  1036. default:
  1037. /* nothing to do */
  1038. break;
  1039. }
  1040. }
  1041. return (errorStatus);
  1042. }
  1043. /** Close an instance of the PCM downmix module.
  1044. * @param [inout] Pointer to a buffer containing the handle of the instance.
  1045. * @returns Returns an error code.
  1046. **/
  1047. PCMDMX_ERROR pcmDmx_Close (
  1048. HANDLE_PCM_DOWNMIX *pSelf
  1049. )
  1050. {
  1051. if (pSelf == NULL) {
  1052. return (PCMDMX_INVALID_HANDLE);
  1053. }
  1054. FreePcmDmxInstance( pSelf );
  1055. *pSelf = NULL;
  1056. return (PCMDMX_OK);
  1057. }
  1058. /** Get library info for this module.
  1059. * @param [out] Pointer to an allocated LIB_INFO structure.
  1060. * @returns Returns an error code.
  1061. */
  1062. PCMDMX_ERROR pcmDmx_GetLibInfo( LIB_INFO *info )
  1063. {
  1064. int i;
  1065. if (info == NULL) {
  1066. return PCMDMX_INVALID_ARGUMENT;
  1067. }
  1068. /* Search for next free tab */
  1069. for (i = 0; i < FDK_MODULE_LAST; i++) {
  1070. if (info[i].module_id == FDK_NONE) break;
  1071. }
  1072. if (i == FDK_MODULE_LAST) {
  1073. return PCMDMX_UNKNOWN;
  1074. }
  1075. info += i;
  1076. /* Add the library info */
  1077. info->module_id = FDK_PCMDMX;
  1078. info->version = LIB_VERSION(PCMDMX_LIB_VL0, PCMDMX_LIB_VL1, PCMDMX_LIB_VL2);
  1079. LIB_VERSION_STRING(info);
  1080. info->build_date = PCMDMX_LIB_BUILD_DATE;
  1081. info->build_time = PCMDMX_LIB_BUILD_TIME;
  1082. info->title = PCMDMX_LIB_TITLE;
  1083. /* Set flags */
  1084. info->flags = 0
  1085. | CAPF_DMX_BLIND /* At least blind downmixing is possible */
  1086. | CAPF_DMX_PCE /* Guided downmix with data from MPEG-2/4 Program Config Elements (PCE). */
  1087. | CAPF_DMX_DVB /* Guided downmix with data from DVB ancillary data fields. */
  1088. ;
  1089. return PCMDMX_OK;
  1090. }