FastLED 3.9.15
Loading...
Searching...
No Matches
dequant.hpp
Go to the documentation of this file.
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 "fl/stl/stdint.h"
47#include "fl/stl/noexcept.h"
48#include "assembly.h"
49
50namespace fl {
51namespace third_party {
52
53/**************************************************************************************
54 * Function: Dequantize
55 *
56 * Description: dequantize coefficients, decode stereo, reorder short blocks
57 * (one granule-worth)
58 *
59 * Inputs: MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
60 * UnpackScaleFactors(), and DecodeHuffman() (for this granule)
61 * index of current granule
62 *
63 * Outputs: dequantized and reordered coefficients in hi->huffDecBuf
64 * (one granule-worth, all channels), format = Q26
65 * operates in-place on huffDecBuf but also needs di->workBuf
66 * updated hi->nonZeroBound index for both channels
67 *
68 * Return: 0 on success, -1 if null input pointers
69 *
70 * Notes: In calling output Q(DQ_FRACBITS_OUT), we assume an implicit bias
71 * of 2^15. Some (floating-point) reference implementations factor this
72 * into the 2^(0.25 * gain) scaling explicitly. But to avoid precision
73 * loss, we don't do that. Instead take it into account in the final
74 * round to PCM (>> by 15 less than we otherwise would have).
75 * Equivalently, we can think of the dequantized coefficients as
76 * Q(DQ_FRACBITS_OUT - 15) with no implicit bias.
77 **************************************************************************************/
78int Dequantize(MP3DecInfo *mp3DecInfo, int gr) FL_NOEXCEPT
79{
80 int i, ch, nSamps;
81 int32_t mOut[2];
82 FrameHeader *fh;
83 SideInfo *si;
84 ScaleFactorInfo *sfi;
85 HuffmanInfo *hi;
86 DequantInfo *di;
88
89 /* validate pointers */
90 if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS || !mp3DecInfo->ScaleFactorInfoPS ||
91 !mp3DecInfo->HuffmanInfoPS || !mp3DecInfo->DequantInfoPS)
92 return -1;
93
94 fh = (FrameHeader *)(mp3DecInfo->FrameHeaderPS);
95
96 /* si is an array of up to 4 structs, stored as gr0ch0, gr0ch1, gr1ch0, gr1ch1 */
97 si = (SideInfo *)(mp3DecInfo->SideInfoPS);
98 sfi = (ScaleFactorInfo *)(mp3DecInfo->ScaleFactorInfoPS);
99 hi = (HuffmanInfo *)mp3DecInfo->HuffmanInfoPS;
100 di = (DequantInfo *)mp3DecInfo->DequantInfoPS;
101 cbi = di->cbi;
102 mOut[0] = mOut[1] = 0;
103
104 /* dequantize all the samples in each channel */
105 for (ch = 0; ch < mp3DecInfo->nChans; ch++) {
106 hi->gb[ch] = DequantChannel(hi->huffDecBuf[ch], di->workBuf, &hi->nonZeroBound[ch], fh,
107 &si->sis[gr][ch], &sfi->sfis[gr][ch], &cbi[ch]);
108 }
109
110 /* joint stereo processing assumes one guard bit in input samples
111 * it's extremely rare not to have at least one gb, so if this is the case
112 * just make a pass over the data and clip to [-2^30+1, 2^30-1]
113 * in practice this may never happen
114 */
115 if (fh->modeExt && (hi->gb[0] < 1 || hi->gb[1] < 1)) {
116 /* Clip to range [-2^30+1, 2^30-1] */
117 const int32_t max_clip = (int32_t)0x3FFFFFFFL; /* 2^30 - 1 */
118 const int32_t min_clip = (int32_t)(-0x40000000L); /* -(2^30) */
119 for (i = 0; i < hi->nonZeroBound[0]; i++) {
120 if (hi->huffDecBuf[0][i] < min_clip) hi->huffDecBuf[0][i] = min_clip;
121 if (hi->huffDecBuf[0][i] > max_clip) hi->huffDecBuf[0][i] = max_clip;
122 }
123 for (i = 0; i < hi->nonZeroBound[1]; i++) {
124 if (hi->huffDecBuf[1][i] < min_clip) hi->huffDecBuf[1][i] = min_clip;
125 if (hi->huffDecBuf[1][i] > max_clip) hi->huffDecBuf[1][i] = max_clip;
126 }
127 }
128
129 /* do mid-side stereo processing, if enabled */
130 if (fh->modeExt >> 1) {
131 if (fh->modeExt & 0x01) {
132 /* intensity stereo enabled - run mid-side up to start of right zero region */
133 if (cbi[1].cbType == 0)
134 nSamps = fh->sfBand->l[cbi[1].cbEndL + 1];
135 else
136 nSamps = 3 * fh->sfBand->s[cbi[1].cbEndSMax + 1];
137 } else {
138 /* intensity stereo disabled - run mid-side on whole spectrum */
139 nSamps = MAX(hi->nonZeroBound[0], hi->nonZeroBound[1]);
140 }
141 MidSideProc(hi->huffDecBuf, nSamps, mOut);
142 }
143
144 /* do intensity stereo processing, if enabled */
145 if (fh->modeExt & 0x01) {
146 nSamps = hi->nonZeroBound[0];
147 if (fh->ver == MPEG1) {
148 IntensityProcMPEG1(hi->huffDecBuf, nSamps, fh, &sfi->sfis[gr][1], di->cbi,
149 fh->modeExt >> 1, si->sis[gr][1].mixedBlock, mOut);
150 } else {
151 IntensityProcMPEG2(hi->huffDecBuf, nSamps, fh, &sfi->sfis[gr][1], di->cbi, &sfi->sfjs,
152 fh->modeExt >> 1, si->sis[gr][1].mixedBlock, mOut);
153 }
154 }
155
156 /* adjust guard bit count and nonZeroBound if we did any stereo processing */
157 if (fh->modeExt) {
158 hi->gb[0] = CLZ(mOut[0]) - 1;
159 hi->gb[1] = CLZ(mOut[1]) - 1;
160 nSamps = MAX(hi->nonZeroBound[0], hi->nonZeroBound[1]);
161 hi->nonZeroBound[0] = nSamps;
162 hi->nonZeroBound[1] = nSamps;
163 }
164
165 /* output format Q(DQ_FRACBITS_OUT) */
166 return 0;
167}
168
169} // namespace third_party
170} // namespace fl
#define MAX(a, b)
Definition coder.h:60
struct _MP3DecInfo MP3DecInfo
@ MPEG1
Definition mp3dec.h:83
int Dequantize(MP3DecInfo *mp3DecInfo, int gr) FL_NOEXCEPT
Definition dequant.hpp:78
void MidSideProc(int32_t x[MAX_NCHAN][MAX_NSAMP], int32_t nSamps, int32_t mOut[2]) FL_NOEXCEPT
Definition stproc.hpp:71
struct fl::third_party::_DequantInfo DequantInfo
struct fl::third_party::_HuffmanInfo HuffmanInfo
struct fl::third_party::_FrameHeader FrameHeader
void IntensityProcMPEG1(int32_t x[MAX_NCHAN][MAX_NSAMP], int32_t nSamps, FrameHeader *fh, ScaleFactorInfoSub *sfis, CriticalBandInfo *cbi, int32_t midSideFlag, int32_t mixFlag, int32_t mOut[2]) FL_NOEXCEPT
Definition stproc.hpp:113
struct fl::third_party::_SideInfo SideInfo
void IntensityProcMPEG2(int32_t x[MAX_NCHAN][MAX_NSAMP], int32_t nSamps, FrameHeader *fh, ScaleFactorInfoSub *sfis, CriticalBandInfo *cbi, ScaleFactorJS *sfjs, int32_t midSideFlag, int32_t mixFlag, int32_t mOut[2]) FL_NOEXCEPT
Definition stproc.hpp:220
fl::i32 int32_t
Definition coder.h:220
__inline int32_t CLZ(int32_t x) FL_NOEXCEPT
Leading zeros.
Definition assembly.h:527
struct fl::third_party::_ScaleFactorInfo ScaleFactorInfo
int32_t DequantChannel(int32_t *sampleBuf, int32_t *workBuf, int32_t *nonZeroBound, FrameHeader *fh, SideInfoSub *sis, ScaleFactorInfoSub *sfis, CriticalBandInfo *cbi) FL_NOEXCEPT
Definition dqchan.hpp:252
ScaleFactorInfoSub sfis[MAX_NGRAN][MAX_NCHAN]
Definition coder.h:270
CriticalBandInfo cbi[MAX_NCHAN]
Definition coder.h:212
int32_t workBuf[MAX_REORDER_SAMPS]
Definition coder.h:211
int32_t nonZeroBound[MAX_NCHAN]
Definition coder.h:217
int32_t gb[MAX_NCHAN]
Definition coder.h:218
int32_t huffDecBuf[MAX_NCHAN][MAX_NSAMP]
Definition coder.h:216
const SFBandTable * sfBand
Definition coder.h:175
SideInfoSub sis[MAX_NGRAN][MAX_NCHAN]
Definition coder.h:200
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT