dequant.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: RCSL 1.0/RPSL 1.0
  3. *
  4. * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file, are
  7. * subject to the current version of the RealNetworks Public Source License
  8. * Version 1.0 (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the RealNetworks Community Source License Version 1.0
  11. * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
  12. * in which case the RCSL will apply. You may also obtain the license terms
  13. * directly from RealNetworks. You may not use this file except in
  14. * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
  15. * applicable to this file, the RCSL. Please see the applicable RPSL or
  16. * RCSL for the rights, obligations and limitations governing use of the
  17. * contents of the file.
  18. *
  19. * This file is part of the Helix DNA Technology. RealNetworks is the
  20. * developer of the Original Code and owns the copyrights in the portions
  21. * it created.
  22. *
  23. * This file, and the files included with this file, is distributed and made
  24. * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  25. * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  26. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
  27. * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  28. *
  29. * Technology Compatibility Kit Test Suite(s) Location:
  30. * http://www.helixcommunity.org/content/tck
  31. *
  32. * Contributor(s):
  33. *
  34. * ***** END LICENSE BLOCK ***** */
  35. /**************************************************************************************
  36. * Fixed-point MP3 decoder
  37. * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
  38. * June 2003
  39. *
  40. * dequant.c - dequantization, stereo processing (intensity, mid-side), short-block
  41. * coefficient reordering
  42. **************************************************************************************/
  43. #include "coder.h"
  44. #include "assembly.h"
  45. /**************************************************************************************
  46. * Function: Dequantize
  47. *
  48. * Description: dequantize coefficients, decode stereo, reorder short blocks
  49. * (one granule-worth)
  50. *
  51. * Inputs: MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
  52. * UnpackScaleFactors(), and DecodeHuffman() (for this granule)
  53. * index of current granule
  54. *
  55. * Outputs: dequantized and reordered coefficients in hi->huffDecBuf
  56. * (one granule-worth, all channels), format = Q26
  57. * operates in-place on huffDecBuf but also needs di->workBuf
  58. * updated hi->nonZeroBound index for both channels
  59. *
  60. * Return: 0 on success, -1 if null input pointers
  61. *
  62. * Notes: In calling output Q(DQ_FRACBITS_OUT), we assume an implicit bias
  63. * of 2^15. Some (floating-point) reference implementations factor this
  64. * into the 2^(0.25 * gain) scaling explicitly. But to avoid precision
  65. * loss, we don't do that. Instead take it into account in the final
  66. * round to PCM (>> by 15 less than we otherwise would have).
  67. * Equivalently, we can think of the dequantized coefficients as
  68. * Q(DQ_FRACBITS_OUT - 15) with no implicit bias.
  69. **************************************************************************************/
  70. int Dequantize(MP3DecInfo *mp3DecInfo, int gr)
  71. {
  72. int i, ch, nSamps, mOut[2];
  73. FrameHeader *fh;
  74. SideInfo *si;
  75. ScaleFactorInfo *sfi;
  76. HuffmanInfo *hi;
  77. DequantInfo *di;
  78. CriticalBandInfo *cbi;
  79. /* validate pointers */
  80. if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS || !mp3DecInfo->ScaleFactorInfoPS ||
  81. !mp3DecInfo->HuffmanInfoPS || !mp3DecInfo->DequantInfoPS)
  82. return -1;
  83. fh = (FrameHeader *)(mp3DecInfo->FrameHeaderPS);
  84. /* si is an array of up to 4 structs, stored as gr0ch0, gr0ch1, gr1ch0, gr1ch1 */
  85. si = (SideInfo *)(mp3DecInfo->SideInfoPS);
  86. sfi = (ScaleFactorInfo *)(mp3DecInfo->ScaleFactorInfoPS);
  87. hi = (HuffmanInfo *)mp3DecInfo->HuffmanInfoPS;
  88. di = (DequantInfo *)mp3DecInfo->DequantInfoPS;
  89. cbi = di->cbi;
  90. mOut[0] = mOut[1] = 0;
  91. /* dequantize all the samples in each channel */
  92. for (ch = 0; ch < mp3DecInfo->nChans; ch++) {
  93. hi->gb[ch] = DequantChannel(hi->huffDecBuf[ch], di->workBuf, &hi->nonZeroBound[ch], fh,
  94. &si->sis[gr][ch], &sfi->sfis[gr][ch], &cbi[ch]);
  95. }
  96. /* joint stereo processing assumes one guard bit in input samples
  97. * it's extremely rare not to have at least one gb, so if this is the case
  98. * just make a pass over the data and clip to [-2^30+1, 2^30-1]
  99. * in practice this may never happen
  100. */
  101. if (fh->modeExt && (hi->gb[0] < 1 || hi->gb[1] < 1)) {
  102. for (i = 0; i < hi->nonZeroBound[0]; i++) {
  103. if (hi->huffDecBuf[0][i] < -0x3fffffff) hi->huffDecBuf[0][i] = -0x3fffffff;
  104. if (hi->huffDecBuf[0][i] > 0x3fffffff) hi->huffDecBuf[0][i] = 0x3fffffff;
  105. }
  106. for (i = 0; i < hi->nonZeroBound[1]; i++) {
  107. if (hi->huffDecBuf[1][i] < -0x3fffffff) hi->huffDecBuf[1][i] = -0x3fffffff;
  108. if (hi->huffDecBuf[1][i] > 0x3fffffff) hi->huffDecBuf[1][i] = 0x3fffffff;
  109. }
  110. }
  111. /* do mid-side stereo processing, if enabled */
  112. if (fh->modeExt >> 1) {
  113. if (fh->modeExt & 0x01) {
  114. /* intensity stereo enabled - run mid-side up to start of right zero region */
  115. if (cbi[1].cbType == 0)
  116. nSamps = fh->sfBand->l[cbi[1].cbEndL + 1];
  117. else
  118. nSamps = 3 * fh->sfBand->s[cbi[1].cbEndSMax + 1];
  119. } else {
  120. /* intensity stereo disabled - run mid-side on whole spectrum */
  121. nSamps = MAX(hi->nonZeroBound[0], hi->nonZeroBound[1]);
  122. }
  123. MidSideProc(hi->huffDecBuf, nSamps, mOut);
  124. }
  125. /* do intensity stereo processing, if enabled */
  126. if (fh->modeExt & 0x01) {
  127. nSamps = hi->nonZeroBound[0];
  128. if (fh->ver == MPEG1) {
  129. IntensityProcMPEG1(hi->huffDecBuf, nSamps, fh, &sfi->sfis[gr][1], di->cbi,
  130. fh->modeExt >> 1, si->sis[gr][1].mixedBlock, mOut);
  131. } else {
  132. IntensityProcMPEG2(hi->huffDecBuf, nSamps, fh, &sfi->sfis[gr][1], di->cbi, &sfi->sfjs,
  133. fh->modeExt >> 1, si->sis[gr][1].mixedBlock, mOut);
  134. }
  135. }
  136. /* adjust guard bit count and nonZeroBound if we did any stereo processing */
  137. if (fh->modeExt) {
  138. hi->gb[0] = CLZ(mOut[0]) - 1;
  139. hi->gb[1] = CLZ(mOut[1]) - 1;
  140. nSamps = MAX(hi->nonZeroBound[0], hi->nonZeroBound[1]);
  141. hi->nonZeroBound[0] = nSamps;
  142. hi->nonZeroBound[1] = nSamps;
  143. }
  144. /* output format Q(DQ_FRACBITS_OUT) */
  145. return 0;
  146. }