diff mbox series

[2/2] sbc: Add sbc_get_delay API to retrieve codec delay

Message ID 20241212084419.1326427-2-arkadiusz.bokowy@gmail.com (mailing list archive)
State New
Headers show
Series [1/2] sbc: Pass const pointer if structure is not modified | expand

Commit Message

Arkadiusz Bokowy Dec. 12, 2024, 8:44 a.m. UTC
Most audio codecs introduce intrinsic codec delay. SBC codec introduces
encode/decode delay which is not constant but depends on the number of
subbunds used. Providing API for that will simplify library usage in
case when precise delay calculation is required.
---
 sbc/sbc.c | 20 ++++++++++++++++++++
 sbc/sbc.h |  3 +++
 2 files changed, 23 insertions(+)
diff mbox series

Patch

diff --git a/sbc/sbc.c b/sbc/sbc.c
index 428c8ee..869f367 100644
--- a/sbc/sbc.c
+++ b/sbc/sbc.c
@@ -1470,6 +1470,26 @@  SBC_EXPORT size_t sbc_get_codesize(const sbc_t *sbc)
 	return subbands * blocks * channels * 2;
 }
 
+SBC_EXPORT unsigned sbc_get_delay(const sbc_t *sbc)
+{
+	uint16_t subbands;
+	struct sbc_priv *priv;
+
+	priv = sbc->priv;
+	if (!priv->init) {
+		subbands = sbc->subbands ? 8 : 4;
+	} else {
+		subbands = priv->frame.subbands;
+	}
+
+	/*
+	 * The delay is caused by the processing function which analyzes
+	 * old samples to encode the current frame. The amount of that
+	 * samples depends on the number of subbands.
+	 */
+	return subbands * 9 + 1;
+}
+
 SBC_EXPORT const char *sbc_get_implementation_info(const sbc_t *sbc)
 {
 	struct sbc_priv *priv;
diff --git a/sbc/sbc.h b/sbc/sbc.h
index b7323e8..829b161 100644
--- a/sbc/sbc.h
+++ b/sbc/sbc.h
@@ -94,6 +94,9 @@  unsigned sbc_get_frame_duration(const sbc_t *sbc);
 /* Returns the uncompressed block size in bytes */
 size_t sbc_get_codesize(const sbc_t *sbc);
 
+/* Returns the delay introduced by the codec in frames */
+unsigned sbc_get_delay(const sbc_t *sbc);
+
 const char *sbc_get_implementation_info(const sbc_t *sbc);
 void sbc_finish(sbc_t *sbc);