Skip to content

Commit 2e8abb7

Browse files
authored
Make MP3 decoder input pointers const-correct (#33)
Update MP3 decoder API to use const unsigned char* for input buffers, making it explicit that input data is not modified. Changes: - All public APIs (MP3Decode, MP3GetNextFrameInfo, MP3FindSyncWord) now take const pointers - BitStreamInfo.bytePtr is now const-qualified - Internal functions (SetBitstreamPointer, UnpackFrameHeader, UnpackSideInfo, DecodeHuffman, etc.) updated for const-correctness throughout the call chain This improves API safety and follows C++ const-correctness best practices while maintaining full backward compatibility.
1 parent 4a2fa5e commit 2e8abb7

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

include/mp3_decoder.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ typedef struct _SFBandTable {
131131
} SFBandTable;
132132

133133
typedef struct _BitStreamInfo {
134-
unsigned char *bytePtr;
134+
const unsigned char *bytePtr;
135135
unsigned int iCache;
136136
int cachedBits;
137137
int nBytes;
@@ -262,9 +262,9 @@ typedef struct _SubbandInfo {
262262
} SubbandInfo;
263263

264264
/* bitstream.c */
265-
void SetBitstreamPointer(BitStreamInfo *bsi, int nBytes, unsigned char *buf);
265+
void SetBitstreamPointer(BitStreamInfo *bsi, int nBytes, const unsigned char *buf);
266266
unsigned int GetBits(BitStreamInfo *bsi, int nBits);
267-
int CalcBitsUsed(BitStreamInfo *bsi, unsigned char *startBuf, int startOffset);
267+
int CalcBitsUsed(BitStreamInfo *bsi, const unsigned char *startBuf, int startOffset);
268268

269269
/* dequant.c, dqchan.c, stproc.c */
270270
int DequantChannel(int *sampleBuf, int *workBuf, int *nonZeroBound, FrameHeader *fh, SideInfoSub *sis,
@@ -338,12 +338,12 @@ typedef struct _MP3DecInfo {
338338
MP3DecInfo *AllocateBuffers(void);
339339
void FreeBuffers(MP3DecInfo *mp3DecInfo);
340340
int CheckPadBit(MP3DecInfo *mp3DecInfo);
341-
int UnpackFrameHeader(MP3DecInfo *mp3DecInfo, unsigned char *buf);
342-
int UnpackSideInfo(MP3DecInfo *mp3DecInfo, unsigned char *buf);
343-
int DecodeHuffman(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch);
341+
int UnpackFrameHeader(MP3DecInfo *mp3DecInfo, const unsigned char *buf);
342+
int UnpackSideInfo(MP3DecInfo *mp3DecInfo, const unsigned char *buf);
343+
int DecodeHuffman(MP3DecInfo *mp3DecInfo, const unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch);
344344
int Dequantize(MP3DecInfo *mp3DecInfo, int gr);
345345
int IMDCT(MP3DecInfo *mp3DecInfo, int gr, int ch);
346-
int UnpackScaleFactors(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, int bitsAvail, int gr, int ch);
346+
int UnpackScaleFactors(MP3DecInfo *mp3DecInfo, const unsigned char *buf, int *bitOffset, int bitsAvail, int gr, int ch);
347347
int Subband(MP3DecInfo *mp3DecInfo, short *pcmBuf);
348348

349349
extern const int samplerateTab[3][3];
@@ -387,11 +387,11 @@ typedef struct _MP3FrameInfo {
387387
/* public API */
388388
HMP3Decoder MP3InitDecoder(void);
389389
void MP3FreeDecoder(HMP3Decoder hMP3Decoder);
390-
int MP3Decode(HMP3Decoder hMP3Decoder, unsigned char **inbuf, int *bytesLeft, short *outbuf, int useSize);
390+
int MP3Decode(HMP3Decoder hMP3Decoder, const unsigned char **inbuf, int *bytesLeft, short *outbuf, int useSize);
391391

392392
void MP3GetLastFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo);
393-
int MP3GetNextFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo, unsigned char *buf);
394-
int MP3FindSyncWord(unsigned char *buf, int nBytes);
393+
int MP3GetNextFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo, const unsigned char *buf);
394+
int MP3FindSyncWord(const unsigned char *buf, int nBytes);
395395

396396
} // namespace helix_decoder
397397
} // namespace esp_audio_libs

