make gp2x mp3 playback functional (need to unpack and compile helix decoder separately in platform/common/helix)

This commit is contained in:
kub 2019-03-18 23:14:07 +01:00 committed by kub
parent c79d0bb90f
commit 340e528ff8
13 changed files with 274 additions and 59 deletions

View file

@ -0,0 +1,42 @@
CROSS ?= arm-linux-gnueabi-
CC = $(CROSS)gcc
AS = $(CROSS)as
AR = $(CROSS)ar
TOOLCHAIN = $(notdir $(CROSS))
CFLAGS += -Ipub -O2 -Wall -fstrict-aliasing -ffast-math
ifneq ($(findstring arm-,$(TOOLCHAIN)),)
CFLAGS += -mcpu=arm940t -mtune=arm940t -mfloat-abi=soft -mfpu=fpa -mabi=apcs-gnu -mno-thumb-interwork
ASFLAGS = -mcpu=arm940t -mfloat-abi=soft -mfpu=fpa -mabi=apcs-gnu
OBJS += real/arm/asmpoly_gcc.o
else
CFLAGS += -m32
ASFLAGS += -m32
OBJS += real/polyphase.o
endif
LIB = $(TOOLCHAIN)helix_mp3.a
SHLIB = $(TOOLCHAIN)helix_mp3.so
all: $(LIB) $(SHLIB)
OBJS += mp3dec.o mp3tabs.o
#OBJS += ipp/bitstream.o ipp/buffers.o ipp/dequant.o ipp/huffman.o ipp/imdct.o ipp/subband.o
OBJS += real/bitstream.o real/buffers.o real/dct32.o real/dequant.o real/dqchan.o real/huffman.o
OBJS += real/hufftabs.o real/imdct.o real/scalfact.o real/stproc.o real/subband.o real/trigtabs.o
OBJS += lib.o
real/arm/asmpoly_gcc.o: real/arm/asmpoly_gcc.s
$(CC) -o $@ $(ASFLAGS) -c $<
$(LIB) : $(OBJS)
$(AR) r $@ $^
$(SHLIB) : $(OBJS) /home/build/opt/open2x/gcc-4.1.1-glibc-2.3.6/lib/gcc/arm-open2x-linux/4.1.1/libgcc.a
$(CC) -o $@ -nostdlib -shared $(CFLAGS) $^
clean:
$(RM) -f $(OBJS)

122
platform/common/helix/lib.c Normal file
View file

@ -0,0 +1,122 @@
#include <stdlib.h>
#include <stdint.h>
// libgcc has this with gcc 4.x
void raise(int sig)
{
}
// very limited heap functions for helix decoder
static char heap[65000] __attribute__((aligned(16)));
static long heap_offs;
void __malloc_init(void)
{
heap_offs = 0;
}
void *malloc(size_t size)
{
void *chunk = heap + heap_offs;
size = (size+15) & ~15;
if (heap_offs + size > sizeof(heap))
return NULL;
else {
heap_offs += size;
return chunk;
}
}
void free(void *chunk)
{
if (chunk == heap)
heap_offs = 0;
}
#if 0
void *memcpy (void *dest, const void *src, size_t n)
{
char *_dest = dest;
const char *_src = src;
while (n--) *_dest++ = *_src++;
return dest;
}
void *memmove (void *dest, const void *src, size_t n)
{
char *_dest = dest+n;
const char *_src = src+n;
if (dest <= src || dest >= _src)
return memcpy(dest, src, n);
while (n--) *--_dest = *--_src;
return dest;
}
#else
/* memcpy/memmove in C with some simple optimizations.
* ATTN does dirty aliasing tricks with undefined behaviour by standard.
* (this works fine with gcc, though...)
*/
void *memcpy(void *dest, const void *src, size_t n)
{
struct _16 { uint32_t a[4]; };
union { const void *v; char *c; uint64_t *l; struct _16 *s; }
ss = { src }, ds = { dest };
const int lm = sizeof(uint32_t)-1;
if ((((unsigned)ss.c ^ (unsigned)ds.c) & lm) == 0) {
/* fast copy if pointers have the same aligment */
while (((unsigned)ss.c & lm) && n > 0) /* align to word */
*ds.c++ = *ss.c++, n--;
while (n >= sizeof(struct _16)) /* copy 16 bytes blocks */
*ds.s++ = *ss.s++, n -= sizeof(struct _16);
if (n >= sizeof(uint64_t)) /* copy leftover 8 byte block */
*ds.l++ = *ss.l++, n -= sizeof(uint64_t);
} else {
/* byte copy if pointers are unaligned */
while (n >= 8) { /* copy 8 byte blocks */
*ds.c++ = *ss.c++, n--; *ds.c++ = *ss.c++, n--;
*ds.c++ = *ss.c++, n--; *ds.c++ = *ss.c++, n--;
*ds.c++ = *ss.c++, n--; *ds.c++ = *ss.c++, n--;
*ds.c++ = *ss.c++, n--; *ds.c++ = *ss.c++, n--;
}
}
/* copy max. 8 leftover bytes */
while (n > 0)
*ds.c++ = *ss.c++, n--;
return dest;
}
void *memmove (void *dest, const void *src, size_t n)
{
struct _16 { uint32_t a[4]; };
union { const void *v; char *c; uint64_t *l; struct _16 *s; }
ss = { src+n }, ds = { dest+n };
const int lm = sizeof(uint32_t)-1;
if (dest <= src || dest >= src+n)
return memcpy(dest, src, n);
if ((((unsigned)ss.c ^ (unsigned)ds.c) & lm) == 0) {
/* fast copy if pointers have the same aligment */
while (((unsigned)ss.c & lm) && n > 0)
*--ds.c = *--ss.c, n--;
while (n >= sizeof(struct _16))
*--ds.s = *--ss.s, n -= sizeof(struct _16);
if (n >= sizeof(uint64_t))
*--ds.l = *--ss.l, n -= sizeof(uint64_t);
} else {
/* byte copy if pointers are unaligned */
while (n >= 8) {
*--ds.c = *--ss.c, n--; *--ds.c = *--ss.c, n--;
*--ds.c = *--ss.c, n--; *--ds.c = *--ss.c, n--;
*--ds.c = *--ss.c, n--; *--ds.c = *--ss.c, n--;
*--ds.c = *--ss.c, n--; *--ds.c = *--ss.c, n--;
}
}
/* copy max. 8 leftover bytes */
while (n > 0)
*--ds.c = *--ss.c, n--;
return dest;
}
#endif

