Compare commits

...

5 commits

Author SHA1 Message Date
2a3cc7873b buffers: Allow an allocator to be specified
This will help enable CircuitPython to use the Adafruit_MP3 library
to implement its MP3File functionality, including the creation of
multiple MP3 playback objects at the same time.

This does not intentionally change any behavior on Arduino.

Testing performed:
 * In Arduino, verified the "play_from_header" example still worked
   on a PyPortal (SAMD51)
 * In WIP CircuitPython, verified that playback (including 2 simultaneous
   mp3 files) functioned on nRF52840 Feather

# Conflicts:
#	src/buffers.c
2019-11-29 10:22:42 -06:00
3668c862fb Support "generic little endian ARM" MCUs
Hopefully all intended architectures support the smlal instruction;
this is supported on "ARM architecture v3M, and ARM architecture v4
and above except xM variants", and missing support should be caught
as an assembler error.
2019-11-29 10:22:42 -06:00
656ca5b2ff various: remove unused-but-set variables
These are treated as compile errors by CircuitPython, and we'd like
to keep it that way.
2019-11-29 10:22:27 -06:00
e9558241f1 various: include <stdint.h> for standard integer typedefs 2019-11-29 10:17:50 -06:00
25c1f2b4d2 Prepare for use of this code from CircuitPython
* Treat most files as "C" files
 * add "extern C" guards to C-ish headers
   for when they are included from C++ files
 * Make Arduino.h inclusion optional,
   not available in CircuitPython
2019-11-29 10:17:47 -06:00
19 changed files with 123 additions and 64 deletions

View file

@ -56,6 +56,10 @@
#ifndef _ASSEMBLY_H
#define _ASSEMBLY_H
#ifdef __cplusplus
extern "C" {
#endif
#if (defined _WIN32 && !defined _WIN32_WCE) || (defined __WINS__ && defined _SYMBIAN) || defined(_OPENWAVE_SIMULATOR) || defined(WINCE_EMULATOR) /* Symbian emulator for Ix86 */
#pragma warning( disable : 4035 ) /* complains about inline asm not returning a value */
@ -267,7 +271,7 @@ static __inline int CLZ(int x)
return numZeros;
}
#elif defined(__GNUC__) && defined(ARM)
#elif defined(__GNUC__) && (defined(ARM) || defined(__ARMEL__))
static __inline int MULSHIFT32(int x, int y)
{
@ -317,6 +321,42 @@ static __inline int CLZ(int x)
return numZeros;
}
typedef signed long long int Word64; // 64-bit signed integer.
typedef union _U64 {
Word64 w64;
struct {
/* ARM ADS = little endian */
unsigned int lo32;
signed int hi32;
} r;
} U64;
static __inline Word64 MADD64(Word64 sum64, int x, int y)
{
U64 u;
u.w64 = sum64;
__asm__ volatile ("smlal %0,%1,%2,%3" : "+&r" (u.r.lo32), "+&r" (u.r.hi32) : "r" (x), "r" (y) : "cc");
return u.w64;
}
__attribute__((__always_inline__)) static __inline Word64 SAR64(Word64 x, int n)
{
unsigned int xLo = (unsigned int) x;
int xHi = (int) (x >> 32);
int nComp = 32-n;
int tmp;
// Shortcut: n is always < 32.
__asm__ __volatile__( "lsl %2, %0, %3\n\t" // tmp <- xHi<<(32-n)
"asr %0, %0, %4\n\t" // xHi <- xHi>>n
"lsr %1, %1, %4\n\t" // xLo <- xLo>>n
"orr %1, %2\n\t" // xLo <= xLo || tmp
: "+&r" (xHi), "+r" (xLo), "=&r" (tmp)
: "r" (nComp), "r" (n) );
x = xLo | ((Word64)xHi << 32);
return( x );
}
#elif defined(__GNUC__) && defined(__AVR32_UC__)
typedef signed long long int Word64; // 64-bit signed integer.
@ -473,4 +513,7 @@ __attribute__((__always_inline__)) static __inline Word64 SAR64(Word64 x, int n)
#endif /* platforms */
#ifdef __cplusplus
}
#endif
#endif /* _ASSEMBLY_H */