src/decode/mp3_decoder.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,9 +1719,9 @@ static void UnpackSFMPEG2(BitStreamInfo *bsi, SideInfoSub *sis, ScaleFactorInfoS
17191719
* Return: length (in bytes) of scale factor data, -1 if null input
17201720
*pointers
17211721
**************************************************************************************/
1722-
int UnpackScaleFactors(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, int bitsAvail, int gr, int ch) {
1722+
int UnpackScaleFactors(MP3DecInfo *mp3DecInfo, const unsigned char *buf, int *bitOffset, int bitsAvail, int gr, int ch) {
17231723
int bitsUsed;
1724-
unsigned char *startBuf;
1724+
const unsigned char *startBuf;
17251725
BitStreamInfo bitStreamInfo, *bsi;
17261726
FrameHeader *fh;
17271727
SideInfo *si;
@@ -7119,7 +7119,7 @@ const int quadTabMaxBits[2] = {6, 4};
71197119
*not necessarily all linBits outputs for x,y > 15)
71207120
**************************************************************************************/
71217121
// no improvement with section=data
7122-
static int DecodeHuffmanPairs(int *xy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset) {
7122+
static int DecodeHuffmanPairs(int *xy, int nVals, int tabIdx, int bitsLeft, const unsigned char *buf, int bitOffset) {
71237123
int i, x, y;
71247124
int cachedBits, padBits, len, startBits, linBits, maxBits, minBits;
71257125
HuffTabType tabType;
@@ -7351,7 +7351,7 @@ static int DecodeHuffmanPairs(int *xy, int nVals, int tabIdx, int bitsLeft, unsi
73517351
* Notes: si_huff.bit tests every vwxy output in both quad tables
73527352
**************************************************************************************/
73537353
// no improvement with section=data
7354-
static int DecodeHuffmanQuads(int *vwxy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset) {
7354+
static int DecodeHuffmanQuads(int *vwxy, int nVals, int tabIdx, int bitsLeft, const unsigned char *buf, int bitOffset) {
73557355
int i, v, w, x, y;
73567356
int len, maxBits, cachedBits, padBits;
73577357
unsigned int cache;
@@ -7467,10 +7467,10 @@ static int DecodeHuffmanQuads(int *vwxy, int nVals, int tabIdx, int bitsLeft, un
74677467
* out of bits prematurely (invalid bitstream)
74687468
**************************************************************************************/
74697469
// .data about 1ms faster per frame
7470-
int DecodeHuffman(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch) {
7470+
int DecodeHuffman(MP3DecInfo *mp3DecInfo, const unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch) {
74717471
int r1Start, r2Start, rEnd[4]; /* region boundaries */
74727472
int i, w, bitsUsed, bitsLeft;
7473-
unsigned char *startBuf = buf;
7473+
const unsigned char *startBuf = buf;
74747474

74757475
FrameHeader *fh;
74767476
SideInfo *si;
@@ -8145,7 +8145,7 @@ void FreeBuffers(MP3DecInfo *mp3DecInfo) {
81458145
*
81468146
* Return: none
81478147
**************************************************************************************/
8148-
void SetBitstreamPointer(BitStreamInfo *bsi, int nBytes, unsigned char *buf) {
8148+
void SetBitstreamPointer(BitStreamInfo *bsi, int nBytes, const unsigned char *buf) {
81498149
/* init bitstream */
81508150
bsi->bytePtr = buf;
81518151
bsi->iCache = 0; /* 4-byte unsigned int */
@@ -8249,7 +8249,7 @@ unsigned int GetBits(BitStreamInfo *bsi, int nBits) {
82498249
* Return: number of bits read from bitstream, as offset from
82508250
*startBuf:startOffset
82518251
**************************************************************************************/
8252-
int CalcBitsUsed(BitStreamInfo *bsi, unsigned char *startBuf, int startOffset) {
8252+
int CalcBitsUsed(BitStreamInfo *bsi, const unsigned char *startBuf, int startOffset) {
82538253
int bitsUsed;
82548254

82558255
bitsUsed = (bsi->bytePtr - startBuf) * 8;
@@ -8301,7 +8301,7 @@ int CheckPadBit(MP3DecInfo *mp3DecInfo) {
83018301
* TODO: check for valid modes, depending on capabilities of decoder
83028302
* test CRC on actual stream (verify no endian problems)
83038303
**************************************************************************************/
8304-
int UnpackFrameHeader(MP3DecInfo *mp3DecInfo, unsigned char *buf) {
8304+
int UnpackFrameHeader(MP3DecInfo *mp3DecInfo, const unsigned char *buf) {
83058305
int verIdx;
83068306
FrameHeader *fh;
83078307

@@ -8386,7 +8386,7 @@ int UnpackFrameHeader(MP3DecInfo *mp3DecInfo, unsigned char *buf) {
83868386
* Return: length (in bytes) of side info data
83878387
* -1 if null input pointers
83888388
**************************************************************************************/
8389-
int UnpackSideInfo(MP3DecInfo *mp3DecInfo, unsigned char *buf) {
8389+
int UnpackSideInfo(MP3DecInfo *mp3DecInfo, const unsigned char *buf) {
83908390
int gr, ch, bd, nBytes;
83918391
BitStreamInfo bitStreamInfo, *bsi;
83928392
FrameHeader *fh;
@@ -8530,7 +8530,7 @@ void MP3FreeDecoder(HMP3Decoder hMP3Decoder) {
85308530
* Return: offset to first sync word (bytes from start of buf)
85318531
* -1 if sync not found after searching nBytes
85328532
**************************************************************************************/
8533-
int MP3FindSyncWord(unsigned char *buf, int nBytes) {
8533+
int MP3FindSyncWord(const unsigned char *buf, int nBytes) {
85348534
int i;
85358535

85368536
/* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching
@@ -8567,9 +8567,9 @@ int MP3FindSyncWord(unsigned char *buf, int nBytes) {
85678567
*function once (first frame) then store the result (nSlots) and just use it
85688568
*from then on
85698569
**************************************************************************************/
8570-
static int MP3FindFreeSync(unsigned char *buf, unsigned char firstFH[4], int nBytes) {
8570+
static int MP3FindFreeSync(const unsigned char *buf, const unsigned char firstFH[4], int nBytes) {
85718571
int offset = 0;
8572-
unsigned char *bufPtr = buf;
8572+
const unsigned char *bufPtr = buf;
85738573

85748574
/* loop until we either:
85758575
* - run out of nBytes (FindMP3SyncWord() returns -1)
@@ -8648,7 +8648,7 @@ void MP3GetLastFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo) {
86488648
* Return: error code, defined in mp3dec.h (0 means no error, < 0 means
86498649
*error)
86508650
**************************************************************************************/
8651-
int MP3GetNextFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo, unsigned char *buf) {
8651+
int MP3GetNextFrameInfo(HMP3Decoder hMP3Decoder, MP3FrameInfo *mp3FrameInfo, const unsigned char *buf) {
86528652
MP3DecInfo *mp3DecInfo = (MP3DecInfo *) hMP3Decoder;
86538653

86548654
if (!mp3DecInfo)
@@ -8707,10 +8707,10 @@ static void MP3ClearBadFrame(MP3DecInfo *mp3DecInfo, short *outbuf) {
87078707
* is not supported (bit reservoir is not maintained if useSize
87088708
*on)
87098709
**************************************************************************************/
8710-
int MP3Decode(HMP3Decoder hMP3Decoder, unsigned char **inbuf, int *bytesLeft, short *outbuf, int useSize) {
8710+
int MP3Decode(HMP3Decoder hMP3Decoder, const unsigned char **inbuf, int *bytesLeft, short *outbuf, int useSize) {
87118711
int offset, bitOffset, mainBits, gr, ch, fhBytes, siBytes, freeFrameBytes;
87128712
int prevBitOffset, sfBlockBits, huffBlockBits;
8713-
unsigned char *mainPtr;
8713+
const unsigned char *mainPtr;
87148714
MP3DecInfo *mp3DecInfo = (MP3DecInfo *) hMP3Decoder;
87158715

87168716
if (!mp3DecInfo)

0 commit comments

Comments
 (0)