Skip to content

Commit

Permalink
Use statically allocated memory for mSBC buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Feb 17, 2024
1 parent d5943c2 commit 6d5cd0c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov-report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
working-directory: ${{ github.workspace }}/build
run: make cov
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: build/lcov.info
24 changes: 6 additions & 18 deletions src/codec-msbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,13 @@ int msbc_init(struct esco_msbc *msbc) {

if (!msbc->initialized) {
debug("Initializing mSBC codec");
if (ffb_init_uint8_t(&msbc->data, sizeof(esco_msbc_frame_t) * 3) == -1)
goto fail_init;
/* Allocate buffer for 1 decoded frame, optional 3 PLC frames and
* some extra frames to account for async PCM samples reading. */
if (ffb_init_int16_t(&msbc->pcm, MSBC_CODESAMPLES * 6) == -1)
goto fail_init;
if ((errno = -sbc_init_msbc(&msbc->sbc, 0)) != 0)
goto fail_init;
return -errno;
}
else {
debug("Re-initializing mSBC codec");
if ((errno = -sbc_reinit_msbc(&msbc->sbc, 0)) != 0)
goto fail_init;
return -errno;
}

/* ensure libsbc uses little-endian PCM on all architectures */
Expand All @@ -79,8 +73,8 @@ int msbc_init(struct esco_msbc *msbc) {
}
#endif

ffb_rewind(&msbc->data);
ffb_rewind(&msbc->pcm);
ffb_init_from_array(&msbc->data, msbc->buffer_data);
ffb_init_from_array(&msbc->pcm, msbc->buffer_pcm);

msbc->seq_initialized = false;
msbc->seq_number = 0;
Expand All @@ -96,9 +90,6 @@ int msbc_init(struct esco_msbc *msbc) {

fail:
sbc_finish(&msbc->sbc);
fail_init:
ffb_free(&msbc->data);
ffb_free(&msbc->pcm);
return -errno;
}

Expand All @@ -109,9 +100,6 @@ void msbc_finish(struct esco_msbc *msbc) {

sbc_finish(&msbc->sbc);

ffb_free(&msbc->data);
ffb_free(&msbc->pcm);

plc_free(msbc->plc);
msbc->plc = NULL;

Expand All @@ -130,7 +118,7 @@ ssize_t msbc_decode(struct esco_msbc *msbc) {
ssize_t rv = 0;

const size_t tmp = input_len;
const esco_msbc_frame_t *frame = h2_header_find(input, &input_len);
const h2_msbc_frame_t *frame = h2_header_find(input, &input_len);
input += tmp - input_len;

/* Skip decoding if there is not enough input data or the output
Expand Down Expand Up @@ -206,7 +194,7 @@ ssize_t msbc_encode(struct esco_msbc *msbc) {

const int16_t *input = msbc->pcm.data;
const size_t input_len = ffb_blen_out(&msbc->pcm);
esco_msbc_frame_t *frame = (esco_msbc_frame_t *)msbc->data.tail;
h2_msbc_frame_t *frame = msbc->data.tail;
size_t output_len = ffb_blen_in(&msbc->data);

/* Skip encoding if there is not enough PCM samples or the output
Expand Down
11 changes: 9 additions & 2 deletions src/codec-msbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
#define MSBC_CODESAMPLES (MSBC_CODESIZE / sizeof(int16_t))
#define MSBC_FRAMELEN 57

typedef struct esco_msbc_frame {
typedef struct h2_msbc_frame {
h2_header_t header;
uint8_t payload[MSBC_FRAMELEN];
uint8_t padding;
} __attribute__ ((packed)) esco_msbc_frame_t;
} __attribute__ ((packed)) h2_msbc_frame_t;

struct esco_msbc {

Expand All @@ -61,6 +61,13 @@ struct esco_msbc {
* used for reinitialization - it makes msbc_init() idempotent. */
bool initialized;

/* Allocated buffer for 3 mSBC frames to have some extra space in case of
* PCM samples asynchronous reading beeing slower than incoming frames. */
uint8_t buffer_data[sizeof(h2_msbc_frame_t) * 3];
/* Allocate buffer for 1 decoded frame, optional 3 PLC frames and
* some extra frames to account for async PCM samples reading. */
int16_t buffer_pcm[MSBC_CODESAMPLES * 6];

};

int msbc_init(struct esco_msbc *msbc);
Expand Down
9 changes: 8 additions & 1 deletion src/shared/ffb.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - ffb.h
* Copyright (c) 2016-2020 Arkadiusz Bokowy
* Copyright (c) 2016-2024 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand Down Expand Up @@ -34,6 +34,13 @@ void ffb_free(ffb_t *ffb);
#define ffb_init_int16_t(p, n) ffb_init(p, n, sizeof(int16_t))
#define ffb_init_int32_t(p, n) ffb_init(p, n, sizeof(int32_t))

/**
* Initialize the FIFO-like buffer from statically allocated array. */
#define ffb_init_from_array(p, array) ( \
(p)->data = (p)->tail = (array), \
(p)->nmemb = sizeof(array) / sizeof(*(array)), \
(p)->size = sizeof(*(array)))

/**
* Get number of unite blocks available for writing. */
#define ffb_len_in(p) (ffb_blen_in(p) / (p)->size)
Expand Down
16 changes: 16 additions & 0 deletions test/test-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "hci.h"
#include "utils.h"
#include "shared/defs.h"
#include "shared/ffb.h"
#include "shared/hex.h"
#include "shared/nv.h"
Expand Down Expand Up @@ -228,6 +229,20 @@ CK_START_TEST(test_ffb) {

} CK_END_TEST

CK_START_TEST(test_ffb_static) {

ffb_t ffb = { 0 };
uint32_t buffer[64];

ffb_init_from_array(&ffb, buffer);

ck_assert_ptr_eq(ffb.data, buffer);
ck_assert_ptr_eq(ffb.tail, buffer);
ck_assert_uint_eq(ffb.nmemb, ARRAYSIZE(buffer));
ck_assert_uint_eq(ffb.size, 4);

} CK_END_TEST

CK_START_TEST(test_ffb_resize) {

const char *data = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Expand Down Expand Up @@ -287,6 +302,7 @@ int main(void) {

/* shared/ffb.c */
tcase_add_test(tc, test_ffb);
tcase_add_test(tc, test_ffb_static);
tcase_add_test(tc, test_ffb_resize);

/* shared/hex.c */
Expand Down

0 comments on commit 6d5cd0c

Please sign in to comment.