Index: apps/codecs/vorbis.c =================================================================== --- apps/codecs/vorbis.c (revision 22075) +++ apps/codecs/vorbis.c (working copy) @@ -22,10 +22,13 @@ #include "codeclib.h" #include "libtremor/ivorbisfile.h" #include "libtremor/ogg.h" +#ifdef SIMULATOR +#include "libtremor/tlsf/tlsf.h" +#endif CODEC_HEADER -#if defined(CPU_ARM) || defined(CPU_COLDFIRE) +#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS) #include jmp_buf rb_jump_buf; #endif @@ -76,8 +79,7 @@ } /* This sets the DSP parameters based on the current logical bitstream - * (sampling rate, number of channels, etc). It also tries to guess - * reasonable buffer parameters based on the current quality setting. + * (sampling rate, number of channels, etc). */ static bool vorbis_set_codec_parameters(OggVorbis_File *vf) { @@ -86,7 +88,6 @@ vi = ov_info(vf, -1); if (vi == NULL) { - //ci->splash(HZ*2, "Vorbis Error"); return false; } @@ -120,27 +121,23 @@ ogg_int64_t vf_pcmlengths[2]; ci->configure(DSP_SET_SAMPLE_DEPTH, 24); - /* Note: These are sane defaults for these values. Perhaps - * they should be set differently based on quality setting - */ -#if defined(CPU_ARM) || defined(CPU_COLDFIRE) - if (setjmp(rb_jump_buf) != 0) - { + if (codec_init()) { + error = CODEC_ERROR; + goto exit; + } + + ogg_malloc_init(); + +#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS) + if (setjmp(rb_jump_buf) != 0) { /* malloc failed; skip to next track */ error = CODEC_ERROR; goto done; } #endif -/* We need to flush reserver memory every track load. */ next_track: - if (codec_init()) { - error = CODEC_ERROR; - goto exit; - } - ogg_malloc_init(); - while (!*ci->taginfo_ready && !ci->stop_codec) ci->sleep(1); @@ -166,6 +163,10 @@ * get here. */ if (!error) { + ogg_free(vf.offsets); + ogg_free(vf.dataoffsets); + ogg_free(vf.serialnos); + vf.offsets = vf_offsets; vf.dataoffsets = &vf_dataoffsets; vf.serialnos = &vf_serialnos; @@ -183,7 +184,7 @@ vf.ready_state = OPENED; vf.links = 1; } else { - //ci->logf("ov_open: %d", error); + DEBUGF("Vorbis: ov_open failed: %d", error); error = CODEC_ERROR; goto done; } @@ -225,7 +226,7 @@ if (n == 0) { eof = 1; } else if (n < 0) { - DEBUGF("Error decoding frame\n"); + DEBUGF("Vorbis: Error decoding frame\n"); } else { ci->pcmbuf_insert(pcm[0], pcm[1], n); ci->set_offset(ov_raw_tell(&vf)); @@ -235,6 +236,15 @@ error = CODEC_OK; done: +#if defined(SIMULATOR) + { + size_t bufsize; + void* buf = ci->codec_get_buffer(&bufsize); + + DEBUGF("Vorbis: Memory max: %u\n", get_max_size(buf)); + } +#endif + if (ci->request_next_track()) { /* Clean things up for the next track */ vf.dataoffsets = NULL; @@ -246,6 +256,6 @@ } exit: + ogg_malloc_destroy(); return error; } - Index: apps/codecs/libtremor/os_types.h =================================================================== --- apps/codecs/libtremor/os_types.h (revision 22075) +++ apps/codecs/libtremor/os_types.h (working copy) @@ -38,16 +38,14 @@ #define _ogg_malloc ogg_malloc #define _ogg_calloc ogg_calloc #define _ogg_realloc ogg_realloc -#define _ogg_free(x) do { } while(0) +#define _ogg_free ogg_free void ogg_malloc_init(void); +void ogg_malloc_destroy(void); void *ogg_malloc(size_t size); -void *ogg_tmpmalloc(size_t size); void *ogg_calloc(size_t nmemb, size_t size); -void *ogg_tmpcalloc(size_t nmemb, size_t size); void *ogg_realloc(void *ptr, size_t size); -long ogg_tmpmalloc_pos(void); -void ogg_tmpmalloc_free(long pos); +void ogg_free(void *ptr); void iram_malloc_init(void); void *iram_malloc(size_t size); Index: apps/codecs/libtremor/SOURCES =================================================================== --- apps/codecs/libtremor/SOURCES (revision 22075) +++ apps/codecs/libtremor/SOURCES (working copy) @@ -14,3 +14,4 @@ window.c ctype.c oggmalloc.c +tlsf/tlsf.c Index: apps/codecs/libtremor/oggmalloc.c =================================================================== --- apps/codecs/libtremor/oggmalloc.c (revision 22075) +++ apps/codecs/libtremor/oggmalloc.c (working copy) @@ -1,87 +1,65 @@ #include "os_types.h" +#include "tlsf/tlsf.h" -#if defined(CPU_ARM) || defined(CPU_COLDFIRE) +#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS) #include extern jmp_buf rb_jump_buf; +#define LONGJMP(x) longjmp(rb_jump_buf, x) +#elif defined(SIMULATOR) +#define LONGJMP(x) do { DEBUGF("Vorbis: allocation failed!\n"); return NULL; } while (false) +#else +#define LONGJMP(x) return NULL #endif -static size_t tmp_ptr; - void ogg_malloc_init(void) { - mallocbuf = ci->codec_get_buffer(&bufsize); - tmp_ptr = bufsize & ~3; - mem_ptr = 0; + size_t bufsize; + void* buf = ci->codec_get_buffer(&bufsize); + init_memory_pool(bufsize, buf); } -void *ogg_malloc(size_t size) +void ogg_malloc_destroy() { - void* x; - - size = (size + 3) & ~3; - - if (mem_ptr + size > tmp_ptr) -#if defined(CPU_ARM) || defined(CPU_COLDFIRE) - longjmp(rb_jump_buf, 1); -#else - return NULL; -#endif - - x = &mallocbuf[mem_ptr]; - mem_ptr += size; /* Keep memory 32-bit aligned */ - - return x; + size_t bufsize; + void* buf = ci->codec_get_buffer(&bufsize); + destroy_memory_pool(buf); } -void *ogg_tmpmalloc(size_t size) +void *ogg_malloc(size_t size) { - size = (size + 3) & ~3; + void* x = tlsf_malloc(size); - if (mem_ptr + size > tmp_ptr) - return NULL; + if (x == NULL) + LONGJMP(1); - tmp_ptr -= size; - return &mallocbuf[tmp_ptr]; + return x; } void *ogg_calloc(size_t nmemb, size_t size) { - void *x; - x = ogg_malloc(nmemb * size); - if (x == NULL) - return NULL; - ci->memset(x, 0, nmemb * size); - return x; -} + void *x = tlsf_calloc(nmemb, size); -void *ogg_tmpcalloc(size_t nmemb, size_t size) -{ - void *x; - x = ogg_tmpmalloc(nmemb * size); if (x == NULL) - return NULL; - ci->memset(x, 0, nmemb * size); + LONGJMP(1); + return x; } void *ogg_realloc(void *ptr, size_t size) { - void *x; - (void)ptr; - x = ogg_malloc(size); + void *x = tlsf_realloc(ptr, size); + + if (x == NULL) + LONGJMP(1); + return x; } -long ogg_tmpmalloc_pos(void) +void ogg_free(void* ptr) { - return tmp_ptr; + tlsf_free(ptr); } -void ogg_tmpmalloc_free(long pos) -{ - tmp_ptr = pos; -} - /* Allocate IRAM buffer */ static unsigned char iram_buff[IRAM_IBSS_SIZE] IBSS_ATTR __attribute__ ((aligned (16))); static size_t iram_remain; Index: apps/codecs/libtremor/sharedbook.c =================================================================== --- apps/codecs/libtremor/sharedbook.c (revision 22075) +++ apps/codecs/libtremor/sharedbook.c (working copy) @@ -71,7 +71,7 @@ static ogg_uint32_t *_make_words(long *l,long n,long sparsecount){ long i,j,count=0; ogg_uint32_t marker[33]; - ogg_uint32_t *r=(ogg_uint32_t *)ogg_tmpmalloc((sparsecount?sparsecount:n)*sizeof(*r)); + ogg_uint32_t *r=(ogg_uint32_t *)_ogg_malloc((sparsecount?sparsecount:n)*sizeof(*r)); memset(marker,0,sizeof(marker)); for(i=0;i>length)){ /* error condition; the lengths must specify an overpopulated tree */ - /* _ogg_free(r); */ + _ogg_free(r); return(NULL); } r[count++]=entry; @@ -188,9 +188,8 @@ ogg_int32_t mindel=_float32_unpack(b->q_min,&minpoint); ogg_int32_t delta=_float32_unpack(b->q_delta,&delpoint); ogg_int32_t *r=(ogg_int32_t *)_ogg_calloc(n*b->dim,sizeof(*r)); - int *rp=(int *)ogg_tmpcalloc(n*b->dim,sizeof(*rp)); + int *rp=(int *)_ogg_calloc(n*b->dim,sizeof(*rp)); - memset(rp, 0, n*b->dim*sizeof(*rp)); *maxpoint=minpoint; /* maptype 1 and 2 both use a quantized value vector, but @@ -277,7 +276,7 @@ if(rp[j]<*maxpoint) r[j]>>=*maxpoint-rp[j]; - /* _ogg_free(rp); */ + _ogg_free(rp); return(r); } return(NULL); @@ -325,7 +324,6 @@ int vorbis_book_init_decode(codebook *c,const static_codebook *s){ int i,j,n=0,tabn; int *sortindex; - long pos = ogg_tmpmalloc_pos(); memset(c,0,sizeof(*c)); /* count actually used entries */ @@ -350,9 +348,13 @@ /* perform sort */ ogg_uint32_t *codes=_make_words(s->lengthlist,s->entries,c->used_entries); - ogg_uint32_t **codep=(ogg_uint32_t **)ogg_tmpmalloc(sizeof(*codep)*n); + ogg_uint32_t **codep=(ogg_uint32_t **)_ogg_malloc(sizeof(*codep)*n); - if(codes==NULL||codep==NULL)goto err_out; + if(codes==NULL||codep==NULL){ + _ogg_free(codep); + _ogg_free(codes); + goto err_out; + } for(i=0;icodelist=(ogg_uint32_t *)_ogg_malloc(n*sizeof(*c->codelist)); /* the index is a reverse index */ for(i=0;icodelist[sortindex[i]]=codes[i]; - /* _ogg_free(codes); */ + _ogg_free(codep); + _ogg_free(codes); @@ -387,6 +390,7 @@ if(s->lengthlist[i]>0) c->dec_codelengths[sortindex[n++]]=s->lengthlist[i]; + _ogg_free(sortindex); c->dec_firsttablen=_ilog(c->used_entries)-4; /* this is magic */ if(c->dec_firsttablen<5)c->dec_firsttablen=5; if(c->dec_firsttablen>8)c->dec_firsttablen=8; @@ -434,10 +438,8 @@ } } - ogg_tmpmalloc_free(pos); return(0); err_out: - ogg_tmpmalloc_free(pos); vorbis_book_clear(c); return(-1); } Index: apps/codecs/libtremor/libtremor.make =================================================================== --- apps/codecs/libtremor/libtremor.make (revision 22075) +++ apps/codecs/libtremor/libtremor.make (working copy) @@ -32,6 +32,10 @@ TREMORFLAGS += -O2 endif +ifdef SIMVER + TREMORFLAGS += -DTLSF_STATISTIC=1 +endif + $(CODECDIR)/libtremor/%.o: $(ROOTDIR)/apps/codecs/libtremor/%.c $(SILENT)mkdir -p $(dir $@) $(call PRINTS,CC $(subst $(ROOTDIR)/,,$<))$(CC) $(TREMORFLAGS) -c $< -o $@ Index: apps/codecs/libtremor/block.c =================================================================== --- apps/codecs/libtremor/block.c (revision 22075) +++ apps/codecs/libtremor/block.c (working copy) @@ -283,7 +283,7 @@ codec_setup_info *ci=(codec_setup_info *)(vi?vi->codec_setup:NULL); private_state *b=(private_state *)v->backend_state; - if(NULL == v->iram_double_pcm) + if(NULL == v->iram_double_pcm && vi != NULL) { /* pcm buffer came from oggmalloc rather than iram */ for(i=0;ichannels;i++)