/ src / dequant.cpp
dequant.cpp
  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  /**************************************************************************************
 37   * Fixed-point MP3 decoder
 38   * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
 39   * June 2003
 40   *
 41   * dequant.c - dequantization, stereo processing (intensity, mid-side), short-block
 42   *               coefficient reordering
 43   **************************************************************************************/
 44  
 45  #include "coder.h"
 46  #include "assembly.h"
 47  
 48  /**************************************************************************************
 49   * Function:    Dequantize
 50   *
 51   * Description: dequantize coefficients, decode stereo, reorder short blocks
 52   *                (one granule-worth)
 53   *
 54   * Inputs:      MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
 55   *                UnpackScaleFactors(), and DecodeHuffman() (for this granule)
 56   *              index of current granule
 57   *
 58   * Outputs:     dequantized and reordered coefficients in hi->huffDecBuf 
 59   *                (one granule-worth, all channels), format = Q26
 60   *              operates in-place on huffDecBuf but also needs di->workBuf
 61   *              updated hi->nonZeroBound index for both channels
 62   *
 63   * Return:      0 on success, -1 if null input pointers
 64   *
 65   * Notes:       In calling output Q(DQ_FRACBITS_OUT), we assume an implicit bias 
 66   *                of 2^15. Some (floating-point) reference implementations factor this 
 67   *                into the 2^(0.25 * gain) scaling explicitly. But to avoid precision 
 68   *                loss, we don't do that. Instead take it into account in the final 
 69   *                round to PCM (>> by 15 less than we otherwise would have).
 70   *              Equivalently, we can think of the dequantized coefficients as 
 71   *                Q(DQ_FRACBITS_OUT - 15) with no implicit bias. 
 72   **************************************************************************************/
 73  int Dequantize(MP3DecInfo *mp3DecInfo, int gr)
 74  {
 75  	int i, ch, nSamps, mOut[2];
 76  	FrameHeader *fh;
 77  	SideInfo *si;
 78  	ScaleFactorInfo *sfi;
 79  	HuffmanInfo *hi;
 80  	DequantInfo *di;
 81  	CriticalBandInfo *cbi;
 82  
 83  	/* validate pointers */
 84  	if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS || !mp3DecInfo->ScaleFactorInfoPS || 
 85  		!mp3DecInfo->HuffmanInfoPS || !mp3DecInfo->DequantInfoPS)
 86  		return -1;
 87  
 88  	fh = (FrameHeader *)(mp3DecInfo->FrameHeaderPS);
 89  
 90  	/* si is an array of up to 4 structs, stored as gr0ch0, gr0ch1, gr1ch0, gr1ch1 */
 91  	si = (SideInfo *)(mp3DecInfo->SideInfoPS);
 92  	sfi = (ScaleFactorInfo *)(mp3DecInfo->ScaleFactorInfoPS);
 93  	hi = (HuffmanInfo *)mp3DecInfo->HuffmanInfoPS;
 94  	di = (DequantInfo *)mp3DecInfo->DequantInfoPS;
 95  	cbi = di->cbi;
 96  	mOut[0] = mOut[1] = 0;
 97  
 98  	/* dequantize all the samples in each channel */
 99  	for (ch = 0; ch < mp3DecInfo->nChans; ch++) {
100  		hi->gb[ch] = DequantChannel(hi->huffDecBuf[ch], di->workBuf, &hi->nonZeroBound[ch], fh, 
101  			&si->sis[gr][ch], &sfi->sfis[gr][ch], &cbi[ch]);
102  	}
103  
104  	/* joint stereo processing assumes one guard bit in input samples
105  	 * it's extremely rare not to have at least one gb, so if this is the case
106  	 *   just make a pass over the data and clip to [-2^30+1, 2^30-1]
107  	 * in practice this may never happen
108  	 */
109  	if (fh->modeExt && (hi->gb[0] < 1 || hi->gb[1] < 1)) {
110  		for (i = 0; i < hi->nonZeroBound[0]; i++) {
111  			if (hi->huffDecBuf[0][i] < -0x3fffffff)	 hi->huffDecBuf[0][i] = -0x3fffffff;
112  			if (hi->huffDecBuf[0][i] >  0x3fffffff)	 hi->huffDecBuf[0][i] =  0x3fffffff;
113  		}
114  		for (i = 0; i < hi->nonZeroBound[1]; i++) {
115  			if (hi->huffDecBuf[1][i] < -0x3fffffff)	 hi->huffDecBuf[1][i] = -0x3fffffff;
116  			if (hi->huffDecBuf[1][i] >  0x3fffffff)	 hi->huffDecBuf[1][i] =  0x3fffffff;
117  		}
118  	}
119  
120  	/* do mid-side stereo processing, if enabled */
121  	if (fh->modeExt >> 1) {
122  		if (fh->modeExt & 0x01) {
123  			/* intensity stereo enabled - run mid-side up to start of right zero region */
124  			if (cbi[1].cbType == 0)
125  				nSamps = fh->sfBand->l[cbi[1].cbEndL + 1];
126  			else 
127  				nSamps = 3 * fh->sfBand->s[cbi[1].cbEndSMax + 1];
128  		} else {
129  			/* intensity stereo disabled - run mid-side on whole spectrum */
130  			nSamps = MAX(hi->nonZeroBound[0], hi->nonZeroBound[1]);
131  		}
132  		MidSideProc(hi->huffDecBuf, nSamps, mOut);
133  	}
134  
135  	/* do intensity stereo processing, if enabled */
136  	if (fh->modeExt & 0x01) {
137  		nSamps = hi->nonZeroBound[0];
138  		if (fh->ver == MPEG1) {
139  			IntensityProcMPEG1(hi->huffDecBuf, nSamps, fh, &sfi->sfis[gr][1], di->cbi, 
140  				fh->modeExt >> 1, si->sis[gr][1].mixedBlock, mOut);
141  		} else {
142  			IntensityProcMPEG2(hi->huffDecBuf, nSamps, fh, &sfi->sfis[gr][1], di->cbi, &sfi->sfjs,
143  				fh->modeExt >> 1, si->sis[gr][1].mixedBlock, mOut);
144  		}
145  	}
146  
147  	/* adjust guard bit count and nonZeroBound if we did any stereo processing */
148  	if (fh->modeExt) {
149  		hi->gb[0] = CLZ(mOut[0]) - 1;
150  		hi->gb[1] = CLZ(mOut[1]) - 1;
151  		nSamps = MAX(hi->nonZeroBound[0], hi->nonZeroBound[1]);
152  		hi->nonZeroBound[0] = nSamps;
153  		hi->nonZeroBound[1] = nSamps;
154  	}
155  
156  	/* output format Q(DQ_FRACBITS_OUT) */
157  	return 0;
158  }