sbrside.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: sbrside.c,v 1.2 2005/05/24 16:01:55 albertofloyd Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com)
  40. * February 2005
  41. *
  42. * sbrside.c - functions for unpacking side info from SBR bitstream
  43. **************************************************************************************/
  44. #include "sbr.h"
  45. /**************************************************************************************
  46. * Function: GetSampRateIdx
  47. *
  48. * Description: get index of given sample rate
  49. *
  50. * Inputs: sample rate (in Hz)
  51. *
  52. * Outputs: none
  53. *
  54. * Return: index of sample rate (table 1.15 in 14496-3:2001(E))
  55. * -1 if sample rate not found in table
  56. **************************************************************************************/
  57. int GetSampRateIdx(int sampRate)
  58. {
  59. int idx;
  60. for (idx = 0; idx < NUM_SAMPLE_RATES; idx++) {
  61. if (sampRate == sampRateTab[idx])
  62. return idx;
  63. }
  64. return -1;
  65. }
  66. /**************************************************************************************
  67. * Function: UnpackSBRHeader
  68. *
  69. * Description: unpack SBR header (table 4.56)
  70. *
  71. * Inputs: BitStreamInfo struct pointing to start of SBR header
  72. *
  73. * Outputs: initialized SBRHeader struct for this SCE/CPE block
  74. *
  75. * Return: non-zero if frame reset is triggered, zero otherwise
  76. **************************************************************************************/
  77. int UnpackSBRHeader(BitStreamInfo *bsi, SBRHeader *sbrHdr)
  78. {
  79. SBRHeader sbrHdrPrev;
  80. /* save previous values so we know whether to reset decoder */
  81. sbrHdrPrev.startFreq = sbrHdr->startFreq;
  82. sbrHdrPrev.stopFreq = sbrHdr->stopFreq;
  83. sbrHdrPrev.freqScale = sbrHdr->freqScale;
  84. sbrHdrPrev.alterScale = sbrHdr->alterScale;
  85. sbrHdrPrev.crossOverBand = sbrHdr->crossOverBand;
  86. sbrHdrPrev.noiseBands = sbrHdr->noiseBands;
  87. sbrHdr->ampRes = GetBits(bsi, 1);
  88. sbrHdr->startFreq = GetBits(bsi, 4);
  89. sbrHdr->stopFreq = GetBits(bsi, 4);
  90. sbrHdr->crossOverBand = GetBits(bsi, 3);
  91. sbrHdr->resBitsHdr = GetBits(bsi, 2);
  92. sbrHdr->hdrExtra1 = GetBits(bsi, 1);
  93. sbrHdr->hdrExtra2 = GetBits(bsi, 1);
  94. if (sbrHdr->hdrExtra1) {
  95. sbrHdr->freqScale = GetBits(bsi, 2);
  96. sbrHdr->alterScale = GetBits(bsi, 1);
  97. sbrHdr->noiseBands = GetBits(bsi, 2);
  98. } else {
  99. /* defaults */
  100. sbrHdr->freqScale = 2;
  101. sbrHdr->alterScale = 1;
  102. sbrHdr->noiseBands = 2;
  103. }
  104. if (sbrHdr->hdrExtra2) {
  105. sbrHdr->limiterBands = GetBits(bsi, 2);
  106. sbrHdr->limiterGains = GetBits(bsi, 2);
  107. sbrHdr->interpFreq = GetBits(bsi, 1);
  108. sbrHdr->smoothMode = GetBits(bsi, 1);
  109. } else {
  110. /* defaults */
  111. sbrHdr->limiterBands = 2;
  112. sbrHdr->limiterGains = 2;
  113. sbrHdr->interpFreq = 1;
  114. sbrHdr->smoothMode = 1;
  115. }
  116. sbrHdr->count++;
  117. /* if any of these have changed from previous frame, reset the SBR module */
  118. if (sbrHdr->startFreq != sbrHdrPrev.startFreq || sbrHdr->stopFreq != sbrHdrPrev.stopFreq ||
  119. sbrHdr->freqScale != sbrHdrPrev.freqScale || sbrHdr->alterScale != sbrHdrPrev.alterScale ||
  120. sbrHdr->crossOverBand != sbrHdrPrev.crossOverBand || sbrHdr->noiseBands != sbrHdrPrev.noiseBands
  121. )
  122. return -1;
  123. else
  124. return 0;
  125. }
  126. /* cLog2[i] = ceil(log2(i)) (disregard i == 0) */
  127. static const unsigned char cLog2[9] = {0, 0, 1, 2, 2, 3, 3, 3, 3};
  128. /**************************************************************************************
  129. * Function: UnpackSBRGrid
  130. *
  131. * Description: unpack SBR grid (table 4.62)
  132. *
  133. * Inputs: BitStreamInfo struct pointing to start of SBR grid
  134. * initialized SBRHeader struct for this SCE/CPE block
  135. *
  136. * Outputs: initialized SBRGrid struct for this channel
  137. *
  138. * Return: none
  139. **************************************************************************************/
  140. static void UnpackSBRGrid(BitStreamInfo *bsi, SBRHeader *sbrHdr, SBRGrid *sbrGrid)
  141. {
  142. int numEnvRaw, env, rel, pBits, border, middleBorder=0;
  143. unsigned char relBordLead[MAX_NUM_ENV], relBordTrail[MAX_NUM_ENV];
  144. unsigned char relBorder0[3], relBorder1[3], relBorder[3];
  145. unsigned char numRelBorder0, numRelBorder1, numRelBorder, numRelLead=0, numRelTrail;
  146. unsigned char absBordLead=0, absBordTrail=0, absBorder;
  147. sbrGrid->ampResFrame = sbrHdr->ampRes;
  148. sbrGrid->frameClass = GetBits(bsi, 2);
  149. switch (sbrGrid->frameClass) {
  150. case SBR_GRID_FIXFIX:
  151. numEnvRaw = GetBits(bsi, 2);
  152. sbrGrid->numEnv = (1 << numEnvRaw);
  153. if (sbrGrid->numEnv == 1)
  154. sbrGrid->ampResFrame = 0;
  155. ASSERT(sbrGrid->numEnv == 1 || sbrGrid->numEnv == 2 || sbrGrid->numEnv == 4);
  156. sbrGrid->freqRes[0] = GetBits(bsi, 1);
  157. for (env = 1; env < sbrGrid->numEnv; env++)
  158. sbrGrid->freqRes[env] = sbrGrid->freqRes[0];
  159. absBordLead = 0;
  160. absBordTrail = NUM_TIME_SLOTS;
  161. numRelLead = sbrGrid->numEnv - 1;
  162. numRelTrail = 0;
  163. /* numEnv = 1, 2, or 4 */
  164. if (sbrGrid->numEnv == 1) border = NUM_TIME_SLOTS / 1;
  165. else if (sbrGrid->numEnv == 2) border = NUM_TIME_SLOTS / 2;
  166. else border = NUM_TIME_SLOTS / 4;
  167. for (rel = 0; rel < numRelLead; rel++)
  168. relBordLead[rel] = border;
  169. middleBorder = (sbrGrid->numEnv >> 1);
  170. break;
  171. case SBR_GRID_FIXVAR:
  172. absBorder = GetBits(bsi, 2) + NUM_TIME_SLOTS;
  173. numRelBorder = GetBits(bsi, 2);
  174. sbrGrid->numEnv = numRelBorder + 1;
  175. for (rel = 0; rel < numRelBorder; rel++)
  176. relBorder[rel] = 2*GetBits(bsi, 2) + 2;
  177. pBits = cLog2[sbrGrid->numEnv + 1];
  178. sbrGrid->pointer = GetBits(bsi, pBits);
  179. for (env = sbrGrid->numEnv - 1; env >= 0; env--)
  180. sbrGrid->freqRes[env] = GetBits(bsi, 1);
  181. absBordLead = 0;
  182. absBordTrail = absBorder;
  183. numRelLead = 0;
  184. numRelTrail = numRelBorder;
  185. for (rel = 0; rel < numRelTrail; rel++)
  186. relBordTrail[rel] = relBorder[rel];
  187. if (sbrGrid->pointer > 1) middleBorder = sbrGrid->numEnv + 1 - sbrGrid->pointer;
  188. else middleBorder = sbrGrid->numEnv - 1;
  189. break;
  190. case SBR_GRID_VARFIX:
  191. absBorder = GetBits(bsi, 2);
  192. numRelBorder = GetBits(bsi, 2);
  193. sbrGrid->numEnv = numRelBorder + 1;
  194. for (rel = 0; rel < numRelBorder; rel++)
  195. relBorder[rel] = 2*GetBits(bsi, 2) + 2;
  196. pBits = cLog2[sbrGrid->numEnv + 1];
  197. sbrGrid->pointer = GetBits(bsi, pBits);
  198. for (env = 0; env < sbrGrid->numEnv; env++)
  199. sbrGrid->freqRes[env] = GetBits(bsi, 1);
  200. absBordLead = absBorder;
  201. absBordTrail = NUM_TIME_SLOTS;
  202. numRelLead = numRelBorder;
  203. numRelTrail = 0;
  204. for (rel = 0; rel < numRelLead; rel++)
  205. relBordLead[rel] = relBorder[rel];
  206. if (sbrGrid->pointer == 0) middleBorder = 1;
  207. else if (sbrGrid->pointer == 1) middleBorder = sbrGrid->numEnv - 1;
  208. else middleBorder = sbrGrid->pointer - 1;
  209. break;
  210. case SBR_GRID_VARVAR:
  211. absBordLead = GetBits(bsi, 2); /* absBorder0 */
  212. absBordTrail = GetBits(bsi, 2) + NUM_TIME_SLOTS; /* absBorder1 */
  213. numRelBorder0 = GetBits(bsi, 2);
  214. numRelBorder1 = GetBits(bsi, 2);
  215. sbrGrid->numEnv = numRelBorder0 + numRelBorder1 + 1;
  216. ASSERT(sbrGrid->numEnv <= 5);
  217. for (rel = 0; rel < numRelBorder0; rel++)
  218. relBorder0[rel] = 2*GetBits(bsi, 2) + 2;
  219. for (rel = 0; rel < numRelBorder1; rel++)
  220. relBorder1[rel] = 2*GetBits(bsi, 2) + 2;
  221. pBits = cLog2[numRelBorder0 + numRelBorder1 + 2];
  222. sbrGrid->pointer = GetBits(bsi, pBits);
  223. for (env = 0; env < sbrGrid->numEnv; env++)
  224. sbrGrid->freqRes[env] = GetBits(bsi, 1);
  225. numRelLead = numRelBorder0;
  226. numRelTrail = numRelBorder1;
  227. for (rel = 0; rel < numRelLead; rel++)
  228. relBordLead[rel] = relBorder0[rel];
  229. for (rel = 0; rel < numRelTrail; rel++)
  230. relBordTrail[rel] = relBorder1[rel];
  231. if (sbrGrid->pointer > 1) middleBorder = sbrGrid->numEnv + 1 - sbrGrid->pointer;
  232. else middleBorder = sbrGrid->numEnv - 1;
  233. break;
  234. }
  235. /* build time border vector */
  236. sbrGrid->envTimeBorder[0] = absBordLead * SAMPLES_PER_SLOT;
  237. rel = 0;
  238. border = absBordLead;
  239. for (env = 1; env <= numRelLead; env++) {
  240. border += relBordLead[rel++];
  241. sbrGrid->envTimeBorder[env] = border * SAMPLES_PER_SLOT;
  242. }
  243. rel = 0;
  244. border = absBordTrail;
  245. for (env = sbrGrid->numEnv - 1; env > numRelLead; env--) {
  246. border -= relBordTrail[rel++];
  247. sbrGrid->envTimeBorder[env] = border * SAMPLES_PER_SLOT;
  248. }
  249. sbrGrid->envTimeBorder[sbrGrid->numEnv] = absBordTrail * SAMPLES_PER_SLOT;
  250. if (sbrGrid->numEnv > 1) {
  251. sbrGrid->numNoiseFloors = 2;
  252. sbrGrid->noiseTimeBorder[0] = sbrGrid->envTimeBorder[0];
  253. sbrGrid->noiseTimeBorder[1] = sbrGrid->envTimeBorder[middleBorder];
  254. sbrGrid->noiseTimeBorder[2] = sbrGrid->envTimeBorder[sbrGrid->numEnv];
  255. } else {
  256. sbrGrid->numNoiseFloors = 1;
  257. sbrGrid->noiseTimeBorder[0] = sbrGrid->envTimeBorder[0];
  258. sbrGrid->noiseTimeBorder[1] = sbrGrid->envTimeBorder[1];
  259. }
  260. }
  261. /**************************************************************************************
  262. * Function: UnpackDeltaTimeFreq
  263. *
  264. * Description: unpack time/freq flags for delta coding of SBR envelopes (table 4.63)
  265. *
  266. * Inputs: BitStreamInfo struct pointing to start of dt/df flags
  267. * number of envelopes
  268. * number of noise floors
  269. *
  270. * Outputs: delta flags for envelope and noise floors
  271. *
  272. * Return: none
  273. **************************************************************************************/
  274. static void UnpackDeltaTimeFreq(BitStreamInfo *bsi, int numEnv, unsigned char *deltaFlagEnv,
  275. int numNoiseFloors, unsigned char *deltaFlagNoise)
  276. {
  277. int env, noiseFloor;
  278. for (env = 0; env < numEnv; env++)
  279. deltaFlagEnv[env] = GetBits(bsi, 1);
  280. for (noiseFloor = 0; noiseFloor < numNoiseFloors; noiseFloor++)
  281. deltaFlagNoise[noiseFloor] = GetBits(bsi, 1);
  282. }
  283. /**************************************************************************************
  284. * Function: UnpackInverseFilterMode
  285. *
  286. * Description: unpack invf flags for chirp factor calculation (table 4.64)
  287. *
  288. * Inputs: BitStreamInfo struct pointing to start of invf flags
  289. * number of noise floor bands
  290. *
  291. * Outputs: invf flags for noise floor bands
  292. *
  293. * Return: none
  294. **************************************************************************************/
  295. static void UnpackInverseFilterMode(BitStreamInfo *bsi, int numNoiseFloorBands, unsigned char *mode)
  296. {
  297. int n;
  298. for (n = 0; n < numNoiseFloorBands; n++)
  299. mode[n] = GetBits(bsi, 2);
  300. }
  301. /**************************************************************************************
  302. * Function: UnpackSinusoids
  303. *
  304. * Description: unpack sinusoid (harmonic) flags for each SBR subband (table 4.67)
  305. *
  306. * Inputs: BitStreamInfo struct pointing to start of sinusoid flags
  307. * number of high resolution SBR subbands (nHigh)
  308. *
  309. * Outputs: sinusoid flags for each SBR subband, zero-filled above nHigh
  310. *
  311. * Return: none
  312. **************************************************************************************/
  313. static void UnpackSinusoids(BitStreamInfo *bsi, int nHigh, int addHarmonicFlag, unsigned char *addHarmonic)
  314. {
  315. int n;
  316. n = 0;
  317. if (addHarmonicFlag) {
  318. for ( ; n < nHigh; n++)
  319. addHarmonic[n] = GetBits(bsi, 1);
  320. }
  321. /* zero out unused bands */
  322. for ( ; n < MAX_QMF_BANDS; n++)
  323. addHarmonic[n] = 0;
  324. }
  325. /**************************************************************************************
  326. * Function: CopyCouplingGrid
  327. *
  328. * Description: copy grid parameters from left to right for channel coupling
  329. *
  330. * Inputs: initialized SBRGrid struct for left channel
  331. *
  332. * Outputs: initialized SBRGrid struct for right channel
  333. *
  334. * Return: none
  335. **************************************************************************************/
  336. static void CopyCouplingGrid(SBRGrid *sbrGridLeft, SBRGrid *sbrGridRight)
  337. {
  338. int env, noiseFloor;
  339. sbrGridRight->frameClass = sbrGridLeft->frameClass;
  340. sbrGridRight->ampResFrame = sbrGridLeft->ampResFrame;
  341. sbrGridRight->pointer = sbrGridLeft->pointer;
  342. sbrGridRight->numEnv = sbrGridLeft->numEnv;
  343. for (env = 0; env < sbrGridLeft->numEnv; env++) {
  344. sbrGridRight->envTimeBorder[env] = sbrGridLeft->envTimeBorder[env];
  345. sbrGridRight->freqRes[env] = sbrGridLeft->freqRes[env];
  346. }
  347. sbrGridRight->envTimeBorder[env] = sbrGridLeft->envTimeBorder[env]; /* borders are [0, numEnv] inclusive */
  348. sbrGridRight->numNoiseFloors = sbrGridLeft->numNoiseFloors;
  349. for (noiseFloor = 0; noiseFloor <= sbrGridLeft->numNoiseFloors; noiseFloor++)
  350. sbrGridRight->noiseTimeBorder[noiseFloor] = sbrGridLeft->noiseTimeBorder[noiseFloor];
  351. /* numEnvPrev, numNoiseFloorsPrev, freqResPrev are updated in DecodeSBREnvelope() and DecodeSBRNoise() */
  352. }
  353. /**************************************************************************************
  354. * Function: CopyCouplingInverseFilterMode
  355. *
  356. * Description: copy invf flags from left to right for channel coupling
  357. *
  358. * Inputs: invf flags for left channel
  359. * number of noise floor bands
  360. *
  361. * Outputs: invf flags for right channel
  362. *
  363. * Return: none
  364. **************************************************************************************/
  365. static void CopyCouplingInverseFilterMode(int numNoiseFloorBands, unsigned char *modeLeft, unsigned char *modeRight)
  366. {
  367. int band;
  368. for (band = 0; band < numNoiseFloorBands; band++)
  369. modeRight[band] = modeLeft[band];
  370. }
  371. /**************************************************************************************
  372. * Function: UnpackSBRSingleChannel
  373. *
  374. * Description: unpack sideband info (grid, delta flags, invf flags, envelope and
  375. * noise floor configuration, sinusoids) for a single channel
  376. *
  377. * Inputs: BitStreamInfo struct pointing to start of sideband info
  378. * initialized PSInfoSBR struct (after parsing SBR header and building
  379. * frequency tables)
  380. * base output channel (range = [0, nChans-1])
  381. *
  382. * Outputs: updated PSInfoSBR struct (SBRGrid and SBRChan)
  383. *
  384. * Return: none
  385. **************************************************************************************/
  386. void UnpackSBRSingleChannel(BitStreamInfo *bsi, PSInfoSBR *psi, int chBase)
  387. {
  388. int bitsLeft;
  389. SBRHeader *sbrHdr = &(psi->sbrHdr[chBase]);
  390. SBRGrid *sbrGridL = &(psi->sbrGrid[chBase+0]);
  391. SBRFreq *sbrFreq = &(psi->sbrFreq[chBase]);
  392. SBRChan *sbrChanL = &(psi->sbrChan[chBase+0]);
  393. psi->dataExtra = GetBits(bsi, 1);
  394. if (psi->dataExtra)
  395. psi->resBitsData = GetBits(bsi, 4);
  396. UnpackSBRGrid(bsi, sbrHdr, sbrGridL);
  397. UnpackDeltaTimeFreq(bsi, sbrGridL->numEnv, sbrChanL->deltaFlagEnv, sbrGridL->numNoiseFloors, sbrChanL->deltaFlagNoise);
  398. UnpackInverseFilterMode(bsi, sbrFreq->numNoiseFloorBands, sbrChanL->invfMode[1]);
  399. DecodeSBREnvelope(bsi, psi, sbrGridL, sbrFreq, sbrChanL, 0);
  400. DecodeSBRNoise(bsi, psi, sbrGridL, sbrFreq, sbrChanL, 0);
  401. sbrChanL->addHarmonicFlag[1] = GetBits(bsi, 1);
  402. UnpackSinusoids(bsi, sbrFreq->nHigh, sbrChanL->addHarmonicFlag[1], sbrChanL->addHarmonic[1]);
  403. psi->extendedDataPresent = GetBits(bsi, 1);
  404. if (psi->extendedDataPresent) {
  405. psi->extendedDataSize = GetBits(bsi, 4);
  406. if (psi->extendedDataSize == 15)
  407. psi->extendedDataSize += GetBits(bsi, 8);
  408. bitsLeft = 8 * psi->extendedDataSize;
  409. /* get ID, unpack extension info, do whatever is necessary with it... */
  410. while (bitsLeft > 0) {
  411. GetBits(bsi, 8);
  412. bitsLeft -= 8;
  413. }
  414. }
  415. }
  416. /**************************************************************************************
  417. * Function: UnpackSBRChannelPair
  418. *
  419. * Description: unpack sideband info (grid, delta flags, invf flags, envelope and
  420. * noise floor configuration, sinusoids) for a channel pair
  421. *
  422. * Inputs: BitStreamInfo struct pointing to start of sideband info
  423. * initialized PSInfoSBR struct (after parsing SBR header and building
  424. * frequency tables)
  425. * base output channel (range = [0, nChans-1])
  426. *
  427. * Outputs: updated PSInfoSBR struct (SBRGrid and SBRChan for both channels)
  428. *
  429. * Return: none
  430. **************************************************************************************/
  431. void UnpackSBRChannelPair(BitStreamInfo *bsi, PSInfoSBR *psi, int chBase)
  432. {
  433. int bitsLeft;
  434. SBRHeader *sbrHdr = &(psi->sbrHdr[chBase]);
  435. SBRGrid *sbrGridL = &(psi->sbrGrid[chBase+0]), *sbrGridR = &(psi->sbrGrid[chBase+1]);
  436. SBRFreq *sbrFreq = &(psi->sbrFreq[chBase]);
  437. SBRChan *sbrChanL = &(psi->sbrChan[chBase+0]), *sbrChanR = &(psi->sbrChan[chBase+1]);
  438. psi->dataExtra = GetBits(bsi, 1);
  439. if (psi->dataExtra) {
  440. psi->resBitsData = GetBits(bsi, 4);
  441. psi->resBitsData = GetBits(bsi, 4);
  442. }
  443. psi->couplingFlag = GetBits(bsi, 1);
  444. if (psi->couplingFlag) {
  445. UnpackSBRGrid(bsi, sbrHdr, sbrGridL);
  446. CopyCouplingGrid(sbrGridL, sbrGridR);
  447. UnpackDeltaTimeFreq(bsi, sbrGridL->numEnv, sbrChanL->deltaFlagEnv, sbrGridL->numNoiseFloors, sbrChanL->deltaFlagNoise);
  448. UnpackDeltaTimeFreq(bsi, sbrGridR->numEnv, sbrChanR->deltaFlagEnv, sbrGridR->numNoiseFloors, sbrChanR->deltaFlagNoise);
  449. UnpackInverseFilterMode(bsi, sbrFreq->numNoiseFloorBands, sbrChanL->invfMode[1]);
  450. CopyCouplingInverseFilterMode(sbrFreq->numNoiseFloorBands, sbrChanL->invfMode[1], sbrChanR->invfMode[1]);
  451. DecodeSBREnvelope(bsi, psi, sbrGridL, sbrFreq, sbrChanL, 0);
  452. DecodeSBRNoise(bsi, psi, sbrGridL, sbrFreq, sbrChanL, 0);
  453. DecodeSBREnvelope(bsi, psi, sbrGridR, sbrFreq, sbrChanR, 1);
  454. DecodeSBRNoise(bsi, psi, sbrGridR, sbrFreq, sbrChanR, 1);
  455. /* pass RIGHT sbrChan struct */
  456. UncoupleSBREnvelope(psi, sbrGridL, sbrFreq, sbrChanR);
  457. UncoupleSBRNoise(psi, sbrGridL, sbrFreq, sbrChanR);
  458. } else {
  459. UnpackSBRGrid(bsi, sbrHdr, sbrGridL);
  460. UnpackSBRGrid(bsi, sbrHdr, sbrGridR);
  461. UnpackDeltaTimeFreq(bsi, sbrGridL->numEnv, sbrChanL->deltaFlagEnv, sbrGridL->numNoiseFloors, sbrChanL->deltaFlagNoise);
  462. UnpackDeltaTimeFreq(bsi, sbrGridR->numEnv, sbrChanR->deltaFlagEnv, sbrGridR->numNoiseFloors, sbrChanR->deltaFlagNoise);
  463. UnpackInverseFilterMode(bsi, sbrFreq->numNoiseFloorBands, sbrChanL->invfMode[1]);
  464. UnpackInverseFilterMode(bsi, sbrFreq->numNoiseFloorBands, sbrChanR->invfMode[1]);
  465. DecodeSBREnvelope(bsi, psi, sbrGridL, sbrFreq, sbrChanL, 0);
  466. DecodeSBREnvelope(bsi, psi, sbrGridR, sbrFreq, sbrChanR, 1);
  467. DecodeSBRNoise(bsi, psi, sbrGridL, sbrFreq, sbrChanL, 0);
  468. DecodeSBRNoise(bsi, psi, sbrGridR, sbrFreq, sbrChanR, 1);
  469. }
  470. sbrChanL->addHarmonicFlag[1] = GetBits(bsi, 1);
  471. UnpackSinusoids(bsi, sbrFreq->nHigh, sbrChanL->addHarmonicFlag[1], sbrChanL->addHarmonic[1]);
  472. sbrChanR->addHarmonicFlag[1] = GetBits(bsi, 1);
  473. UnpackSinusoids(bsi, sbrFreq->nHigh, sbrChanR->addHarmonicFlag[1], sbrChanR->addHarmonic[1]);
  474. psi->extendedDataPresent = GetBits(bsi, 1);
  475. if (psi->extendedDataPresent) {
  476. psi->extendedDataSize = GetBits(bsi, 4);
  477. if (psi->extendedDataSize == 15)
  478. psi->extendedDataSize += GetBits(bsi, 8);
  479. bitsLeft = 8 * psi->extendedDataSize;
  480. /* get ID, unpack extension info, do whatever is necessary with it... */
  481. while (bitsLeft > 0) {
  482. GetBits(bsi, 8);
  483. bitsLeft -= 8;
  484. }
  485. }
  486. }