summaryrefslogtreecommitdiffstats
path: root/sbc
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@nokia.com>2007-11-01 15:27:38 +0000
committerJohan Hedberg <johan.hedberg@nokia.com>2007-11-01 15:27:38 +0000
commit9138f99acb799d7ce702afb357c134042f933f4b (patch)
tree1887f013c64c8385fd4b91016c4854b113f986b3 /sbc
parent9a5725b6306e1d69a89d988af94bd3efe1ef5de6 (diff)
Coding style cleanup
Diffstat (limited to 'sbc')
-rw-r--r--sbc/sbc.c297
-rw-r--r--sbc/sbcdec.c36
-rw-r--r--sbc/sbcenc.c3
-rw-r--r--sbc/sbcinfo.c14
-rw-r--r--sbc/sbctester.c64
5 files changed, 229 insertions, 185 deletions
diff --git a/sbc/sbc.c b/sbc/sbc.c
index 94e5c752..3e65603d 100644
--- a/sbc/sbc.c
+++ b/sbc/sbc.c
@@ -25,9 +25,9 @@
/* todo items:
- use a log2 table for byte integer scale factors calculation (sum log2 results for high and low bytes)
- fill bitpool by 16 bits instead of one at a time in bits allocation/bitpool generation
- port to the dsp
+ use a log2 table for byte integer scale factors calculation (sum log2 results
+ for high and low bytes) fill bitpool by 16 bits instead of one at a time in
+ bits allocation/bitpool generation port to the dsp
*/
@@ -77,7 +77,7 @@
#define SBC_SB_4 0x00
#define SBC_SB_8 0x01
-/* This structure contains an unpacked SBC frame.
+/* This structure contains an unpacked SBC frame.
Yes, there is probably quite some unused space herein */
struct sbc_frame {
uint16_t sampling_frequency; /* in kHz */
@@ -95,13 +95,19 @@ struct sbc_frame {
} allocation_method;
uint8_t subbands;
uint8_t bitpool;
- uint8_t join; /* bit number x set means joint stereo has been used in subband x */
- uint8_t scale_factor[2][8]; /* only the lower 4 bits of every element are to be used */
- uint16_t audio_sample[16][2][8]; /* raw integer subband samples in the frame */
+
+ /* bit number x set means joint stereo has been used in subband x */
+ uint8_t join;
+
+ /* only the lower 4 bits of every element are to be used */
+ uint8_t scale_factor[2][8];
+
+ /* raw integer subband samples in the frame */
+ uint16_t audio_sample[16][2][8];
int32_t sb_sample_f[16][2][8];
- int32_t sb_sample[16][2][8]; /* modified subband samples */
- int16_t pcm_sample[2][16*8]; /* original pcm audio samples */
+ int32_t sb_sample[16][2][8]; /* modified subband samples */
+ int16_t pcm_sample[2][16*8]; /* original pcm audio samples */
};
struct sbc_decoder_state {
@@ -175,8 +181,9 @@ static uint8_t sbc_crc8(const uint8_t * data, size_t len)
}
/*
- * Code straight from the spec to calculate the bits array
- * Takes a pointer to the frame in question, a pointer to the bits array and the sampling frequency (as 2 bit integer)
+ * Code straight from the spec to calculate the bits array
+ * Takes a pointer to the frame in question, a pointer to the bits array and
+ * the sampling frequency (as 2 bit integer)
*/
static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], uint8_t sf)
{
@@ -186,24 +193,21 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
for (ch = 0; ch < frame->channels; ch++) {
if (frame->allocation_method == SNR) {
- for (sb = 0; sb < frame->subbands; sb++) {
+ for (sb = 0; sb < frame->subbands; sb++)
bitneed[ch][sb] = frame->scale_factor[ch][sb];
- }
} else {
for (sb = 0; sb < frame->subbands; sb++) {
- if (frame->scale_factor[ch][sb] == 0) {
+ if (frame->scale_factor[ch][sb] == 0)
bitneed[ch][sb] = -5;
- } else {
- if (frame->subbands == 4) {
+ else {
+ if (frame->subbands == 4)
loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
- } else {
+ else
loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
- }
- if (loudness > 0) {
+ if (loudness > 0)
bitneed[ch][sb] = loudness / 2;
- } else {
+ else
bitneed[ch][sb] = loudness;
- }
}
}
}
@@ -222,11 +226,10 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
bitcount += slicecount;
slicecount = 0;
for (sb = 0; sb < frame->subbands; sb++) {
- if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16)) {
+ if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
slicecount++;
- } else if (bitneed[ch][sb] == bitslice + 1) {
+ else if (bitneed[ch][sb] == bitslice + 1)
slicecount += 2;
- }
}
} while (bitcount + slicecount < frame->bitpool);
@@ -236,9 +239,9 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
}
for (sb = 0; sb < frame->subbands; sb++) {
- if (bitneed[ch][sb] < bitslice + 2) {
+ if (bitneed[ch][sb] < bitslice + 2)
bits[ch][sb] = 0;
- } else {
+ else {
bits[ch][sb] = bitneed[ch][sb] - bitslice;
if (bits[ch][sb] > 16)
bits[ch][sb] = 16;
@@ -274,26 +277,23 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
if (frame->allocation_method == SNR) {
for (ch = 0; ch < 2; ch++) {
- for (sb = 0; sb < frame->subbands; sb++) {
+ for (sb = 0; sb < frame->subbands; sb++)
bitneed[ch][sb] = frame->scale_factor[ch][sb];
- }
}
} else {
for (ch = 0; ch < 2; ch++) {
for (sb = 0; sb < frame->subbands; sb++) {
- if (frame->scale_factor[ch][sb] == 0) {
+ if (frame->scale_factor[ch][sb] == 0)
bitneed[ch][sb] = -5;
- } else {
- if (frame->subbands == 4) {
+ else {
+ if (frame->subbands == 4)
loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
- } else {
+ else
loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
- }
- if (loudness > 0) {
+ if (loudness > 0)
bitneed[ch][sb] = loudness / 2;
- } else {
+ else
bitneed[ch][sb] = loudness;
- }
}
}
}
@@ -316,11 +316,10 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
slicecount = 0;
for (ch = 0; ch < 2; ch++) {
for (sb = 0; sb < frame->subbands; sb++) {
- if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16)) {
+ if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
slicecount++;
- } else if (bitneed[ch][sb] == bitslice + 1) {
+ else if (bitneed[ch][sb] == bitslice + 1)
slicecount += 2;
- }
}
}
} while (bitcount + slicecount < frame->bitpool);
@@ -354,9 +353,8 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
if (ch == 1) {
ch = 0;
sb++;
- } else {
+ } else
ch = 1;
- }
}
ch = 0;
@@ -369,16 +367,15 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
if (ch == 1) {
ch = 0;
sb++;
- } else {
+ } else
ch = 1;
- }
}
}
}
-/*
+/*
* Unpacks a SBC frame at the beginning of the stream in data,
* which has at most len bytes into frame.
* Returns the length in bytes of the packed frame, or a negative
@@ -389,17 +386,21 @@ static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8], ui
* -3 CRC8 incorrect
* -4 Bitpool value out of bounds
*/
-static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_t len)
+static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame,
+ size_t len)
{
int consumed;
- /* Will copy the parts of the header that are relevant to crc calculation here */
+ /* Will copy the parts of the header that are relevant to crc
+ * calculation here */
uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int crc_pos = 0;
int32_t temp;
- uint8_t sf; /* sampling_frequency, temporarily needed as array index */
+ uint8_t sf; /* sampling_frequency, temporarily needed as
+ array index */
- int ch, sb, blk, bit; /* channel, subband, block and bit standard counters */
+ int ch, sb, blk, bit; /* channel, subband, block and bit standard
+ counters */
int bits[2][8]; /* bits distribution */
int levels[2][8]; /* levels derived from that */
@@ -458,10 +459,12 @@ static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_
frame->bitpool = data[2];
- if (((frame->channel_mode == MONO || frame->channel_mode == DUAL_CHANNEL)
- && frame->bitpool > 16 * frame->subbands)
- || ((frame->channel_mode == STEREO || frame->channel_mode == JOINT_STEREO)
- && frame->bitpool > 32 * frame->subbands))
+ if (((frame->channel_mode == MONO ||
+ frame->channel_mode == DUAL_CHANNEL) &&
+ frame->bitpool > 16 * frame->subbands) ||
+ ((frame->channel_mode == STEREO ||
+ frame->channel_mode == JOINT_STEREO) &&
+ frame->bitpool > 32 * frame->subbands))
return -4;
/* data[3] is crc, we're checking it later */
@@ -477,14 +480,12 @@ static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_
return -1;
frame->join = 0x00;
- for (sb = 0; sb < frame->subbands - 1; sb++) {
+ for (sb = 0; sb < frame->subbands - 1; sb++)
frame->join |= ((data[4] >> (7 - sb)) & 0x01) << sb;
- }
- if (frame->subbands == 4) {
+ if (frame->subbands == 4)
crc_header[crc_pos / 8] = data[4] & 0xf0;
- } else {
+ else
crc_header[crc_pos / 8] = data[4];
- }
consumed += frame->subbands;
crc_pos += frame->subbands;
@@ -496,8 +497,10 @@ static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_
for (ch = 0; ch < frame->channels; ch++) {
for (sb = 0; sb < frame->subbands; sb++) {
/* FIXME assert(consumed % 4 == 0); */
- frame->scale_factor[ch][sb] = (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
- crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
+ frame->scale_factor[ch][sb] =
+ (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
+ crc_header[crc_pos >> 3] |=
+ frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
consumed += 4;
crc_pos += 4;
@@ -522,7 +525,8 @@ static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_
return -1;
b = (data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01;
- frame->audio_sample[blk][ch][sb] |= b << (bits[ch][sb] - bit - 1);
+ frame->audio_sample[blk][ch][sb] |=
+ b << (bits[ch][sb] - bit - 1);
consumed++;
}
@@ -531,24 +535,27 @@ static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_
}
for (ch = 0; ch < frame->channels; ch++) {
- for (sb = 0; sb < frame->subbands; sb++) {
+ for (sb = 0; sb < frame->subbands; sb++)
levels[ch][sb] = (1 << bits[ch][sb]) - 1;
- }
}
for (blk = 0; blk < frame->blocks; blk++) {
for (ch = 0; ch < frame->channels; ch++) {
for (sb = 0; sb < frame->subbands; sb++) {
if (levels[ch][sb] > 0) {
- frame->sb_sample[blk][ch][sb] =
- (((frame->audio_sample[blk][ch][sb] << 16) | 0x8000) / levels[ch][sb]) - 0x8000;
+ frame->sb_sample[blk][ch][sb] =
+ (((frame->audio_sample[blk][ch][sb] << 16) | 0x8000) /
+ levels[ch][sb]) - 0x8000;
frame->sb_sample[blk][ch][sb] >>= 3;
- frame->sb_sample[blk][ch][sb] = (frame->sb_sample[blk][ch][sb] << (frame->scale_factor[ch][sb] + 1)); // Q13
- } else {
+ /* Q13 */
+ frame->sb_sample[blk][ch][sb] =
+ (frame->sb_sample[blk][ch][sb] <<
+ (frame->scale_factor[ch][sb] + 1));
+
+ } else
frame->sb_sample[blk][ch][sb] = 0;
- }
}
}
}
@@ -557,8 +564,11 @@ static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_
for (blk = 0; blk < frame->blocks; blk++) {
for (sb = 0; sb < frame->subbands; sb++) {
if (frame->join & (0x01 << sb)) {
- temp = frame->sb_sample[blk][0][sb] + frame->sb_sample[blk][1][sb];
- frame->sb_sample[blk][1][sb] = frame->sb_sample[blk][0][sb] - frame->sb_sample[blk][1][sb];
+ temp = frame->sb_sample[blk][0][sb] +
+ frame->sb_sample[blk][1][sb];
+ frame->sb_sample[blk][1][sb] =
+ frame->sb_sample[blk][0][sb] -
+ frame->sb_sample[blk][1][sb];
frame->sb_sample[blk][0][sb] = temp;
}
}
@@ -568,11 +578,11 @@ static int sbc_unpack_frame(const uint8_t * data, struct sbc_frame *frame, size_
if ((consumed & 0x7) != 0)
consumed += 8 - (consumed & 0x7);
-
return consumed >> 3;
}
-static void sbc_decoder_init(struct sbc_decoder_state *state, const struct sbc_frame *frame)
+static void sbc_decoder_init(struct sbc_decoder_state *state,
+ const struct sbc_frame *frame)
{
int i, ch;
@@ -595,19 +605,16 @@ static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
state->offset[ch][i]--;
if (state->offset[ch][i] < 0) {
state->offset[ch][i] = 79;
- for(j = 0; j < 9; j++) {
+ for(j = 0; j < 9; j++)
state->V[ch][j+80] = state->V[ch][j];
- }
}
}
-
for(i = 0; i < 8; i++) {
/* Distribute the new matrix value to the shifted position */
SBC_FIXED_0(res);
- for (j = 0; j < 4; j++) {
+ for (j = 0; j < 4; j++)
MULA(res, synmatrix4[i][j], frame->sb_sample[blk][ch][j]);
- }
state->V[ch][state->offset[ch][i]] = SCALE4_STAGED1(res);
}
@@ -616,11 +623,14 @@ static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
k = (i + 4) & 0xf;
SBC_FIXED_0(res);
for(j = 0; j < 10; idx++) {
- MULA(res, state->V[ch][state->offset[ch][i]+j++], sbc_proto_4_40m0[idx]);
- MULA(res, state->V[ch][state->offset[ch][k]+j++], sbc_proto_4_40m1[idx]);
+ MULA(res, state->V[ch][state->offset[ch][i]+j++],
+ sbc_proto_4_40m0[idx]);
+ MULA(res, state->V[ch][state->offset[ch][k]+j++],
+ sbc_proto_4_40m1[idx]);
}
- /* Store in output */
- frame->pcm_sample[ch][blk * 4 + i] = SCALE4_STAGED2(res); // Q0
+
+ /* Store in output, Q0 */
+ frame->pcm_sample[ch][blk * 4 + i] = SCALE4_STAGED2(res);
}
}
@@ -635,9 +645,8 @@ static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
state->offset[ch][i]--;
if (state->offset[ch][i] < 0) {
state->offset[ch][i] = 159;
- for(j = 0; j < 9; j++) {
- state->V[ch][j+160] = state->V[ch][j];
- }
+ for(j = 0; j < 9; j++)
+ state->V[ch][j+160] = state->V[ch][j];
}
}
@@ -645,11 +654,12 @@ static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
/* Distribute the new matrix value to the shifted position */
SBC_FIXED_0(res);
for (j = 0; j < 8; j++) {
- MULA(res, synmatrix8[i][j], frame->sb_sample[blk][ch][j]); // Q28 = Q15 * Q13
+ /* Q28 = Q15 * Q13 */
+ MULA(res, synmatrix8[i][j], frame->sb_sample[blk][ch][j]);
}
- state->V[ch][state->offset[ch][i]] = SCALE8_STAGED1(res); // Q10
+ /* Q10 */
+ state->V[ch][state->offset[ch][i]] = SCALE8_STAGED1(res);
}
-
/* Compute the samples */
for(idx = 0, i = 0; i < 8; i++) {
@@ -665,10 +675,11 @@ static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
}
}
-static int sbc_synthesize_audio(struct sbc_decoder_state *state, struct sbc_frame *frame)
+static int sbc_synthesize_audio(struct sbc_decoder_state *state,
+ struct sbc_frame *frame)
{
int ch, blk;
-
+
switch (frame->subbands) {
case 4:
for (ch = 0; ch < frame->channels; ch++) {
@@ -689,7 +700,8 @@ static int sbc_synthesize_audio(struct sbc_decoder_state *state, struct sbc_fram
}
}
-static void sbc_encoder_init(struct sbc_encoder_state *state, const struct sbc_frame *frame)
+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;
@@ -702,9 +714,9 @@ static inline void _sbc_analyze_four(const int32_t *in, int32_t *out)
sbc_extended_t t[8];
sbc_extended_t s[4];
- MUL(res, _sbc_proto_4[0], (in[8] - in[32])); // Q18
+ MUL(res, _sbc_proto_4[0], (in[8] - in[32])); /* Q18 */
MULA(res, _sbc_proto_4[1], (in[16] - in[24]));
- t[0] = SCALE4_STAGE1(res); // Q8
+ t[0] = SCALE4_STAGE1(res); /* Q8 */
MUL(res, _sbc_proto_4[2], in[1]);
MULA(res, _sbc_proto_4[3], in[9]);
@@ -739,7 +751,8 @@ static inline void _sbc_analyze_four(const int32_t *in, int32_t *out)
MULA(res, _sbc_proto_4[12], in[37]);
t[5] = SCALE4_STAGE1(res);
- /* don't compute t[6]... this term always multiplies with cos(pi/2) = 0 */
+ /* don't compute t[6]... this term always multiplies
+ * with cos(pi/2) = 0 */
MUL(res, _sbc_proto_4[6], in[7]);
MULA(res, _sbc_proto_4[5], in[15]);
@@ -754,7 +767,7 @@ static inline void _sbc_analyze_four(const int32_t *in, int32_t *out)
MULA(s[2], _anamatrix4[3], t[5] + t[7]);
MUL(s[3], _anamatrix4[3], t[1] + t[3]);
MULA(s[3], _anamatrix4[1], - t[5] + t[7]);
- out[0] = SCALE4_STAGE2( s[0] + s[1] + s[2]); // Q0
+ out[0] = SCALE4_STAGE2( s[0] + s[1] + s[2]); /* Q0 */
out[1] = SCALE4_STAGE2(-s[0] + s[1] + s[3]);
out[2] = SCALE4_STAGE2(-s[0] + s[1] - s[3]);
out[3] = SCALE4_STAGE2( s[0] + s[1] - s[2]);
@@ -776,14 +789,14 @@ static inline void _sbc_analyze_eight(const int32_t *in, int32_t *out)
sbc_extended_t res;
sbc_extended_t t[8];
sbc_extended_t s[8];
-
- MUL(res, _sbc_proto_8[0], (in[16] - in[64])); // Q18 = Q18 * Q0
+
+ MUL(res, _sbc_proto_8[0], (in[16] - in[64])); /* Q18 = Q18 * Q0 */
MULA(res, _sbc_proto_8[1], (in[32] - in[48]));
MULA(res, _sbc_proto_8[2], in[4]);
MULA(res, _sbc_proto_8[3], in[20]);
MULA(res, _sbc_proto_8[4], in[36]);
MULA(res, _sbc_proto_8[5], in[52]);
- t[0] = SCALE8_STAGE1(res); // Q10
+ t[0] = SCALE8_STAGE1(res); /* Q10 */
MUL(res, _sbc_proto_8[6], in[2]);
MULA(res, _sbc_proto_8[7], in[18]);
@@ -862,7 +875,7 @@ static inline void _sbc_analyze_eight(const int32_t *in, int32_t *out)
MULA(res, -_sbc_proto_8[21], in[75]);
t[7] = SCALE8_STAGE1(res);
- MUL(s[0], _anamatrix8[0], t[0]); // = Q14 * Q10
+ MUL(s[0], _anamatrix8[0], t[0]); /* = Q14 * Q10 */
MULA(s[0], _anamatrix8[1], t[6]);
MUL(s[1], _anamatrix8[7], t[1]);
MUL(s[2], _anamatrix8[2], t[2]);
@@ -895,7 +908,8 @@ static inline void _sbc_analyze_eight(const int32_t *in, int32_t *out)
}
static inline void sbc_analyze_eight(struct sbc_encoder_state *state,
- struct sbc_frame *frame, int ch, int blk)
+ struct sbc_frame *frame, int ch,
+ int blk)
{
int i;
@@ -907,23 +921,22 @@ static inline void sbc_analyze_eight(struct sbc_encoder_state *state,
_sbc_analyze_eight(state->X[ch], frame->sb_sample_f[blk][ch]);
}
-static int sbc_analyze_audio(struct sbc_encoder_state *state, struct sbc_frame *frame)
+static int sbc_analyze_audio(struct sbc_encoder_state *state,
+ struct sbc_frame *frame)
{
int ch, blk;
switch (frame->subbands) {
case 4:
for (ch = 0; ch < frame->channels; ch++)
- for (blk = 0; blk < frame->blocks; blk++) {
+ for (blk = 0; blk < frame->blocks; blk++)
sbc_analyze_four(state, frame, ch, blk);
- }
return frame->blocks * 4;
case 8:
for (ch = 0; ch < frame->channels; ch++)
- for (blk = 0; blk < frame->blocks; blk++) {
+ for (blk = 0; blk < frame->blocks; blk++)
sbc_analyze_eight(state, frame, ch, blk);
- }
return frame->blocks * 8;
default:
@@ -933,9 +946,9 @@ static int sbc_analyze_audio(struct sbc_encoder_state *state, struct sbc_frame *
/*
* Packs the SBC frame from frame into the memory at data. At most len
- * bytes will be used, should more memory be needed an appropriate
+ * bytes will be used, should more memory be needed an appropriate
* error code will be returned. Returns the length of the packed frame
- * on success or a negative value on error.
+ * on success or a negative value on error.
*
* The error codes are:
* -1 Not enough memory reserved
@@ -953,7 +966,8 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int crc_pos = 0;
- uint8_t sf; /* Sampling frequency as temporary value for table lookup */
+ /* Sampling frequency as temporary value for table lookup */
+ uint8_t sf;
int ch, sb, blk, bit; /* channel, subband, block and bit counters */
int bits[2][8]; /* bits distribution */
@@ -965,7 +979,8 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
return -1;
}
- /* Clear first 4 bytes of data (that's the constant length part of the SBC header) */
+ /* Clear first 4 bytes of data (that's the constant length part of the
+ * SBC header) */
memset(data, 0, 4);
data[0] = SBC_SYNCWORD;
@@ -1021,12 +1036,13 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
}
data[2] = frame->bitpool;
- if (((frame->channel_mode == MONO || frame->channel_mode == DUAL_CHANNEL)
- && frame->bitpool > 16 * frame->subbands)
- || ((frame->channel_mode == STEREO || frame->channel_mode == JOINT_STEREO)
- && frame->bitpool > 32 * frame->subbands)) {
+ if ((frame->channel_mode == MONO || frame->channel_mode == DUAL_CHANNEL) &&
+ frame->bitpool > 16 * frame->subbands)
+ return -5;
+
+ if ((frame->channel_mode == STEREO || frame->channel_mode == JOINT_STEREO) &&
+ frame->bitpool > 32 * frame->subbands)
return -5;
- }
/* Can't fill in crc yet */
@@ -1050,14 +1066,20 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
}
if (frame->channel_mode == JOINT_STEREO) {
- int32_t sb_sample_j[16][2][7]; /* like frame->sb_sample but joint stereo */
- int scalefactor_j[2][7], scale_factor_j[2][7]; /* scalefactor and scale_factor in joint case */
+ /* like frame->sb_sample but joint stereo */
+ int32_t sb_sample_j[16][2][7];
+ /* scalefactor and scale_factor in joint case */
+ int scalefactor_j[2][7], scale_factor_j[2][7];
/* Calculate joint stereo signal */
for (sb = 0; sb < frame->subbands - 1; sb++) {
for (blk = 0; blk < frame->blocks; blk++) {
- sb_sample_j[blk][0][sb] = (frame->sb_sample_f[blk][0][sb] + frame->sb_sample_f[blk][1][sb]) >> 1;
- sb_sample_j[blk][1][sb] = (frame->sb_sample_f[blk][0][sb] - frame->sb_sample_f[blk][1][sb]) >> 1;
+ sb_sample_j[blk][0][sb] =
+ (frame->sb_sample_f[blk][0][sb] +
+ frame->sb_sample_f[blk][1][sb]) >> 1;
+ sb_sample_j[blk][1][sb] =
+ (frame->sb_sample_f[blk][0][sb] -
+ frame->sb_sample_f[blk][1][sb]) >> 1;
}
}
@@ -1087,8 +1109,10 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
scalefactor[0][sb] = scalefactor_j[0][sb];
scalefactor[1][sb] = scalefactor_j[1][sb];
for (blk = 0; blk < frame->blocks; blk++) {
- frame->sb_sample_f[blk][0][sb] = sb_sample_j[blk][0][sb];
- frame->sb_sample_f[blk][1][sb] = sb_sample_j[blk][1][sb];
+ frame->sb_sample_f[blk][0][sb] =
+ sb_sample_j[blk][0][sb];
+ frame->sb_sample_f[blk][1][sb] =
+ sb_sample_j[blk][1][sb];
}
}
}
@@ -1097,15 +1121,13 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
return -1;
data[4] = 0;
- for (sb = 0; sb < frame->subbands - 1; sb++) {
+ for (sb = 0; sb < frame->subbands - 1; sb++)
data[4] |= ((frame->join >> sb) & 0x01) << (7 - sb);
- }
- if (frame->subbands == 4) {
+ if (frame->subbands == 4)
crc_header[crc_pos / 8] = data[4] & 0xf0;
- } else {
+ else
crc_header[crc_pos / 8] = data[4];
- }
-
+
produced += frame->subbands;
crc_pos += frame->subbands;
}
@@ -1130,21 +1152,20 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
sbc_calculate_bits(frame, bits, sf);
for (ch = 0; ch < frame->channels; ch++) {
- for (sb = 0; sb < frame->subbands; sb++) {
+ for (sb = 0; sb < frame->subbands; sb++)
levels[ch][sb] = (1 << bits[ch][sb]) - 1;
- }
}
for (blk = 0; blk < frame->blocks; blk++) {
for (ch = 0; ch < frame->channels; ch++) {
for (sb = 0; sb < frame->subbands; sb++) {
- if (levels[ch][sb] > 0) {
+ if (levels[ch][sb] > 0)
frame->audio_sample[blk][ch][sb] =
- (uint16_t) ((((frame->sb_sample_f[blk][ch][sb]*levels[ch][sb]) >> (frame->scale_factor[ch][sb] + 1)) +
- levels[ch][sb]) >> 1);
- } else {
+ (uint16_t) ((((frame->sb_sample_f[blk][ch][sb]*levels[ch][sb]) >>
+ (frame->scale_factor[ch][sb] + 1)) +
+ levels[ch][sb]) >> 1);
+ else
frame->audio_sample[blk][ch][sb] = 0;
- }
}
}
}
@@ -1155,14 +1176,12 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
if (bits[ch][sb] != 0) {
for (bit = 0; bit < bits[ch][sb]; bit++) {
int b; /* A bit */
- if (produced > len * 8) {
+ if (produced > len * 8)
return -1;
- }
- if (produced % 8 == 0) {
+ if (produced % 8 == 0)
data[produced / 8] = 0;
- }
- b = ((frame->audio_sample[blk][ch][sb]) >> (bits[ch][sb] - bit -
- 1)) & 0x01;
+ b = ((frame->audio_sample[blk][ch][sb]) >>
+ (bits[ch][sb] - bit - 1)) & 0x01;
data[produced / 8] |= b << (7 - (produced % 8));
produced++;
}
@@ -1171,9 +1190,8 @@ static int sbc_pack_frame(uint8_t * data, struct sbc_frame *frame, size_t len)
}
}
- if (produced % 8 != 0) {
+ if (produced % 8 != 0)
produced += 8 - (produced % 8);
- }
return produced / 8;
}
@@ -1226,7 +1244,6 @@ int sbc_decode(sbc_t *sbc, void *data, int count)
priv = sbc->priv;
framelen = sbc_unpack_frame(data, &priv->frame, count);
-
if (!priv->init) {
sbc_decoder_init(&priv->dec_state, &priv->frame);
diff --git a/sbc/sbcdec.c b/sbc/sbcdec.c
index ebf9ae7c..866b8448 100644
--- a/sbc/sbcdec.c
+++ b/sbc/sbcdec.c
@@ -46,7 +46,8 @@ static void decode(char *filename, char *output, int tofile)
struct stat st;
off_t filesize;
sbc_t sbc;
- int fd, ad, pos, streamlen, framelen, count, written, format = AFMT_S16_BE;
+ int fd, ad, pos, streamlen, framelen, count, written;
+ int format = AFMT_S16_BE;
if (stat(filename, &st) < 0) {
fprintf(stderr, "Can't get size of file %s: %s\n",
@@ -101,48 +102,53 @@ static void decode(char *filename, char *output, int tofile)
if (!tofile) {
if (ioctl(ad, SNDCTL_DSP_SETFMT, &format) < 0) {
fprintf(stderr, "Can't set audio format on %s: %s\n",
- output, strerror(errno));
+ output, strerror(errno));
goto close;
}
if (ioctl(ad, SNDCTL_DSP_CHANNELS, &sbc.channels) < 0) {
- fprintf(stderr, "Can't set number of channels on %s: %s\n",
- output, strerror(errno));
+ fprintf(stderr,
+ "Can't set number of channels on %s: %s\n",
+ output, strerror(errno));
goto close;
}
if (ioctl(ad, SNDCTL_DSP_SPEED, &sbc.rate) < 0) {
fprintf(stderr, "Can't set audio rate on %s: %s\n",
- output, strerror(errno));
+ output, strerror(errno));
goto close;
}
}
count = 0;
while (framelen > 0) {
- // we have completed an sbc_decode at this point
- // sbc.len is the length of the frame we just decoded
- // count is the number of decoded bytes yet to be written
+ /* we have completed an sbc_decode at this point sbc.len is the
+ * length of the frame we just decoded count is the number of
+ * decoded bytes yet to be written */
if (count + sbc.len > BUF_SIZE) {
- // buffer is too full to stuff decoded audio in
- // so it must be written to the device
+ /* buffer is too full to stuff decoded audio in so it
+ * must be written to the device */
written = write(ad, buf, count);
if (written > 0)
count -= written;
}
- // sanity check
+ /* sanity check */
if (count + sbc.len > BUF_SIZE) {
- fprintf(stderr, "buffer size of %d is too small for decoded data (%d)\n", BUF_SIZE, sbc.len + count);
+ fprintf(stderr,
+ "buffer size of %d is too small for decoded"
+ " data (%d)\n", BUF_SIZE, sbc.len + count);
exit(1);
}
- // move the latest decoded data into buf and increase the count
+ /* move the latest decoded data into buf and increase
+ * the count */
memcpy(buf + count, sbc.data, sbc.len);
count += sbc.len;
- // push the pointer in the file forward to the next bit to be decoded
- // tell the decoder to decode up to the remaining length of the file (!)
+ /* push the pointer in the file forward to the next bit to be
+ * decoded tell the decoder to decode up to the remaining
+ * length of the file (!) */
pos += framelen;
framelen = sbc_decode(&sbc, stream + pos, streamlen - pos);
}
diff --git a/sbc/sbcenc.c b/sbc/sbcenc.c
index 48e01021..94b9f642 100644
--- a/sbc/sbcenc.c
+++ b/sbc/sbcenc.c
@@ -228,7 +228,8 @@ int main(int argc, char *argv[])
case 's':
subbands = atoi(strdup(optarg));
if (subbands != 8 && subbands != 4) {
- fprintf(stderr, "Invalid subbands %d!\n", subbands);
+ fprintf(stderr, "Invalid subbands %d!\n",
+ subbands);
exit(1);
}
break;
diff --git a/sbc/sbcinfo.c b/sbc/sbcinfo.c
index e8fb398f..7b6d416d 100644
--- a/sbc/sbcinfo.c
+++ b/sbc/sbcinfo.c
@@ -113,7 +113,8 @@ static double calc_bit_rate(struct sbc_frame_hdr *hdr)
return 0;
}
- return ((8 * (calc_frame_len(hdr) + 4) * f) / (nrof_subbands * nrof_blocks));
+ return ((8 * (calc_frame_len(hdr) + 4) * f) /
+ (nrof_subbands * nrof_blocks));
}
static char *freq2str(uint8_t freq)
@@ -226,7 +227,8 @@ static int analyze_file(char *filename)
while (1) {
len = __read(fd, &hdr, sizeof(hdr));
if (len < 0) {
- fprintf(stderr, "Unable to read frame header (error %d)\n", errno);
+ fprintf(stderr, "Unable to read frame header"
+ " (error %d)\n", errno);
break;
}
@@ -234,8 +236,9 @@ static int analyze_file(char *filename)
break;
if (len < sizeof(hdr) || hdr.syncword != 0x9c) {
- fprintf(stderr, "Corrupted SBC stream (len %d syncword 0x%02x)\n",
- len, hdr.syncword);
+ fprintf(stderr, "Corrupted SBC stream "
+ "(len %d syncword 0x%02x)\n",
+ len, hdr.syncword);
break;
}
@@ -260,7 +263,8 @@ static int analyze_file(char *filename)
len = __read(fd, buf, size);
if (len != size) {
- fprintf(stderr, "Unable to read frame data (error %d)\n", errno);
+ fprintf(stderr, "Unable to read frame data "
+ "(error %d)\n", errno);
break;
}
diff --git a/sbc/sbctester.c b/sbc/sbctester.c
index 15e63c2a..af4d1244 100644
--- a/sbc/sbctester.c
+++ b/sbc/sbctester.c
@@ -44,14 +44,14 @@ static double sampletobits(short sample16, int verbose)
if (verbose)
printf("===> sampletobits(%hd, %04hX)\n", sample16, sample16);
- // Bit 0 is MSB
+ /* Bit 0 is MSB */
if (sample16 < 0)
bits = -1;
if (verbose)
printf("%d", (sample16 < 0) ? 1 : 0);
- // Bit 15 is LSB
+ /* Bit 15 is LSB */
for (i = 1; i < 16; i++) {
bit = (unsigned short) sample16;
bit >>= 15 - i;
@@ -104,8 +104,10 @@ static int calculate_rms_level(SNDFILE * sndref, SF_INFO * infosref,
r1 = sf_read_short(sndref, refsample, infostst->channels);
if (r1 != infostst->channels) {
- printf("Failed to read reference data: %s (r1=%d, channels=%d)",
- sf_strerror(sndref), r1, infostst->channels);
+ printf("Failed to read reference data: %s "
+ "(r1=%d, channels=%d)",
+ sf_strerror(sndref), r1,
+ infostst->channels);
if (csv)
fclose(csv);
return -1;
@@ -113,8 +115,10 @@ static int calculate_rms_level(SNDFILE * sndref, SF_INFO * infosref,
r2 = sf_read_short(sndtst, tstsample, infostst->channels);
if (r2 != infostst->channels) {
- printf("Failed to read test data: %s (r2=%d, channels=%d)\n",
- sf_strerror(sndtst), r2, infostst->channels);
+ printf("Failed to read test data: %s "
+ "(r2=%d, channels=%d)\n",
+ sf_strerror(sndtst), r2,
+ infostst->channels);
if (csv)
fclose(csv);
return -1;
@@ -122,7 +126,8 @@ static int calculate_rms_level(SNDFILE * sndref, SF_INFO * infosref,
for (j = 0; j < infostst->channels; j++) {
if (csv)
- fprintf(csv, "%d;%d;", refsample[j], tstsample[j]);
+ fprintf(csv, "%d;%d;", refsample[j],
+ tstsample[j]);
refbits = sampletobits(refsample[j], 0);
tstbits = sampletobits(tstsample[j], 0);
@@ -140,7 +145,8 @@ static int calculate_rms_level(SNDFILE * sndref, SF_INFO * infosref,
printf("Channel %d\n", j);
printf("Accumulated %f\n", rms_accu[j]);
rms_accu[j] /= (double) infostst->frames;
- printf("Accumulated / %f = %f\n", (double) infostst->frames, rms_accu[j]);
+ printf("Accumulated / %f = %f\n", (double) infostst->frames,
+ rms_accu[j]);
rms_level[j] = sqrt(rms_accu[j]);
printf("Level = %f (%f x %f = %f)\n",
rms_level[j], rms_level[j], rms_level[j],
@@ -162,7 +168,8 @@ static int calculate_rms_level(SNDFILE * sndref, SF_INFO * infosref,
}
static int check_absolute_diff(SNDFILE * sndref, SF_INFO * infosref,
- SNDFILE * sndtst, SF_INFO * infostst, int accuracy)
+ SNDFILE * sndtst, SF_INFO * infostst,
+ int accuracy)
{
short refsample[MAXCHANNELS], tstsample[MAXCHANNELS];
short refmax[MAXCHANNELS], tstmax[MAXCHANNELS];
@@ -190,15 +197,19 @@ static int check_absolute_diff(SNDFILE * sndref, SF_INFO * infosref,
r1 = sf_read_short(sndref, refsample, infostst->channels);
if (r1 != infostst->channels) {
- printf("Failed to read reference data: %s (r1=%d, channels=%d)",
- sf_strerror(sndref), r1, infostst->channels);
+ printf("Failed to read reference data: %s "
+ "(r1=%d, channels=%d)",
+ sf_strerror(sndref), r1,
+ infostst->channels);
return -1;
}
r2 = sf_read_short(sndtst, tstsample, infostst->channels);
if (r2 != infostst->channels) {
- printf("Failed to read test data: %s (r2=%d, channels=%d)\n",
- sf_strerror(sndtst), r2, infostst->channels);
+ printf("Failed to read test data: %s "
+ "(r2=%d, channels=%d)\n",
+ sf_strerror(sndtst), r2,
+ infostst->channels);
return -1;
}
@@ -210,7 +221,7 @@ static int check_absolute_diff(SNDFILE * sndref, SF_INFO * infosref,
if (cur_diff > rms_absolute) {
calc_count++;
- //printf("Channel %d exceeded : fabs(%f - %f) = %f > %f\n", j, tstbits, refbits, cur_diff, rms_absolute);
+ /* printf("Channel %d exceeded : fabs(%f - %f) = %f > %f\n", j, tstbits, refbits, cur_diff, rms_absolute); */
verdict = 0;
}
@@ -224,7 +235,8 @@ static int check_absolute_diff(SNDFILE * sndref, SF_INFO * infosref,
for (j = 0; j < infostst->channels; j++) {
printf("Calculated max: %f (%hd-%hd=%hd)\n",
- calc_max[j], tstmax[j], refmax[j], tstmax[j] - refmax[j]);
+ calc_max[j], tstmax[j], refmax[j],
+ tstmax[j] - refmax[j]);
}
printf("%s return %d\n", __FUNCTION__, verdict);
@@ -297,17 +309,19 @@ int main(int argc, char *argv[])
}
printf("reference:\n\t%d frames,\n\t%d hz,\n\t%d channels\n",
- (int) infosref.frames, (int) infosref.samplerate, (int) infosref.channels);
+ (int) infosref.frames, (int) infosref.samplerate,
+ (int) infosref.channels);
printf("testfile:\n\t%d frames,\n\t%d hz,\n\t%d channels\n",
- (int) infostst.frames, (int) infostst.samplerate, (int) infostst.channels);
+ (int) infostst.frames, (int) infostst.samplerate,
+ (int) infostst.channels);
- // check number of channels
+ /* check number of channels */
if (infosref.channels > 2 || infostst.channels > 2) {
printf("Too many channels\n");
goto error;
}
- // compare number of samples
+ /* compare number of samples */
if (infosref.samplerate != infostst.samplerate ||
infosref.channels != infostst.channels) {
printf("Cannot compare files with different charasteristics\n");
@@ -317,17 +331,19 @@ int main(int argc, char *argv[])
accuracy = DEFACCURACY;
printf("Accuracy: %d\n", accuracy);
- // Condition 1 rms level
- pass_rms = calculate_rms_level(sndref, &infosref, sndtst, &infostst, accuracy, "out.csv");
+ /* Condition 1 rms level */
+ pass_rms = calculate_rms_level(sndref, &infosref, sndtst, &infostst,
+ accuracy, "out.csv");
if (pass_rms < 0)
goto error;
- // Condition 2 absolute difference
- pass_absolute = check_absolute_diff(sndref, &infosref, sndtst, &infostst, accuracy);
+ /* Condition 2 absolute difference */
+ pass_absolute = check_absolute_diff(sndref, &infosref, sndtst,
+ &infostst, accuracy);
if (pass_absolute < 0)
goto error;
- // Verdict
+ /* Verdict */
pass = pass_rms && pass_absolute;
printf("Verdict: %s\n", pass ? "pass" : "fail");