View file

@ -67,6 +67,7 @@
*
* Notes: slow, platform-independent equivalent to memset(buf, 0, nBytes)
**************************************************************************************/
#ifndef MPDEC_ALLOCATOR
static void ClearBuffer(void *buf, int nBytes)
{
int i;
@ -77,6 +78,7 @@ static void ClearBuffer(void *buf, int nBytes)
return;
}
#endif
/**************************************************************************************
* Function: AllocateBuffers
@ -97,7 +99,7 @@ static void ClearBuffer(void *buf, int nBytes)
MP3DecInfo *AllocateBuffers(void)
{
MP3DecInfo *mp3DecInfo;
/*
#ifdef MPDEC_ALLOCATOR
FrameHeader *fh;
SideInfo *si;
ScaleFactorInfo *sfi;
@ -105,20 +107,8 @@ MP3DecInfo *AllocateBuffers(void)
DequantInfo *di;
IMDCTInfo *mi;
SubbandInfo *sbi;
*/
// Buffers:
static char s_mp3DecInfo[sizeof(MP3DecInfo)];
static char fh[sizeof(FrameHeader)];
static char si[sizeof(SideInfo)];
static char sfi[sizeof(ScaleFactorInfo)];
static char hi[sizeof(HuffmanInfo)];
static char di[sizeof(DequantInfo)];
static char mi[sizeof(IMDCTInfo)];
static char sbi[sizeof(SubbandInfo)];
//mp3DecInfo = (MP3DecInfo *)malloc(sizeof(MP3DecInfo));
mp3DecInfo = (MP3DecInfo *)s_mp3DecInfo;
mp3DecInfo = (MP3DecInfo *)MPDEC_ALLOCATOR(sizeof(MP3DecInfo));
if (!mp3DecInfo)
{
#ifndef _WIN32
@ -129,35 +119,23 @@ MP3DecInfo *AllocateBuffers(void)
#endif
return 0;
}
ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));
/* Switched to static allocations.
hi = (HuffmanInfo *) malloc(sizeof(HuffmanInfo));
sbi = (SubbandInfo *) malloc(sizeof(SubbandInfo));
mi = (IMDCTInfo *) malloc(sizeof(IMDCTInfo));
di = (DequantInfo *) malloc(sizeof(DequantInfo));
si = (SideInfo *) malloc(sizeof(SideInfo));
sfi = (ScaleFactorInfo *) malloc(sizeof(ScaleFactorInfo));
fh = (FrameHeader *) malloc(sizeof(FrameHeader));
*/
mp3DecInfo->FrameHeaderPS = (void *)fh;
mp3DecInfo->SideInfoPS = (void *)si;
mp3DecInfo->ScaleFactorInfoPS = (void *)sfi;
mp3DecInfo->HuffmanInfoPS = (void *)hi;
mp3DecInfo->DequantInfoPS = (void *)di;
mp3DecInfo->IMDCTInfoPS = (void *)mi;
mp3DecInfo->SubbandInfoPS = (void *)sbi;
hi = (HuffmanInfo *) MPDEC_ALLOCATOR(sizeof(HuffmanInfo));
sbi = (SubbandInfo *) MPDEC_ALLOCATOR(sizeof(SubbandInfo));
mi = (IMDCTInfo *) MPDEC_ALLOCATOR(sizeof(IMDCTInfo));
di = (DequantInfo *) MPDEC_ALLOCATOR(sizeof(DequantInfo));
si = (SideInfo *) MPDEC_ALLOCATOR(sizeof(SideInfo));
sfi = (ScaleFactorInfo *) MPDEC_ALLOCATOR(sizeof(ScaleFactorInfo));
fh = (FrameHeader *) MPDEC_ALLOCATOR(sizeof(FrameHeader));
/* Removed; static allocation guarantees success.
if (!fh || !si || !sfi || !hi || !di || !mi || !sbi) {
#ifndef _WIN32
#ifdef DEMO_HELIX_FOOTPRINT
sprintf(COPY_DEBUG_BUFFER,"mp3DecInfo:%d[%d] | fh:%d[%d] | si:%d[%d] \
| sfi:%d[%d] | hi:%d[%d] | di:%d[%d] | mi:%d[%d] | sbi:%d[%d]\n",
(int)mp3DecInfo, (int)sizeof(MP3DecInfo), (int)fh, (int)sizeof(FrameHeader),
(int)si, (int)sizeof(SideInfo), (int)sfi, (int)sizeof(ScaleFactorInfo),
(int)hi, (int)sizeof(HuffmanInfo), (int)di, (int)sizeof(DequantInfo),
| sfi:%d[%d] | hi:%d[%d] | di:%d[%d] | mi:%d[%d] | sbi:%d[%d]\n",
(int)mp3DecInfo, (int)sizeof(MP3DecInfo), (int)fh, (int)sizeof(FrameHeader),
(int)si, (int)sizeof(SideInfo), (int)sfi, (int)sizeof(ScaleFactorInfo),
(int)hi, (int)sizeof(HuffmanInfo), (int)di, (int)sizeof(DequantInfo),
(int)mi, (int)sizeof(IMDCTInfo), (int)sbi, (int)sizeof(SubbandInfo) );
DV_DEBUG_USART_Trace( COPY_DEBUG_BUFFER );
#endif
@ -165,17 +143,20 @@ MP3DecInfo *AllocateBuffers(void)
FreeBuffers(mp3DecInfo); // safe to call - only frees memory that was successfully allocated
return 0;
}
*/
#ifndef _WIN32
#ifdef DEMO_HELIX_FOOTPRINT
sprintf(COPY_DEBUG_BUFFER, "Total decoder malloc size: %d\n",
(int)(sizeof(MP3DecInfo) + sizeof(FrameHeader) + sizeof(SideInfo)
+ sizeof(ScaleFactorInfo) + sizeof(HuffmanInfo) + sizeof(DequantInfo)
+ sizeof(IMDCTInfo) + sizeof(SubbandInfo)));
DV_DEBUG_USART_Trace( COPY_DEBUG_BUFFER );
#endif
#endif
#else
// Buffers:
static char s_mp3DecInfo[sizeof(MP3DecInfo)];
static char fh[sizeof(FrameHeader)];
static char si[sizeof(SideInfo)];
static char sfi[sizeof(ScaleFactorInfo)];
static char hi[sizeof(HuffmanInfo)];
static char di[sizeof(DequantInfo)];
static char mi[sizeof(IMDCTInfo)];
static char sbi[sizeof(SubbandInfo)];
mp3DecInfo = (MP3DecInfo *)s_mp3DecInfo;
ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));
/* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
ClearBuffer(fh, sizeof(FrameHeader));
@ -186,10 +167,34 @@ MP3DecInfo *AllocateBuffers(void)
ClearBuffer(mi, sizeof(IMDCTInfo));
ClearBuffer(sbi, sizeof(SubbandInfo));
#endif
mp3DecInfo->FrameHeaderPS = (void *)fh;
mp3DecInfo->SideInfoPS = (void *)si;
mp3DecInfo->ScaleFactorInfoPS = (void *)sfi;
mp3DecInfo->HuffmanInfoPS = (void *)hi;
mp3DecInfo->DequantInfoPS = (void *)di;
mp3DecInfo->IMDCTInfoPS = (void *)mi;
mp3DecInfo->SubbandInfoPS = (void *)sbi;
#ifndef _WIN32
#ifdef DEMO_HELIX_FOOTPRINT
sprintf(COPY_DEBUG_BUFFER, "Total decoder malloc size: %d\n",
(int)(sizeof(MP3DecInfo) + sizeof(FrameHeader) + sizeof(SideInfo)
+ sizeof(ScaleFactorInfo) + sizeof(HuffmanInfo) + sizeof(DequantInfo)
+ sizeof(IMDCTInfo) + sizeof(SubbandInfo)));
DV_DEBUG_USART_Trace( COPY_DEBUG_BUFFER );
#endif
#endif
return mp3DecInfo;
}
#define SAFE_FREE(x) {if (x) free(x); (x) = 0;} /* helper macro */
#ifdef MPDEC_FREE
#define SAFE_FREE(x) {if (x) MPDEC_FREE(x); (x) = 0;} /* helper macro */
#else
#define SAFE_FREE(x) { (x) = 0; }
#endif
/**************************************************************************************
* Function: FreeBuffers
@ -208,8 +213,7 @@ void FreeBuffers(MP3DecInfo *mp3DecInfo)
{
if (!mp3DecInfo)
return;
/* Switched to static allocations.
SAFE_FREE(mp3DecInfo->FrameHeaderPS);
SAFE_FREE(mp3DecInfo->SideInfoPS);
SAFE_FREE(mp3DecInfo->ScaleFactorInfoPS);
@ -219,5 +223,4 @@ void FreeBuffers(MP3DecInfo *mp3DecInfo)
SAFE_FREE(mp3DecInfo->SubbandInfoPS);
SAFE_FREE(mp3DecInfo);
*/
}

