Skip to content

Commit

Permalink
Allow circular buffer to report disjoint buffer
Browse files Browse the repository at this point in the history
This allows to avoid memmove after each consume_cbuff_data.
Some extra care was necessary to round queue transactions to a multiple
of the sample size.

I also did some refactoring on the cbuff to allow it to grow and on the
resamplers to allw them to report not only how many bytes were consumed,
but also how many bytes were produced, so we can move cbuff pointers
accordingly.

tail/head methods reports the available sizes in 2 parameters :
"available" for the first chunk size, and "extra" for the optional
second chunk (which starts at the buffer beginning).
It's up to the caller to split it's contiguous accesses around these two
chunks.

Another method that I initially wanted to try was the virtual memory
trick to mirror the beggining of the buffer at the end of itself. But
this trick requires some code specific platform and can be tricky when
we need to grow the buffer. So I went this explicit 2 chunk approach
which is simpler and doesn't require platform specific code.
  • Loading branch information
bsmiles32 committed Jan 18, 2021
1 parent 393b1c1 commit 1c76454
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 90 deletions.
65 changes: 56 additions & 9 deletions src/circular_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,83 @@ void release_cbuff(struct circular_buffer* cbuff)
memset(cbuff, 0, sizeof(*cbuff));
}

void grow_cbuff(struct circular_buffer* cbuff, size_t new_size)
{
if (new_size >= cbuff->size) {

cbuff->data = realloc(cbuff->data, new_size);

size_t delta = new_size - cbuff->size;
size_t delta_max = (cbuff->head >= cbuff->tail) ? cbuff->tail : cbuff->head;

void* cbuff_head(const struct circular_buffer* cbuff, size_t* available)
if (delta_max <= delta) {
memcpy((unsigned char*)cbuff->data + cbuff->size, cbuff->data, delta_max);
memset((unsigned char*)cbuff->data + cbuff->size + delta_max, 0, delta - delta_max);
}
else {
memcpy((unsigned char*)cbuff->data + cbuff->size, cbuff->data, delta);
memmove(cbuff->data, (const unsigned char*)cbuff->data + delta, delta_max - delta);
}

cbuff->size = new_size;
}
}


void* cbuff_head(const struct circular_buffer* cbuff, size_t* available, size_t* extra)
{
assert(cbuff->head <= cbuff->size);
assert(cbuff->tail <= cbuff->size);

if (cbuff->head >= cbuff->tail) {
*available = cbuff->size - cbuff->head;
*extra = cbuff->tail;
}
else {
*available = cbuff->tail - cbuff->head;
*extra = 0;
}

*available = cbuff->size - cbuff->head;
return (unsigned char*)cbuff->data + cbuff->head;
}


void* cbuff_tail(const struct circular_buffer* cbuff, size_t* available)
void* cbuff_tail(const struct circular_buffer* cbuff, size_t* available, size_t* extra)
{
*available = cbuff->head;
return cbuff->data;
assert(cbuff->head <= cbuff->size);
assert(cbuff->tail <= cbuff->size);

if (cbuff->head >= cbuff->tail) {
*available = cbuff->head - cbuff->tail;
*extra = 0;
}
else {
*available = cbuff->size - cbuff->tail;
*extra = cbuff->head;
}

return (unsigned char*)cbuff->data + cbuff->tail;
}


void produce_cbuff_data(struct circular_buffer* cbuff, size_t amount)
{
assert(cbuff->head + amount <= cbuff->size);
//assert((cbuff->head + amount) % cbuff->size <= cbuff->tail);

cbuff->head += amount;
if (cbuff->head > cbuff->size) {
cbuff->head -= cbuff->size;
}
}


