Skip to contentSkip to conversation

Performance optimizations for input data processing in SBC encoder

PATCH In review Siarhei Siamashka 2 participants 3 messages Jan 27, 2009 – Jan 29, 2009 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Performance optimizations for input data processing in SBC encoder

Hello all,

Here is a cleaned up version of the previous experimental patch:
http://marc.info/?l=linux-bluetooth&m=123245036109697&w=2

I changed it to be alignment and byte order neutral (input data is read one byte at a time). It's a bit slower than reading via int16_t * pointer, but avoids headache of worrying about the other problems. Endian conversion is still also kept (when reading one byte at a time, it does not affect performance anyway).

The patch should be safe to apply.

Benchmarks show consistent performance improvement ~30% for both x86 and ARM Cortex-A8. It's even more than I measured before just because optimizations are cumulative and the effect of each individual change becomes more visible when the other parts also get faster (the previous benchmark was run before "-funroll-loops" optimization got committed).

ARM Cortex-A8:

before:

real    1m 24.78s
user    1m 21.20s
sys     0m 3.57s

after:

real    1m 4.72s
user    1m 1.03s
sys     0m 3.68s

Intel Core2:

before:

real    0m10.210s
user    0m9.761s
sys     0m0.324s

after:

real    0m7.729s
user    0m7.268s
sys     0m0.376s

Best regards,
Siarhei Siamashka

Quoted 1 line

From 4ba7973335bd19ac364e474bc811c20c3111d4e2 Mon Sep 17 00:00:00 2001

From: Siarhei Siamashka <[email protected]>
Date: Tue, 27 Jan 2009 18:57:35 +0200
Subject: [PATCH] Performance optimizations for input data processing in SBC encoder

Channels deinterleaving, endian conversion and samples reordering is done in one pass, avoiding the use of intermediate buffer. Also this code is implemented as a new "performance primitive", which allows further platform specific optimizations (ARMv6 and ARM NEON should gain quite a lot from assembly optimizations here).

---
 sbc/sbc.c                 |   55 +++++-----
 sbc/sbc_primitives.c      |  260 +++++++++++++++++++++++++++++++++++----------
 sbc/sbc_primitives.h      |   26 ++++--
 sbc/sbc_primitives_mmx.c  |   62 +----------
 sbc/sbc_primitives_neon.c |   58 +----------
 5 files changed, 258 insertions(+), 203 deletions(-)
