FastLED 3.9.15
Loading...
Searching...
No Matches
bitstream.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 * bitstream.c - bitstream unpacking, frame header parsing, side info parsing
42 **************************************************************************************/
43
44#include "coder.h"
45#include "fl/stl/stdint.h"
46#include "fl/stl/noexcept.h"
47#include "assembly.h"
48
49namespace fl {
50namespace third_party {
51
52/**************************************************************************************
53 * Function: SetBitstreamPointer
54 *
55 * Description: initialize bitstream reader
56 *
57 * Inputs: pointer to BitStreamInfo struct
58 * number of bytes in bitstream
59 * pointer to byte-aligned buffer of data to read from
60 *
61 * Outputs: filled bitstream info struct
62 *
63 * Return: none
64 **************************************************************************************/
65void SetBitstreamPointer(BitStreamInfo *bsi, int32_t nBytes, const unsigned char *buf) FL_NOEXCEPT
66{
67 /* init bitstream */
68 bsi->bytePtr = buf;
69 bsi->iCache = 0; /* 4-byte unsigned int */
70 bsi->cachedBits = 0; /* i.e. zero bits in cache */
71 bsi->nBytes = nBytes;
72}
73
74/**************************************************************************************
75 * Function: RefillBitstreamCache
76 *
77 * Description: read new data from bitstream buffer into bsi cache
78 *
79 * Inputs: pointer to initialized BitStreamInfo struct
80 *
81 * Outputs: updated bitstream info struct
82 *
83 * Return: none
84 *
85 * Notes: only call when iCache is completely drained (resets bitOffset to 0)
86 * always loads 4 new bytes except when bsi->nBytes < 4 (end of buffer)
87 * stores data as big-endian in cache, regardless of machine endian-ness
88 *
89 * TODO: optimize for ARM
90 * possibly add little/big-endian modes for doing 32-bit loads
91 **************************************************************************************/
93{
94 int32_t nBytes = bsi->nBytes;
95
96 /* optimize for common case, independent of machine endian-ness */
97 if (nBytes >= 4) {
98 bsi->iCache = ((uint32_t)(*bsi->bytePtr++)) << 24;
99 bsi->iCache |= ((uint32_t)(*bsi->bytePtr++)) << 16;
100 bsi->iCache |= ((uint32_t)(*bsi->bytePtr++)) << 8;
101 bsi->iCache |= ((uint32_t)(*bsi->bytePtr++));
102 bsi->cachedBits = 32;
103 bsi->nBytes -= 4;
104 } else {
105 bsi->iCache = 0;
106 while (nBytes--) {
107 bsi->iCache |= (*bsi->bytePtr++);
108 bsi->iCache <<= 8;
109 }
110 bsi->iCache <<= ((3 - bsi->nBytes)*8);
111 bsi->cachedBits = 8*bsi->nBytes;
112 bsi->nBytes = 0;
113 }
114}
115
116/**************************************************************************************
117 * Function: GetBits
118 *
119 * Description: get bits from bitstream, advance bitstream pointer
120 *
121 * Inputs: pointer to initialized BitStreamInfo struct
122 * number of bits to get from bitstream
123 *
124 * Outputs: updated bitstream info struct
125 *
126 * Return: the next nBits bits of data from bitstream buffer
127 *
128 * Notes: nBits must be in range [0, 31], nBits outside this range masked by 0x1f
129 * for speed, does not indicate error if you overrun bit buffer
130 * if nBits = 0, returns 0 (useful for scalefactor unpacking)
131 *
132 * TODO: optimize for ARM
133 **************************************************************************************/
135{
136 uint32_t data, lowBits;
137
138 nBits &= 0x1f; /* nBits mod 32 to avoid unpredictable results like >> by negative amount */
139 data = bsi->iCache >> (31 - nBits); /* unsigned >> so zero-extend */
140 data >>= 1; /* do as >> 31, >> 1 so that nBits = 0 works okay (returns 0) */
141 bsi->iCache <<= nBits; /* left-justify cache */
142 bsi->cachedBits -= nBits; /* how many bits have we drawn from the cache so far */
143
144 /* if we cross an int boundary, refill the cache */
145 if (bsi->cachedBits < 0) {
146 lowBits = -bsi->cachedBits;
148 data |= bsi->iCache >> (32 - lowBits); /* get the low-order bits */
149
150 bsi->cachedBits -= lowBits; /* how many bits have we drawn from the cache so far */
151 bsi->iCache <<= lowBits; /* left-justify cache */
152 }
153
154 return data;
155}
156
157/**************************************************************************************
158 * Function: CalcBitsUsed
159 *
160 * Description: calculate how many bits have been read from bitstream
161 *
162 * Inputs: pointer to initialized BitStreamInfo struct
163 * pointer to start of bitstream buffer
164 * bit offset into first byte of startBuf (0-7)
165 *
166 * Outputs: none
167 *
168 * Return: number of bits read from bitstream, as offset from startBuf:startOffset
169 **************************************************************************************/
170int32_t CalcBitsUsed(BitStreamInfo *bsi, const unsigned char *startBuf, int32_t startOffset) FL_NOEXCEPT
171{
172 int32_t bitsUsed;
173
174 bitsUsed = (bsi->bytePtr - startBuf) * 8;
175 bitsUsed -= bsi->cachedBits;
176 bitsUsed -= startOffset;
177
178 return bitsUsed;
179}
180
181/**************************************************************************************
182 * Function: CheckPadBit
183 *
184 * Description: check whether padding byte is present in an MP3 frame
185 *
186 * Inputs: MP3DecInfo struct with valid FrameHeader struct
187 * (filled by UnpackFrameHeader())
188 *
189 * Outputs: none
190 *
191 * Return: 1 if pad bit is set, 0 if not, -1 if null input pointer
192 **************************************************************************************/
194{
195 FrameHeader *fh;
196
197 /* validate pointers */
198 if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS)
199 return -1;
200
201 fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
202
203 return (fh->paddingBit ? 1 : 0);
204}
205
206/**************************************************************************************
207 * Function: UnpackFrameHeader
208 *
209 * Description: parse the fields of the MP3 frame header
210 *
211 * Inputs: buffer pointing to a complete MP3 frame header (4 bytes, plus 2 if CRC)
212 *
213 * Outputs: filled frame header info in the MP3DecInfo structure
214 * updated platform-specific FrameHeader struct
215 *
216 * Return: length (in bytes) of frame header (for caller to calculate offset to
217 * first byte following frame header)
218 * -1 if null frameHeader or invalid header
219 *
220 * TODO: check for valid modes, depending on capabilities of decoder
221 * test CRC on actual stream (verify no endian problems)
222 **************************************************************************************/
223int UnpackFrameHeader(MP3DecInfo *mp3DecInfo, const unsigned char *buf) FL_NOEXCEPT
224{
225
226 int verIdx;
227 FrameHeader *fh;
228
229 /* validate pointers and sync word */
230 if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || (buf[0] & SYNCWORDH) != SYNCWORDH || (buf[1] & SYNCWORDL) != SYNCWORDL)
231 return -1;
232
233 fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
234
235 /* read header fields - use bitmasks instead of GetBits() for speed, since format never varies */
236 verIdx = (buf[1] >> 3) & 0x03;
237 fh->ver = (MPEGVersion)( verIdx == 0 ? MPEG25 : ((verIdx & 0x01) ? MPEG1 : MPEG2) );
238 fh->layer = 4 - ((buf[1] >> 1) & 0x03); /* easy mapping of index to layer number, 4 = error */
239 fh->crc = 1 - ((buf[1] >> 0) & 0x01);
240 fh->brIdx = (buf[2] >> 4) & 0x0f;
241 fh->srIdx = (buf[2] >> 2) & 0x03;
242 fh->paddingBit = (buf[2] >> 1) & 0x01;
243 fh->privateBit = (buf[2] >> 0) & 0x01;
244 fh->sMode = (StereoMode)((buf[3] >> 6) & 0x03); /* maps to correct enum (see definition) */
245 fh->modeExt = (buf[3] >> 4) & 0x03;
246 fh->copyFlag = (buf[3] >> 3) & 0x01;
247 fh->origFlag = (buf[3] >> 2) & 0x01;
248 fh->emphasis = (buf[3] >> 0) & 0x03;
249
250 /* check parameters to avoid indexing tables with bad values */
251 if (fh->srIdx == 3 || fh->layer == 4 || fh->brIdx == 15)
252 return -1;
253
254 fh->sfBand = &sfBandTable[fh->ver][fh->srIdx]; /* for readability (we reference sfBandTable many times in decoder) */
255 if (fh->sMode != Joint) /* just to be safe (dequant, stproc check fh->modeExt) */
256 fh->modeExt = 0;
257
258 /* init user-accessible data */
259 mp3DecInfo->nChans = (fh->sMode == Mono ? 1 : 2);
260 mp3DecInfo->samprate = samplerateTab[fh->ver][fh->srIdx];
261 mp3DecInfo->nGrans = (fh->ver == MPEG1 ? NGRANS_MPEG1 : NGRANS_MPEG2);
262 mp3DecInfo->nGranSamps = ((int)samplesPerFrameTab[fh->ver][fh->layer - 1]) / mp3DecInfo->nGrans;
263 mp3DecInfo->layer = fh->layer;
264 mp3DecInfo->version = fh->ver;
265
266 /* get bitrate and nSlots from table, unless brIdx == 0 (free mode) in which case caller must figure it out himself
267 * question - do we want to overwrite mp3DecInfo->bitrate with 0 each time if it's free mode, and
268 * copy the pre-calculated actual free bitrate into it in mp3dec.c (according to the spec,
269 * this shouldn't be necessary, since it should be either all frames free or none free)
270 */
271 if (fh->brIdx) {
272 mp3DecInfo->bitrate = ((int)bitrateTab[fh->ver][fh->layer - 1][fh->brIdx]) * 1000;
273
274 /* nSlots = total frame bytes (from table) - sideInfo bytes - header - CRC (if present) + pad (if present) */
275 mp3DecInfo->nSlots = (int)slotTab[fh->ver][fh->srIdx][fh->brIdx] -
276 (int)sideBytesTab[fh->ver][(fh->sMode == Mono ? 0 : 1)] -
277 4 - (fh->crc ? 2 : 0) + (fh->paddingBit ? 1 : 0);
278 }
279
280 /* load crc word, if enabled, and return length of frame header (in bytes) */
281 if (fh->crc) {
282 fh->CRCWord = ((int)buf[4] << 8 | (int)buf[5] << 0);
283 return 6;
284 } else {
285 fh->CRCWord = 0;
286 return 4;
287 }
288}
289
290/**************************************************************************************
291 * Function: UnpackSideInfo
292 *
293 * Description: parse the fields of the MP3 side info header
294 *
295 * Inputs: MP3DecInfo structure filled by UnpackFrameHeader()
296 * buffer pointing to the MP3 side info data
297 *
298 * Outputs: updated mainDataBegin in MP3DecInfo struct
299 * updated private (platform-specific) SideInfo struct
300 *
301 * Return: length (in bytes) of side info data
302 * -1 if null input pointers
303 **************************************************************************************/
304int UnpackSideInfo(MP3DecInfo *mp3DecInfo, const unsigned char *buf) FL_NOEXCEPT
305{
306 int gr, ch, bd, nBytes;
307 BitStreamInfo bitStreamInfo, *bsi;
308 FrameHeader *fh;
309 SideInfo *si;
310 SideInfoSub *sis;
311
312 /* validate pointers and sync word */
313 if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS)
314 return -1;
315
316 fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
317 si = ((SideInfo *)(mp3DecInfo->SideInfoPS));
318
319 bsi = &bitStreamInfo;
320 if (fh->ver == MPEG1) {
321 /* MPEG 1 */
323 SetBitstreamPointer(bsi, nBytes, buf);
324 si->mainDataBegin = GetBits(bsi, 9);
325 si->privateBits = GetBits(bsi, (fh->sMode == Mono ? 5 : 3));
326
327 for (ch = 0; ch < mp3DecInfo->nChans; ch++)
328 for (bd = 0; bd < MAX_SCFBD; bd++)
329 si->scfsi[ch][bd] = GetBits(bsi, 1);
330 } else {
331 /* MPEG 2, MPEG 2.5 */
333 SetBitstreamPointer(bsi, nBytes, buf);
334 si->mainDataBegin = GetBits(bsi, 8);
335 si->privateBits = GetBits(bsi, (fh->sMode == Mono ? 1 : 2));
336 }
337
338 for(gr =0; gr < mp3DecInfo->nGrans; gr++) {
339 for (ch = 0; ch < mp3DecInfo->nChans; ch++) {
340 sis = &si->sis[gr][ch]; /* side info subblock for this granule, channel */
341
342 sis->part23Length = GetBits(bsi, 12);
343 sis->nBigvals = GetBits(bsi, 9);
344 sis->globalGain = GetBits(bsi, 8);
345 sis->sfCompress = GetBits(bsi, (fh->ver == MPEG1 ? 4 : 9));
346 sis->winSwitchFlag = GetBits(bsi, 1);
347
348 if(sis->winSwitchFlag) {
349 /* this is a start, stop, short, or mixed block */
350 sis->blockType = GetBits(bsi, 2); /* 0 = normal, 1 = start, 2 = short, 3 = stop */
351 sis->mixedBlock = GetBits(bsi, 1); /* 0 = not mixed, 1 = mixed */
352 sis->tableSelect[0] = GetBits(bsi, 5);
353 sis->tableSelect[1] = GetBits(bsi, 5);
354 sis->tableSelect[2] = 0; /* unused */
355 sis->subBlockGain[0] = GetBits(bsi, 3);
356 sis->subBlockGain[1] = GetBits(bsi, 3);
357 sis->subBlockGain[2] = GetBits(bsi, 3);
358
359 /* TODO - check logic */
360 if (sis->blockType == 0) {
361 /* this should not be allowed, according to spec */
362 sis->nBigvals = 0;
363 sis->part23Length = 0;
364 sis->sfCompress = 0;
365 } else if (sis->blockType == 2 && sis->mixedBlock == 0) {
366 /* short block, not mixed */
367 sis->region0Count = 8;
368 } else {
369 /* start, stop, or short-mixed */
370 sis->region0Count = 7;
371 }
372 sis->region1Count = 20 - sis->region0Count;
373 } else {
374 /* this is a normal block */
375 sis->blockType = 0;
376 sis->mixedBlock = 0;
377 sis->tableSelect[0] = GetBits(bsi, 5);
378 sis->tableSelect[1] = GetBits(bsi, 5);
379 sis->tableSelect[2] = GetBits(bsi, 5);
380 sis->region0Count = GetBits(bsi, 4);
381 sis->region1Count = GetBits(bsi, 3);
382 }
383 sis->preFlag = (fh->ver == MPEG1 ? GetBits(bsi, 1) : 0);
384 sis->sfactScale = GetBits(bsi, 1);
385 sis->count1TableSelect = GetBits(bsi, 1);
386 }
387 }
388 mp3DecInfo->mainDataBegin = si->mainDataBegin; /* needed by main decode loop */
389
390 ASSERT(nBytes == CalcBitsUsed(bsi, buf, 0) >> 3);
391
392 return nBytes;
393}
394
395} // namespace third_party
396} // namespace fl
StereoMode
Definition coder.h:143
@ Mono
Definition coder.h:147
@ Joint
Definition coder.h:145
#define SIBYTES_MPEG2_MONO
Definition coder.h:95
#define SIBYTES_MPEG1_MONO
Definition coder.h:93
#define SIBYTES_MPEG2_STEREO
Definition coder.h:96
#define SIBYTES_MPEG1_STEREO
Definition coder.h:94
#define ASSERT(x)
Definition coder.h:56
#define NGRANS_MPEG2
Definition mp3common.h:54
#define MAX_SCFBD
Definition mp3common.h:52
#define SYNCWORDL
Definition mp3common.h:64
#define SYNCWORDH
Definition mp3common.h:63
#define NGRANS_MPEG1
Definition mp3common.h:53
struct _MP3DecInfo MP3DecInfo
MPEGVersion
Definition mp3dec.h:82
@ MPEG25
Definition mp3dec.h:85
@ MPEG2
Definition mp3dec.h:84
@ MPEG1
Definition mp3dec.h:83
struct fl::third_party::_BitStreamInfo BitStreamInfo
fl::u32 uint32_t
Definition coder.h:219
const short slotTab[3][3][15]
Definition mp3tabs.hpp:110
const short sideBytesTab[3][2]
Definition mp3tabs.hpp:100
int UnpackFrameHeader(MP3DecInfo *mp3DecInfo, const unsigned char *buf) FL_NOEXCEPT
const int32_t samplerateTab[3][3]
Definition mp3tabs.hpp:53
struct fl::third_party::_FrameHeader FrameHeader
uint32_t GetBits(BitStreamInfo *bsi, int32_t nBits) FL_NOEXCEPT
void SetBitstreamPointer(BitStreamInfo *bsi, int32_t nBytes, const unsigned char *buf) FL_NOEXCEPT
Definition bitstream.hpp:65
int UnpackSideInfo(MP3DecInfo *mp3DecInfo, const unsigned char *buf) FL_NOEXCEPT
struct fl::third_party::_SideInfo SideInfo
const short samplesPerFrameTab[3][3]
Definition mp3tabs.hpp:88
const SFBandTable sfBandTable[3][3]
Definition mp3tabs.hpp:135
fl::i32 int32_t
Definition coder.h:220
struct fl::third_party::_SideInfoSub SideInfoSub
const short bitrateTab[3][3][15]
Definition mp3tabs.hpp:64
int CheckPadBit(MP3DecInfo *mp3DecInfo) FL_NOEXCEPT
static __inline void RefillBitstreamCache(BitStreamInfo *bsi) FL_NOEXCEPT
Definition bitstream.hpp:92
int32_t CalcBitsUsed(BitStreamInfo *bsi, const unsigned char *startBuf, int32_t startOffset) FL_NOEXCEPT
const SFBandTable * sfBand
Definition coder.h:175
SideInfoSub sis[MAX_NGRAN][MAX_NCHAN]
Definition coder.h:200
int32_t scfsi[MAX_NCHAN][MAX_SCFBD]
Definition coder.h:198
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT