diff mbox series

sbc: Fix mSBC reinitialization

Message ID 20241211155905.1312473-1-arkadiusz.bokowy@gmail.com
State New
Headers show
Series sbc: Fix mSBC reinitialization | expand

Commit Message

Arkadiusz Bokowy Dec. 11, 2024, 3:59 p.m. UTC
Commit 10310fb has introduced a function for mSBC reinitialization.
However, it did not set msbc private flag, which is checked e.g. by
sbc_set_defaults() and sbc_get_frame_length() functions.

This commit fixes this, so now after reinitialization this library
properly packs/unpacks mSBC frames and the sbc_get_frame_length()
function returns correct frame length.
---
 sbc/sbc.c | 71 ++++++++++++++++++++++++++-----------------------------
 1 file changed, 33 insertions(+), 38 deletions(-)
diff mbox series

Patch

diff --git a/sbc/sbc.c b/sbc/sbc.c
index 2fab366..886806d 100644
--- a/sbc/sbc.c
+++ b/sbc/sbc.c
@@ -994,17 +994,13 @@  static void sbc_set_defaults(sbc_t *sbc, unsigned long flags)
 {
 	struct sbc_priv *priv = sbc->priv;
 
-	if (priv->msbc) {
-		priv->pack_frame = msbc_pack_frame;
-		priv->unpack_frame = msbc_unpack_frame;
-	} else {
-		priv->pack_frame = sbc_pack_frame;
-		priv->unpack_frame = sbc_unpack_frame;
-	}
+	priv->pack_frame = sbc_pack_frame;
+	priv->unpack_frame = sbc_unpack_frame;
 
 	sbc->flags = flags;
 	sbc->frequency = SBC_FREQ_44100;
 	sbc->mode = SBC_MODE_STEREO;
+	sbc->allocation = SBC_AM_LOUDNESS;
 	sbc->subbands = SBC_SB_8;
 	sbc->blocks = SBC_BLK_16;
 	sbc->bitpool = 32;
@@ -1017,6 +1013,30 @@  static void sbc_set_defaults(sbc_t *sbc, unsigned long flags)
 #endif
 }
 
+static void sbc_set_defaults_msbc(sbc_t *sbc, unsigned long flags)
+{
+	struct sbc_priv *priv = sbc->priv;
+
+	priv->msbc = true;
+	priv->pack_frame = msbc_pack_frame;
+	priv->unpack_frame = msbc_unpack_frame;
+
+	sbc->flags = flags;
+	sbc->frequency = SBC_FREQ_16000;
+	sbc->mode = SBC_MODE_MONO;
+	sbc->allocation = SBC_AM_LOUDNESS;
+	sbc->subbands = SBC_SB_8;
+	sbc->blocks = MSBC_BLOCKS;
+	sbc->bitpool = 26;
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+	sbc->endian = SBC_LE;
+#elif __BYTE_ORDER == __BIG_ENDIAN
+	sbc->endian = SBC_BE;
+#else
+#error "Unknown byte order"
+#endif
+}
+
 SBC_EXPORT int sbc_init(sbc_t *sbc, unsigned long flags)
 {
 	if (!sbc)
@@ -1040,33 +1060,13 @@  SBC_EXPORT int sbc_init(sbc_t *sbc, unsigned long flags)
 
 SBC_EXPORT int sbc_init_msbc(sbc_t *sbc, unsigned long flags)
 {
-	struct sbc_priv *priv;
-
-	if (!sbc)
-		return -EIO;
-
-	memset(sbc, 0, sizeof(sbc_t));
-
-	sbc->priv_alloc_base = malloc(sizeof(struct sbc_priv) + SBC_ALIGN_MASK);
-	if (!sbc->priv_alloc_base)
-		return -ENOMEM;
-
-	sbc->priv = (void *) (((uintptr_t) sbc->priv_alloc_base +
-			SBC_ALIGN_MASK) & ~((uintptr_t) SBC_ALIGN_MASK));
-
-	memset(sbc->priv, 0, sizeof(struct sbc_priv));
-
-	priv = sbc->priv;
-	priv->msbc = true;
+	int err;
 
-	sbc_set_defaults(sbc, flags);
+	err = sbc_init(sbc, flags);
+	if (err < 0)
+		return err;
 
-	sbc->frequency = SBC_FREQ_16000;
-	sbc->blocks = MSBC_BLOCKS;
-	sbc->subbands = SBC_SB_8;
-	sbc->mode = SBC_MODE_MONO;
-	sbc->allocation = SBC_AM_LOUDNESS;
-	sbc->bitpool = 26;
+	sbc_set_defaults_msbc(sbc, flags);
 
 	return 0;
 }
@@ -1079,12 +1079,7 @@  SBC_EXPORT int sbc_reinit_msbc(sbc_t *sbc, unsigned long flags)
 	if (err < 0)
 		return err;
 
-	sbc->frequency = SBC_FREQ_16000;
-	sbc->blocks = MSBC_BLOCKS;
-	sbc->subbands = SBC_SB_8;
-	sbc->mode = SBC_MODE_MONO;
-	sbc->allocation = SBC_AM_LOUDNESS;
-	sbc->bitpool = 26;
+	sbc_set_defaults_msbc(sbc, flags);
 
 	return 0;
 }