sbc/sbc.c +29 -26
index 190ac17..365ee1f 100644
@@ -657,14 +657,11 @@ static int sbc_analyze_audio(struct sbc_encoder_state *state,
 		for (ch = 0; ch < frame->channels; ch++)
 			for (blk = 0; blk < frame->blocks; blk += 4) {
 				state->sbc_analyze_4b_4s(
-					&frame->pcm_sample[ch][blk * 4],
-					&state->X[ch][state->position[ch]],
+					&state->X[ch][state->position +
+							48 - blk * 4],
 					frame->sb_sample_f[blk][ch],
 					frame->sb_sample_f[blk + 1][ch] -
 					frame->sb_sample_f[blk][ch]);
-				state->position[ch] -= 16;
-				if (state->position[ch] < 0)
-					state->position[ch] = 64 - 16;
 			}
 		return frame->blocks * 4;
 
@@ -672,14 +669,11 @@ static int sbc_analyze_audio(struct sbc_encoder_state *state,
 		for (ch = 0; ch < frame->channels; ch++)
 			for (blk = 0; blk < frame->blocks; blk += 4) {
 				state->sbc_analyze_4b_8s(
-					&frame->pcm_sample[ch][blk * 8],
-					&state->X[ch][state->position[ch]],
+					&state->X[ch][state->position +
+							96 - blk * 8],
 					frame->sb_sample_f[blk][ch],
 					frame->sb_sample_f[blk + 1][ch] -
 					frame->sb_sample_f[blk][ch]);
-				state->position[ch] -= 32;
-				if (state->position[ch] < 0)
-					state->position[ch] = 128 - 32;
 			}
 		return frame->blocks * 8;
 
@@ -935,8 +929,7 @@ static void sbc_encoder_init(struct sbc_encoder_state *state,
 				const struct sbc_frame *frame)
 {
 	memset(&state->X, 0, sizeof(state->X));
-	state->subbands = frame->subbands;
-	state->position[0] = state->position[1] = 12 * frame->subbands;
+	state->position = SBC_X_BUFFER_SIZE - frame->subbands * 9;
 
 	sbc_init_primitives(state);
 }
@@ -1060,8 +1053,10 @@ int sbc_encode(sbc_t *sbc, void *input, int input_len, void *output,
 		int output_len, int *written)
 {
 	struct sbc_priv *priv;
-	char *ptr;
-	int i, ch, framelen, samples;
+	int framelen, samples;
+	int (*sbc_enc_process_input)(int position,
+			const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+			int nsamples, int nchannels);
 
 	if (!sbc && !input)
 		return -EIO;
@@ -1096,20 +1091,28 @@ int sbc_encode(sbc_t *sbc, void *input, int input_len, void *output,
 	if (!output || output_len < priv->frame.length)
 		return -ENOSPC;
 
-	ptr = input;
-
-	for (i = 0; i < priv->frame.subbands * priv->frame.blocks; i++) {
-		for (ch = 0; ch < priv->frame.channels; ch++) {
-			int16_t s;
-			if (sbc->endian == SBC_BE)
-				s = (ptr[0] & 0xff) << 8 | (ptr[1] & 0xff);
-			else
-				s = (ptr[0] & 0xff) | (ptr[1] & 0xff) << 8;
-			ptr += 2;
-			priv->frame.pcm_sample[ch][i] = s;
-		}
+	/* Select the needed input data processing function and call it */
+	if (priv->frame.subbands == 8) {
+		if (sbc->endian == SBC_BE)
+			sbc_enc_process_input =
+				priv->enc_state.sbc_enc_process_input_8s_be;
+		else
+			sbc_enc_process_input =
+				priv->enc_state.sbc_enc_process_input_8s_le;
+	} else {
+		if (sbc->endian == SBC_BE)
+			sbc_enc_process_input =
+				priv->enc_state.sbc_enc_process_input_4s_be;
+		else
+			sbc_enc_process_input =
+				priv->enc_state.sbc_enc_process_input_4s_le;
 	}
 
+	priv->enc_state.position = sbc_enc_process_input(
+		priv->enc_state.position, (const uint8_t *) input,
+		priv->enc_state.X, priv->frame.subbands * priv->frame.blocks,
+		priv->frame.channels);
+
 	samples = sbc_analyze_audio(&priv->enc_state, &priv->frame);
 
 	framelen = sbc_pack_frame(output, &priv->frame, output_len);
sbc/sbc_primitives.c +204 -56

294 lines not on this page · raw message

sbc/sbc_primitives.h +19 -7
index a418ed8..5b7c9ac 100644
@@ -27,6 +27,7 @@
 #define __SBC_PRIMITIVES_H
 
 #define SCALE_OUT_BITS 15
+#define SBC_X_BUFFER_SIZE 328
 
 #ifdef __GNUC__
 #define SBC_ALWAYS_INLINE __attribute__((always_inline))
@@ -35,17 +36,28 @@
 #endif
 
 struct sbc_encoder_state {
-	int subbands;
-	int position[2];
-	int16_t SBC_ALIGNED X[2][256];
+	int position;
+	int16_t SBC_ALIGNED X[2][SBC_X_BUFFER_SIZE];
 	/* Polyphase analysis filter for 4 subbands configuration,
 	 * it handles 4 blocks at once */
-	void (*sbc_analyze_4b_4s)(int16_t *pcm, int16_t *x,
-					int32_t *out, int out_stride);
+	void (*sbc_analyze_4b_4s)(int16_t *x, int32_t *out, int out_stride);
 	/* Polyphase analysis filter for 8 subbands configuration,
 	 * it handles 4 blocks at once */
-	void (*sbc_analyze_4b_8s)(int16_t *pcm, int16_t *x,
-					int32_t *out, int out_stride);
+	void (*sbc_analyze_4b_8s)(int16_t *x, int32_t *out, int out_stride);
+	/* Process input data (deinterleave, endian conversion, reordering),
+	 * depending on the number of subbands and input data byte order */
+	int (*sbc_enc_process_input_4s_le)(int position,
+			const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+			int nsamples, int nchannels);
+	int (*sbc_enc_process_input_4s_be)(int position,
+			const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+			int nsamples, int nchannels);
+	int (*sbc_enc_process_input_8s_le)(int position,
+			const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+			int nsamples, int nchannels);
+	int (*sbc_enc_process_input_8s_be)(int position,
+			const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
+			int nsamples, int nchannels);
 };
 
 /*
sbc/sbc_primitives_mmx.c +4 -58

76 lines not on this page · raw message

sbc/sbc_primitives_neon.c +2 -56

74 lines not on this page · raw message

Signature · 1 line
1.5.6.5
maintainer
#1 lore raw

Hi Siarhei,

Quoted 10 lines

Here is a cleaned up version of the previous experimental patch:
http://marc.info/?l=linux-bluetooth&m=123245036109697&w=2

I changed it to be alignment and byte order neutral (input data is read one byte at a time). It's a bit slower than reading via int16_t * pointer, but avoids headache of worrying about the other problems. Endian conversion is still also kept (when reading one byte at a time, it does not affect performance anyway).

The patch should be safe to apply.

your patch has been applied. Thanks.

Quoted 7 lines

Benchmarks show consistent performance improvement ~30% for both x86 and ARM Cortex-A8. It's even more than I measured before just because optimizations are cumulative and the effect of each individual change becomes
more visible when the other parts also get faster (the previous benchmark was
run before "-funroll-loops" optimization got committed).

Sounds great to me. Keep optimizing it :)

Regards

Marcel

#2 lore raw
Quoted 11 lines · On Wednesday 28 January 2009 07:46:53 ext Marcel Holtmann wrote:

Hi Siarhei,

Quoted 9 lines

Here is a cleaned up version of the previous experimental patch:
http://marc.info/?l=linux-bluetooth&m=123245036109697&w=2

I changed it to be alignment and byte order neutral (input data is read one byte at a time). It's a bit slower than reading via int16_t * pointer, but avoids headache of worrying about the other problems. Endian conversion is still also kept (when reading one byte at a time, it does not affect performance anyway).

Quoted 3 lines
Quoted 1 line

The patch should be safe to apply.

your patch has been applied. Thanks.

Thanks.

Quoted 9 lines
Quoted 7 lines

Benchmarks show consistent performance improvement ~30% for both x86 and ARM Cortex-A8. It's even more than I measured before just because optimizations are cumulative and the effect of each individual change becomes
more visible when the other parts also get faster (the previous benchmark was
run before "-funroll-loops" optimization got committed).

Sounds great to me. Keep optimizing it :)

Fortunately not so much is left to be optimized :)

Joint stereo encoding performance still can be improved. Also a few other tweaks could be tried for 'sbc_pack_frame_internal'. After that, only implementing SIMD optimizations for various CPU cores (primarily ARM11) would be left to do.

Best regards,
Siarhei Siamashka