void consume_cbuff_data(struct circular_buffer* cbuff, size_t amount)
{
assert(cbuff->head >= amount);
//assert((cbuff->tail + amount) % cbuff->size <= cbuff->tail);

memmove(cbuff->data, (unsigned char*)cbuff->data + amount, cbuff->head - amount);
cbuff->head -= amount;
cbuff->tail += amount;
if (cbuff->tail > cbuff->size) {
cbuff->tail -= cbuff->size;
}
}

7 changes: 5 additions & 2 deletions src/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ struct circular_buffer
void* data;
size_t size;
size_t head;
size_t tail;
};

int init_cbuff(struct circular_buffer* cbuff, size_t capacity);

void release_cbuff(struct circular_buffer* cbuff);

void* cbuff_head(const struct circular_buffer* cbuff, size_t* available);
void grow_cbuff(struct circular_buffer* cbuff, size_t new_size);

void* cbuff_tail(const struct circular_buffer* cbuff, size_t* available);
void* cbuff_head(const struct circular_buffer* cbuff, size_t* available, size_t* extra);

void* cbuff_tail(const struct circular_buffer* cbuff, size_t* available, size_t* extra);

void produce_cbuff_data(struct circular_buffer* cbuff, size_t amount);

Expand Down
16 changes: 6 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,27 +381,23 @@ EXPORT void CALL SetSpeedFactor(int percentage)
sdl_set_speed_factor(l_sdl_backend, percentage);
}

size_t ResampleAndMix(void* resampler, const struct resampler_interface* iresampler,
void* mix_buffer,
const void* src, size_t src_size, unsigned int src_freq,
void* dst, size_t dst_size, unsigned int dst_freq)
void ResampleAndMix(void* resampler, const struct resampler_interface* iresampler,
void* mix_buffer,
const void* src, size_t src_size, unsigned int src_freq, size_t* consumed,
void* dst, size_t dst_size, unsigned int dst_freq, size_t* produced)
{
size_t consumed;

#if defined(HAS_OSS_SUPPORT)
if (VolumeControlType == VOLUME_TYPE_OSS)
{
consumed = iresampler->resample(resampler, src, src_size, src_freq, dst, dst_size, dst_freq);
iresampler->resample(resampler, src, src_size, src_freq, consumed, dst, dst_size, dst_freq, produced);
}
else
#endif
{
consumed = iresampler->resample(resampler, src, src_size, src_freq, mix_buffer, dst_size, dst_freq);
iresampler->resample(resampler, src, src_size, src_freq, consumed, mix_buffer, dst_size, dst_freq, produced);
memset(dst, 0, dst_size);
SDL_MixAudio(dst, mix_buffer, dst_size, VolSDL);
}

return consumed;
}

void SetPlaybackVolume(void)
Expand Down
8 changes: 4 additions & 4 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ enum {

void SetPlaybackVolume(void);

size_t ResampleAndMix(void* resampler, const struct resampler_interface* iresampler,
void* mix_buffer,
const void* src, size_t src_size, unsigned int src_freq,
void* dst, size_t dst_size, unsigned int dst_freq);
void ResampleAndMix(void* resampler, const struct resampler_interface* iresampler,
void* mix_buffer,
const void* src, size_t src_size, unsigned int src_freq, size_t* consumed,
void* dst, size_t dst_size, unsigned int dst_freq, size_t* produced);

/* declarations of pointers to Core config functions */
extern ptr_ConfigListSections ConfigListSections;
Expand Down
6 changes: 3 additions & 3 deletions src/resamplers/resamplers.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ struct resampler_interface

void (*release)(void* resampler);

size_t (*resample)(void* resampler,
const void* src, size_t src_size, unsigned int src_freq,
void* dst, size_t dst_size, unsigned int dst_freq);
void (*resample)(void* resampler,
const void* src, size_t src_size, unsigned int src_freq, size_t* consumed,
void* dst, size_t dst_size, unsigned int dst_freq, size_t* produced);
};

const struct resampler_interface* get_iresampler(const char* resampler_id, void** resampler);
Expand Down
19 changes: 8 additions & 11 deletions src/resamplers/speex.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ static void speex_release(void* resampler)
speex_resampler_destroy(spx_state);
}