View file

@ -46,6 +46,10 @@
#include "mp3common.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(ASSERT)
#undef ASSERT
#endif
@ -304,4 +308,7 @@ extern const int csa[8][2];
extern const int coef32[31];
extern const int polyCoef[264];
#ifdef __cplusplus
}
#endif
#endif /* _CODER_H */

View file

@ -43,6 +43,7 @@
#include "coder.h"
#include "assembly.h"
#include <stdint.h>
typedef int ARRAY3[3]; /* for short-block reordering */
@ -245,7 +246,7 @@ static const int pow2frac[8] = {
ScaleFactorInfoSub *sfis, CriticalBandInfo *cbi)
{
int i, j, w, cb;
int cbStartL, cbEndL, cbStartS, cbEndS;
int cbEndL, cbStartS, cbEndS;
int nSamps, nonZero, sfactMultiplier, gbMask;
int globalGain, gainI;
int cbMax[3];
@ -253,7 +254,6 @@ static const int pow2frac[8] = {
/* set default start/end points for short/long blocks - will update with non-zero cb info */
if (sis->blockType == 2) {
cbStartL = 0;
if (sis->mixedBlock) {
cbEndL = (fh->ver == MPEG1 ? 8 : 6);
cbStartS = 3;
@ -264,7 +264,6 @@ static const int pow2frac[8] = {
cbEndS = 13;
} else {
/* long block */
cbStartL = 0;
cbEndL = 22;
cbStartS = 13;
cbEndS = 13;

View file

@ -388,7 +388,6 @@ int DecodeHuffman(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, in
FrameHeader *fh;
SideInfo *si;
SideInfoSub *sis;
ScaleFactorInfo *sfi;
HuffmanInfo *hi;
/* validate pointers */
@ -398,7 +397,6 @@ int DecodeHuffman(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, in
fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
si = ((SideInfo *)(mp3DecInfo->SideInfoPS));
sis = &si->sis[gr][ch];
sfi = ((ScaleFactorInfo *)(mp3DecInfo->ScaleFactorInfoPS));
hi = (HuffmanInfo*)(mp3DecInfo->HuffmanInfoPS);
if (huffBlockBits < 0)

View file

@ -44,6 +44,7 @@
#include "coder.h"
#include "assembly.h"
#include <stdint.h>
/**************************************************************************************
* Function: AntiAlias

View file

@ -47,6 +47,10 @@
#include "mp3dec.h"
#include "statname.h" /* do name-mangling for static linking */
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_SCFBD 4 /* max scalefactor bands per channel */
#define NGRANS_MPEG1 2
#define NGRANS_MPEG2 1
@ -120,4 +124,8 @@ extern const short sideBytesTab[3][2];
extern const short slotTab[3][3][15];
extern const SFBandTable sfBandTable[3][3];
#ifdef __cplusplus
}
#endif
#endif /* _MP3COMMON_H */

View file

@ -44,7 +44,9 @@
#ifndef _MP3DEC_H
#define _MP3DEC_H
#ifdef ARDUINO
#include <Arduino.h>
#endif
#if defined(_WIN32) && !defined(_WIN32_WCE)
#
@ -58,6 +60,8 @@
#
#elif defined(__GNUC__) && defined(ARM)
#
#elif defined(__GNUC__) && defined(__ARMEL__)
#
#elif defined(__GNUC__) && defined(__i386__)
#
#elif defined(_OPENWAVE_SIMULATOR) || defined(_OPENWAVE_ARMULATOR)

View file

@ -208,7 +208,7 @@ static const char NRTab[6][3][4] = {
static void UnpackSFMPEG2(BitStreamInfo *bsi, SideInfoSub *sis, ScaleFactorInfoSub *sfis, int gr, int ch, int modeExt, ScaleFactorJS *sfjs)
{
int i, sfb, sfcIdx, btIdx, nrIdx, iipTest;
int i, sfb, sfcIdx, btIdx, nrIdx;
int slen[4], nr[4];
int sfCompress, preFlag, intensityScale;
@ -298,7 +298,6 @@ static void UnpackSFMPEG2(BitStreamInfo *bsi, SideInfoSub *sis, ScaleFactorInfoS
if(sis->blockType == 2) {
if(sis->mixedBlock) {
/* do long block portion */
iipTest = (1 << slen[0]) - 1;
for (sfb=0; sfb < 6; sfb++) {
sfis->l[sfb] = (char)GetBits(bsi, slen[0]);
}
@ -312,7 +311,6 @@ static void UnpackSFMPEG2(BitStreamInfo *bsi, SideInfoSub *sis, ScaleFactorInfoS
/* remaining short blocks, sfb just keeps incrementing */
for ( ; nrIdx <= 3; nrIdx++) {
iipTest = (1 << slen[nrIdx]) - 1;
for (i=0; i < nr[nrIdx]; i++, sfb++) {
sfis->s[sfb][0] = (char)GetBits(bsi, slen[nrIdx]);
sfis->s[sfb][1] = (char)GetBits(bsi, slen[nrIdx]);
@ -325,7 +323,6 @@ static void UnpackSFMPEG2(BitStreamInfo *bsi, SideInfoSub *sis, ScaleFactorInfoS
/* long blocks */
sfb = 0;
for (nrIdx = 0; nrIdx <= 3; nrIdx++) {
iipTest = (1 << slen[nrIdx]) - 1;
for(i=0; i < nr[nrIdx]; i++, sfb++) {
sfis->l[sfb] = (char)GetBits(bsi, slen[nrIdx]);
}

View file

@ -60,7 +60,6 @@
int Subband(MP3DecInfo *mp3DecInfo, short *pcmBuf)
{
int b;
HuffmanInfo *hi;
IMDCTInfo *mi;
SubbandInfo *sbi;
@ -68,7 +67,6 @@ int Subband(MP3DecInfo *mp3DecInfo, short *pcmBuf)
if (!mp3DecInfo || !mp3DecInfo->HuffmanInfoPS || !mp3DecInfo->IMDCTInfoPS || !mp3DecInfo->SubbandInfoPS)
return -1;
hi = (HuffmanInfo *)mp3DecInfo->HuffmanInfoPS;
mi = (IMDCTInfo *)(mp3DecInfo->IMDCTInfoPS);
sbi = (SubbandInfo*)(mp3DecInfo->SubbandInfoPS);

View file

@ -44,6 +44,7 @@
// constants in RAM are not significantly faster
#include "coder.h"
#include <stdint.h>
/* post-IMDCT window, win[blockType][i]
* format = Q31