View file

@ -21,33 +21,6 @@ unsigned short mpeg1_l3_bitrates[16] = {
0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
};
int mp3_find_sync_word(const unsigned char *buf, int size)
{
const unsigned char *p, *pe;
/* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */
for (p = buf, pe = buf + size - 3; p <= pe; p++)
{
int pn;
if (p[0] != 0xff)
continue;
pn = p[1];
if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1
(pn & 6) == 0) { // invalid layer
p++; continue;
}
pn = p[2];
if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates
(pn & 0x0c) != 0) { // not 44kHz
continue;
}
return p - buf;
}
return -1;
}
static int try_get_bitrate(unsigned char *buf, int buf_size)
{
int offs1, offs = 0;

View file

@ -12,8 +12,8 @@ int mp3dec_decode(FILE *f, int *file_pos, int file_len);
extern unsigned short mpeg1_l3_bitrates[16];
#ifdef __GP2X__
void mp3_update_local(int *buffer, int length, int stereo);
void mp3_start_play_local(void *f, int pos);
int _mp3dec_start(FILE *f, int fpos_start);
int _mp3dec_decode(FILE *f, int *file_pos, int file_len);
#endif
#endif // __COMMON_MP3_H__

View file

@ -9,6 +9,7 @@
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <pico/pico_int.h>
#include <pico/sound/mix.h>
@ -20,10 +21,15 @@ static HMP3Decoder mp3dec;
static unsigned char mp3_input_buffer[2 * 1024];
#ifdef __GP2X__
#define mp3_update mp3_update_local
#define mp3_start_play mp3_start_play_local
#define mp3dec_decode _mp3dec_decode
#define mp3dec_start _mp3dec_start
#endif
static void *libhelix;
HMP3Decoder (*p_MP3InitDecoder)(void);
void (*p_MP3FreeDecoder)(HMP3Decoder);
int (*p_MP3Decode)(HMP3Decoder, unsigned char **, int *, short *, int);
int mp3dec_decode(FILE *f, int *file_pos, int file_len)
{
unsigned char *readPtr;
@ -51,7 +57,7 @@ int mp3dec_decode(FILE *f, int *file_pos, int file_len)
bytesLeft -= offset;
had_err = err;
err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
err = p_MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0);
if (err) {
if (err == ERR_MP3_MAINDATA_UNDERFLOW && !had_err) {
// just need another frame
@ -86,10 +92,31 @@ int mp3dec_decode(FILE *f, int *file_pos, int file_len)
int mp3dec_start(FILE *f, int fpos_start)
{
if (libhelix == NULL) {
libhelix = dlopen("./libhelix.so", RTLD_NOW);
if (libhelix == NULL) {
lprintf("mp3dec: load libhelix.so: %s\n", dlerror());
return -1;
}
p_MP3InitDecoder = dlsym(libhelix, "MP3InitDecoder");
p_MP3FreeDecoder = dlsym(libhelix, "MP3FreeDecoder");
p_MP3Decode = dlsym(libhelix, "MP3Decode");
if (p_MP3InitDecoder == NULL || p_MP3FreeDecoder == NULL
|| p_MP3Decode == NULL)
{
lprintf("mp3dec: missing symbol(s) in libhelix.so\n");
dlclose(libhelix);
libhelix = NULL;
return -1;
}
}
// must re-init decoder for new track
if (mp3dec)
MP3FreeDecoder(mp3dec);
mp3dec = MP3InitDecoder();
p_MP3FreeDecoder(mp3dec);
mp3dec = p_MP3InitDecoder();
return (mp3dec == 0) ? -1 : 0;
}

View file

@ -0,0 +1,27 @@
int mp3_find_sync_word(const unsigned char *buf, int size)
{
const unsigned char *p, *pe;
/* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */
for (p = buf, pe = buf + size - 3; p <= pe; p++)
{
int pn;
if (p[0] != 0xff)
continue;
pn = p[1];
if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1
(pn & 6) == 0) { // invalid layer
p++; continue;
}
pn = p[2];
if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates
(pn & 0x0c) != 0) { // not 44kHz
continue;
}
return p - buf;
}
return -1;
}