static size_t speex_resample(void* resampler,
const void* src, size_t src_size, unsigned int src_freq,
void* dst, size_t dst_size, unsigned int dst_freq)
static void speex_resample(void* resampler,
const void* src, size_t src_size, unsigned int src_freq, size_t* consumed,
void* dst, size_t dst_size, unsigned int dst_freq, size_t* produced)
{
SpeexResamplerState* spx_state = (SpeexResamplerState*)resampler;

Expand All @@ -111,17 +111,14 @@ static size_t speex_resample(void* resampler,
if (error != RESAMPLER_ERR_SUCCESS)
{
DebugMessage(M64MSG_ERROR, "Speex error: %s", speex_resampler_strerror(error));
*consumed = src_size;
*produced = dst_size;
memset(dst, 0, dst_size);
return src_size;
}

if (dst_size != out_len * BYTES_PER_SAMPLE) {
DebugMessage(M64MSG_WARNING, "dst_size = %u != outlen*4 = %u",
(uint32_t) dst_size, out_len * BYTES_PER_SAMPLE);
return;
}
memset((char*)dst + out_len * BYTES_PER_SAMPLE, 0, dst_size - out_len * BYTES_PER_SAMPLE);

return in_len * BYTES_PER_SAMPLE;
*consumed = in_len * BYTES_PER_SAMPLE;
*produced = out_len * BYTES_PER_SAMPLE;
}


Expand Down
19 changes: 8 additions & 11 deletions src/resamplers/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ static void src_release(void* resampler)
}
}

static size_t src_resample(void* resampler,
const void* src, size_t src_size, unsigned int src_freq,
void* dst, size_t dst_size, unsigned int dst_freq)
static void src_resample(void* resampler,
const void* src, size_t src_size, unsigned int src_freq, size_t* consumed,
void* dst, size_t dst_size, unsigned int dst_freq, size_t* produced)
{
struct src_resampler* src_resampler = (struct src_resampler*)resampler;

Expand Down Expand Up @@ -176,19 +176,16 @@ static size_t src_resample(void* resampler,
if (error)
{
DebugMessage(M64MSG_ERROR, "SRC error: %s", src_strerror(error));
*consumed = src_size;
*produced = dst_size;
memset(dst, 0, dst_size);
return src_size;
}

if (dst_size != src_data.output_frames_gen*4) {
DebugMessage(M64MSG_WARNING, "dst_size = %u != output_frames_gen*4 = %u",
(uint32_t) dst_size, (uint32_t) src_data.output_frames_gen*4);
return;
}

src_float_to_short_array(src_resampler->fbuffers[1].data, (short*)dst, src_data.output_frames_gen*2);
memset((char*)dst + src_data.output_frames_gen*4, 0, dst_size - src_data.output_frames_gen*4);

return src_data.input_frames_used * 4;
*consumed = src_data.input_frames_used * 4;
*produced = src_data.output_frames_gen * 4;
}


Expand Down
9 changes: 5 additions & 4 deletions src/resamplers/trivial.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ static void trivial_release(void* resampler)
/* nothing to do */
}

static size_t trivial_resample(void* resampler,
const void* src, size_t src_size, unsigned int src_freq,
void* dst, size_t dst_size, unsigned int dst_freq)
static void trivial_resample(void* resampler,
const void* src, size_t src_size, unsigned int src_freq, size_t* consumed,
void* dst, size_t dst_size, unsigned int dst_freq, size_t* produced)
{
enum { BYTES_PER_SAMPLE = 4 };
size_t i;
Expand Down Expand Up @@ -71,7 +71,8 @@ static size_t trivial_resample(void* resampler,
}
}

return j * 4;
*consumed = j * 4;
*produced = i * 4;
}


Expand Down

0 comments on commit 1c76454

Please sign in to comment.