From 3f61caa0cd7e818416be08778f356efd54e596fb Mon Sep 17 00:00:00 2001 From: Nils Wallménius Date: Sun, 6 May 2012 12:22:09 +0200 Subject: rbcodec: abstract tdspeed buffer allocation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move code dealing with rockbox specific buflib allocations into a rockbox specific file and implement buffer allocation with malloc/free for warble/stand alone lib. Based on patch by Sean Bartell. Change-Id: I8cb85dad5890fbd34c1bb26abbb89c0b0f6b55cf Reviewed-on: http://gerrit.rockbox.org/144 Tested-by: Nils Wallménius Reviewed-by: Michael Sevakis Reviewed-by: Nils Wallménius --- lib/rbcodec/dsp/tdspeed.c | 148 +++++++++++-------------------------- lib/rbcodec/dsp/tdspeed.h | 1 + lib/rbcodec/rbcodecplatform-unix.h | 23 ++++++ lib/rbcodec/test/SOURCES | 2 - lib/rbcodec/test/warble.c | 2 - 5 files changed, 66 insertions(+), 110 deletions(-) (limited to 'lib/rbcodec') diff --git a/lib/rbcodec/dsp/tdspeed.c b/lib/rbcodec/dsp/tdspeed.c index 82b6b0ea95..ea1013e049 100644 --- a/lib/rbcodec/dsp/tdspeed.c +++ b/lib/rbcodec/dsp/tdspeed.c @@ -20,15 +20,17 @@ * KIND, either express or implied. * ****************************************************************************/ +#include "platform.h" #include "config.h" -#include "system.h" #include "sound.h" #include "core_alloc.h" #include "dsp-util.h" #include "dsp_proc_entry.h" #include "tdspeed.h" +#ifndef assert #define assert(cond) +#endif #define TIMESTRETCH_SET_FACTOR (DSP_PROC_SETTING+DSP_PROC_TIMESTRETCH) @@ -39,6 +41,7 @@ #define MAX_INPUTCOUNT 512 /* Max input count so dst doesn't overflow */ #define FIXED_BUFCOUNT 3072 /* 48KHz factor 3.0 */ #define FIXED_OUTBUFCOUNT 4096 +#define NBUFFERS 4 enum tdspeed_ops { @@ -64,8 +67,13 @@ static struct tdspeed_state_s int32_t *ovl_buff[2]; /* overlap buffer (L+R) */ } tdspeed_state; -static int handles[4] = { 0, 0, 0, 0 }; -static int32_t *buffers[4] = { NULL, NULL, NULL, NULL }; +static int32_t *buffers[NBUFFERS] = { NULL, NULL, NULL, NULL }; +static const int buffer_sizes[NBUFFERS] = { + FIXED_BUFCOUNT * sizeof(int32_t), + FIXED_BUFCOUNT * sizeof(int32_t), + FIXED_OUTBUFCOUNT * sizeof(int32_t), + FIXED_OUTBUFCOUNT * sizeof(int32_t) +}; #define overlap_buffer (&buffers[0]) #define outbuf (&buffers[2]) @@ -74,107 +82,6 @@ static int32_t *buffers[4] = { NULL, NULL, NULL, NULL }; /* Processed buffer passed out to later stages */ static struct dsp_buffer dsp_outbuf; -static int move_callback(int handle, void *current, void *new) -{ -#if 0 - /* Should not currently need to block this since DSP loop completes an - iteration before yielding and begins again at its input buffer */ - if (dsp_is_busy(tdspeed_state.dsp)) - return BUFLIB_CB_CANNOT_MOVE; /* DSP processing in progress */ -#endif - - ptrdiff_t shift = (int32_t *)new - (int32_t *)current; - int32_t **p32 = dsp_outbuf.p32; - - for (unsigned int i = 0; i < ARRAYLEN(handles); i++) - { - if (handle != handles[i]) - continue; - - switch (i) - { - case 0: case 1: - /* moving overlap (input) buffers */ - tdspeed_state.ovl_buff[i] = new; - break; - - case 2: - /* moving outbuf left channel and dsp_outbuf.p32[0] */ - if (p32[0] == p32[1]) - p32[1] += shift; /* mono mode */ - - p32[0] += shift; - break; - - case 3: - /* moving outbuf right channel and dsp_outbuf.p32[1] */ - p32[1] += shift; - break; - } - - buffers[i] = new; - break; - } - - return BUFLIB_CB_OK; -} - -static struct buflib_callbacks ops = -{ - .move_callback = move_callback, - .shrink_callback = NULL, -}; - -/* Allocate timestretch buffers */ -static bool tdspeed_alloc_buffers(void) -{ - static const struct - { - const char *name; - size_t size; - } bufdefs[4] = - { - { "tdspeed ovl L", FIXED_BUFCOUNT * sizeof(int32_t) }, - { "tdspeed ovl R", FIXED_BUFCOUNT * sizeof(int32_t) }, - { "tdspeed out L", FIXED_OUTBUFCOUNT * sizeof(int32_t) }, - { "tdspeed out R", FIXED_OUTBUFCOUNT * sizeof(int32_t) }, - }; - - for (unsigned int i = 0; i < ARRAYLEN(bufdefs); i++) - { - if (handles[i] <= 0) - { - handles[i] = core_alloc_ex(bufdefs[i].name, bufdefs[i].size, &ops); - - if (handles[i] <= 0) - return false; - } - - if (buffers[i] == NULL) - { - buffers[i] = core_get_data(handles[i]); - - if (buffers[i] == NULL) - return false; - } - } - - return true; -} - -/* Free timestretch buffers */ -static void tdspeed_free_buffers(void) -{ - for (unsigned int i = 0; i < ARRAYLEN(handles); i++) - { - if (handles[i] > 0) - core_free(handles[i]); - - handles[i] = 0; - buffers[i] = NULL; - } -} - /* Discard all data */ static void tdspeed_flush(void) { @@ -650,7 +557,7 @@ static intptr_t tdspeed_configure(struct dsp_proc_entry *this, break; case DSP_PROC_INIT: - if (!tdspeed_alloc_buffers()) + if (!tdspeed_alloc_buffers(buffers, buffer_sizes, NBUFFERS)) return -1; /* fail the init */ st->this = this; @@ -664,7 +571,7 @@ static intptr_t tdspeed_configure(struct dsp_proc_entry *this, st->this = NULL; st->factor = PITCH_SPEED_100; dsp_outbuf.remcount = 0; - tdspeed_free_buffers(); + tdspeed_free_buffers(buffers, NBUFFERS); break; case TIMESTRETCH_SET_FACTOR: @@ -680,6 +587,35 @@ static intptr_t tdspeed_configure(struct dsp_proc_entry *this, (void)value; } +void tdspeed_move(int i, void* current, void* new) +{ + ptrdiff_t shift = (int32_t *)new - (int32_t *)current; + int32_t **p32 = dsp_outbuf.p32; + + switch (i) + { + case 0: case 1: + /* moving overlap (input) buffers */ + tdspeed_state.ovl_buff[i] = new; + break; + + case 2: + /* moving outbuf left channel and dsp_outbuf.p32[0] */ + if (p32[0] == p32[1]) + p32[1] += shift; /* mono mode */ + + p32[0] += shift; + break; + + case 3: + /* moving outbuf right channel and dsp_outbuf.p32[1] */ + p32[1] += shift; + break; + } + + buffers[i] = new; +} + /* Database entry */ DSP_PROC_DB_ENTRY(TIMESTRETCH, tdspeed_configure); diff --git a/lib/rbcodec/dsp/tdspeed.h b/lib/rbcodec/dsp/tdspeed.h index ca8a7846a4..2949c1bee9 100644 --- a/lib/rbcodec/dsp/tdspeed.h +++ b/lib/rbcodec/dsp/tdspeed.h @@ -39,5 +39,6 @@ void dsp_timestretch_enable(bool enable); void dsp_set_timestretch(int32_t percent); int32_t dsp_get_timestretch(void); bool dsp_timestretch_available(void); +void tdspeed_move(int i, void* current, void* new); #endif /* _TDSPEED_H */ diff --git a/lib/rbcodec/rbcodecplatform-unix.h b/lib/rbcodec/rbcodecplatform-unix.h index c9c8a29642..5e65be3ae4 100644 --- a/lib/rbcodec/rbcodecplatform-unix.h +++ b/lib/rbcodec/rbcodecplatform-unix.h @@ -74,4 +74,27 @@ static inline off_t filesize(int fd) { #endif #endif */ + +static inline bool tdspeed_alloc_buffers(int32_t **buffers, + const int *buf_s, int nbuf) +{ + int i; + for (i = 0; i < nbuf; i++) + { + buffers[i] = malloc(buf_s[i]); + if (!buffers[i]) + return false; + } + return true; +} + +static inline void tdspeed_free_buffers(int32_t **buffers, int nbuf) +{ + int i; + for (i = 0; i < nbuf; i++) + { + free(buffers[i]); + } +} + #endif diff --git a/lib/rbcodec/test/SOURCES b/lib/rbcodec/test/SOURCES index 4c0d906dc5..a89af238ca 100644 --- a/lib/rbcodec/test/SOURCES +++ b/lib/rbcodec/test/SOURCES @@ -1,6 +1,4 @@ warble.c -../../../firmware/buflib.c -../../../firmware/core_alloc.c ../../../firmware/common/strlcpy.c ../../../firmware/common/unicode.c ../../../firmware/common/structec.c diff --git a/lib/rbcodec/test/warble.c b/lib/rbcodec/test/warble.c index ea8efcffca..d5bc1c5aea 100644 --- a/lib/rbcodec/test/warble.c +++ b/lib/rbcodec/test/warble.c @@ -33,7 +33,6 @@ #include #include "buffering.h" /* TYPE_PACKET_AUDIO */ #include "codecs.h" -#include "core_alloc.h" /* core_allocator_init */ #include "dsp_core.h" #include "metadata.h" #include "settings.h" @@ -851,7 +850,6 @@ int main(int argc, char **argv) } } - core_allocator_init(); if (argc == optind + 2) { write_init(argv[optind + 1]); } else if (argc == optind + 1) { -- cgit v1.2.3