1*9cab9fdeSChristos Margiolis /*-
2*9cab9fdeSChristos Margiolis * Copyright (c) 2015 Nathanial Sloss <nathanialsloss@yahoo.com.au>
3*9cab9fdeSChristos Margiolis *
4*9cab9fdeSChristos Margiolis * This software is dedicated to the memory of -
5*9cab9fdeSChristos Margiolis * Baron James Anlezark (Barry) - 1 Jan 1949 - 13 May 2012.
6*9cab9fdeSChristos Margiolis *
7*9cab9fdeSChristos Margiolis * Barry was a man who loved his music.
8*9cab9fdeSChristos Margiolis *
9*9cab9fdeSChristos Margiolis * Redistribution and use in source and binary forms, with or without
10*9cab9fdeSChristos Margiolis * modification, are permitted provided that the following conditions
11*9cab9fdeSChristos Margiolis * are met:
12*9cab9fdeSChristos Margiolis * 1. Redistributions of source code must retain the above copyright
13*9cab9fdeSChristos Margiolis * notice, this list of conditions and the following disclaimer.
14*9cab9fdeSChristos Margiolis * 2. Redistributions in binary form must reproduce the above copyright
15*9cab9fdeSChristos Margiolis * notice, this list of conditions and the following disclaimer in the
16*9cab9fdeSChristos Margiolis * documentation and/or other materials provided with the distribution.
17*9cab9fdeSChristos Margiolis *
18*9cab9fdeSChristos Margiolis * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*9cab9fdeSChristos Margiolis * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*9cab9fdeSChristos Margiolis * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*9cab9fdeSChristos Margiolis * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*9cab9fdeSChristos Margiolis * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*9cab9fdeSChristos Margiolis * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*9cab9fdeSChristos Margiolis * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*9cab9fdeSChristos Margiolis * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*9cab9fdeSChristos Margiolis * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*9cab9fdeSChristos Margiolis * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*9cab9fdeSChristos Margiolis * POSSIBILITY OF SUCH DAMAGE.
29*9cab9fdeSChristos Margiolis */
30*9cab9fdeSChristos Margiolis
31*9cab9fdeSChristos Margiolis #include <sys/cdefs.h>
32*9cab9fdeSChristos Margiolis #include <sys/types.h>
33*9cab9fdeSChristos Margiolis #include <sys/param.h>
34*9cab9fdeSChristos Margiolis #include <sys/endian.h>
35*9cab9fdeSChristos Margiolis #include <sys/uio.h>
36*9cab9fdeSChristos Margiolis
37*9cab9fdeSChristos Margiolis #include <stdio.h>
38*9cab9fdeSChristos Margiolis #include <errno.h>
39*9cab9fdeSChristos Margiolis #include <stdbool.h>
40*9cab9fdeSChristos Margiolis #include <stdlib.h>
41*9cab9fdeSChristos Margiolis #include <string.h>
42*9cab9fdeSChristos Margiolis #include <unistd.h>
43*9cab9fdeSChristos Margiolis #include <math.h>
44*9cab9fdeSChristos Margiolis
45*9cab9fdeSChristos Margiolis #include "sbc_coeffs.h"
46*9cab9fdeSChristos Margiolis #include "bt.h"
47*9cab9fdeSChristos Margiolis
48*9cab9fdeSChristos Margiolis #define SYNCWORD 0x9c
49*9cab9fdeSChristos Margiolis #define ABS(x) (((x) < 0) ? -(x) : (x))
50*9cab9fdeSChristos Margiolis #define BIT30 (1U << 30)
51*9cab9fdeSChristos Margiolis #define BM(x) ((1LL << (x)) - 1LL)
52*9cab9fdeSChristos Margiolis
53*9cab9fdeSChristos Margiolis /* Loudness offset allocations. */
54*9cab9fdeSChristos Margiolis static const int loudnessoffset8[4][8] = {
55*9cab9fdeSChristos Margiolis {-2, 0, 0, 0, 0, 0, 0, 1},
56*9cab9fdeSChristos Margiolis {-3, 0, 0, 0, 0, 0, 1, 2},
57*9cab9fdeSChristos Margiolis {-4, 0, 0, 0, 0, 0, 1, 2},
58*9cab9fdeSChristos Margiolis {-4, 0, 0, 0, 0, 0, 1, 2},
59*9cab9fdeSChristos Margiolis };
60*9cab9fdeSChristos Margiolis
61*9cab9fdeSChristos Margiolis static const int loudnessoffset4[4][4] = {
62*9cab9fdeSChristos Margiolis {-1, 0, 0, 0},
63*9cab9fdeSChristos Margiolis {-2, 0, 0, 1},
64*9cab9fdeSChristos Margiolis {-2, 0, 0, 1},
65*9cab9fdeSChristos Margiolis {-2, 0, 0, 1},
66*9cab9fdeSChristos Margiolis };
67*9cab9fdeSChristos Margiolis
68*9cab9fdeSChristos Margiolis static uint8_t
calc_scalefactors_joint(struct sbc_encode * sbc)69*9cab9fdeSChristos Margiolis calc_scalefactors_joint(struct sbc_encode *sbc)
70*9cab9fdeSChristos Margiolis {
71*9cab9fdeSChristos Margiolis float sb_j[16][2];
72*9cab9fdeSChristos Margiolis uint32_t x;
73*9cab9fdeSChristos Margiolis uint32_t y;
74*9cab9fdeSChristos Margiolis uint8_t block;
75*9cab9fdeSChristos Margiolis uint8_t joint;
76*9cab9fdeSChristos Margiolis uint8_t sb;
77*9cab9fdeSChristos Margiolis uint8_t lz;
78*9cab9fdeSChristos Margiolis
79*9cab9fdeSChristos Margiolis joint = 0;
80*9cab9fdeSChristos Margiolis for (sb = 0; sb != sbc->bands - 1; sb++) {
81*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
82*9cab9fdeSChristos Margiolis sb_j[block][0] = (sbc->samples[block][0][sb] +
83*9cab9fdeSChristos Margiolis sbc->samples[block][1][sb]) / 2.0f;
84*9cab9fdeSChristos Margiolis sb_j[block][1] = (sbc->samples[block][0][sb] -
85*9cab9fdeSChristos Margiolis sbc->samples[block][1][sb]) / 2.0f;
86*9cab9fdeSChristos Margiolis }
87*9cab9fdeSChristos Margiolis
88*9cab9fdeSChristos Margiolis x = 1 << 15;
89*9cab9fdeSChristos Margiolis y = 1 << 15;
90*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
91*9cab9fdeSChristos Margiolis x |= (uint32_t)ABS(sb_j[block][0]);
92*9cab9fdeSChristos Margiolis y |= (uint32_t)ABS(sb_j[block][1]);
93*9cab9fdeSChristos Margiolis }
94*9cab9fdeSChristos Margiolis
95*9cab9fdeSChristos Margiolis lz = 1;
96*9cab9fdeSChristos Margiolis while (!(x & BIT30)) {
97*9cab9fdeSChristos Margiolis lz++;
98*9cab9fdeSChristos Margiolis x <<= 1;
99*9cab9fdeSChristos Margiolis }
100*9cab9fdeSChristos Margiolis x = 16 - lz;
101*9cab9fdeSChristos Margiolis
102*9cab9fdeSChristos Margiolis lz = 1;
103*9cab9fdeSChristos Margiolis while (!(y & BIT30)) {
104*9cab9fdeSChristos Margiolis lz++;
105*9cab9fdeSChristos Margiolis y <<= 1;
106*9cab9fdeSChristos Margiolis }
107*9cab9fdeSChristos Margiolis y = 16 - lz;
108*9cab9fdeSChristos Margiolis
109*9cab9fdeSChristos Margiolis if ((sbc->scalefactor[0][sb] + sbc->scalefactor[1][sb]) > x + y) {
110*9cab9fdeSChristos Margiolis joint |= 1 << (sbc->bands - sb - 1);
111*9cab9fdeSChristos Margiolis sbc->scalefactor[0][sb] = x;
112*9cab9fdeSChristos Margiolis sbc->scalefactor[1][sb] = y;
113*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
114*9cab9fdeSChristos Margiolis sbc->samples[block][0][sb] = sb_j[block][0];
115*9cab9fdeSChristos Margiolis sbc->samples[block][1][sb] = sb_j[block][1];
116*9cab9fdeSChristos Margiolis }
117*9cab9fdeSChristos Margiolis }
118*9cab9fdeSChristos Margiolis }
119*9cab9fdeSChristos Margiolis return (joint);
120*9cab9fdeSChristos Margiolis }
121*9cab9fdeSChristos Margiolis
122*9cab9fdeSChristos Margiolis static void
calc_scalefactors(struct sbc_encode * sbc)123*9cab9fdeSChristos Margiolis calc_scalefactors(struct sbc_encode *sbc)
124*9cab9fdeSChristos Margiolis {
125*9cab9fdeSChristos Margiolis uint8_t block;
126*9cab9fdeSChristos Margiolis uint8_t ch;
127*9cab9fdeSChristos Margiolis uint8_t sb;
128*9cab9fdeSChristos Margiolis
129*9cab9fdeSChristos Margiolis for (ch = 0; ch != sbc->channels; ch++) {
130*9cab9fdeSChristos Margiolis for (sb = 0; sb != sbc->bands; sb++) {
131*9cab9fdeSChristos Margiolis uint32_t x = 1 << 15;
132*9cab9fdeSChristos Margiolis uint8_t lx = 1;
133*9cab9fdeSChristos Margiolis
134*9cab9fdeSChristos Margiolis for (block = 0; block != sbc->blocks; block++)
135*9cab9fdeSChristos Margiolis x |= (uint32_t)ABS(sbc->samples[block][ch][sb]);
136*9cab9fdeSChristos Margiolis
137*9cab9fdeSChristos Margiolis while (!(x & BIT30)) {
138*9cab9fdeSChristos Margiolis lx++;
139*9cab9fdeSChristos Margiolis x <<= 1;
140*9cab9fdeSChristos Margiolis }
141*9cab9fdeSChristos Margiolis sbc->scalefactor[ch][sb] = 16 - lx;
142*9cab9fdeSChristos Margiolis }
143*9cab9fdeSChristos Margiolis }
144*9cab9fdeSChristos Margiolis }
145*9cab9fdeSChristos Margiolis
146*9cab9fdeSChristos Margiolis static void
calc_bitneed(struct bt_config * cfg)147*9cab9fdeSChristos Margiolis calc_bitneed(struct bt_config *cfg)
148*9cab9fdeSChristos Margiolis {
149*9cab9fdeSChristos Margiolis struct sbc_encode *sbc = cfg->handle.sbc_enc;
150*9cab9fdeSChristos Margiolis int32_t bitneed[2][8];
151*9cab9fdeSChristos Margiolis int32_t max_bitneed, bitcount;
152*9cab9fdeSChristos Margiolis int32_t slicecount, bitslice;
153*9cab9fdeSChristos Margiolis int32_t loudness;
154*9cab9fdeSChristos Margiolis int ch, sb, start_chan = 0;
155*9cab9fdeSChristos Margiolis
156*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_DUAL)
157*9cab9fdeSChristos Margiolis sbc->channels = 1;
158*9cab9fdeSChristos Margiolis
159*9cab9fdeSChristos Margiolis next_chan:
160*9cab9fdeSChristos Margiolis max_bitneed = 0;
161*9cab9fdeSChristos Margiolis bitcount = 0;
162*9cab9fdeSChristos Margiolis slicecount = 0;
163*9cab9fdeSChristos Margiolis
164*9cab9fdeSChristos Margiolis if (cfg->allocm == ALLOC_SNR) {
165*9cab9fdeSChristos Margiolis for (ch = start_chan; ch < sbc->channels; ch++) {
166*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
167*9cab9fdeSChristos Margiolis bitneed[ch][sb] = sbc->scalefactor[ch][sb];
168*9cab9fdeSChristos Margiolis
169*9cab9fdeSChristos Margiolis if (bitneed[ch][sb] > max_bitneed)
170*9cab9fdeSChristos Margiolis max_bitneed = bitneed[ch][sb];
171*9cab9fdeSChristos Margiolis }
172*9cab9fdeSChristos Margiolis }
173*9cab9fdeSChristos Margiolis } else {
174*9cab9fdeSChristos Margiolis for (ch = start_chan; ch < sbc->channels; ch++) {
175*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
176*9cab9fdeSChristos Margiolis if (sbc->scalefactor[ch][sb] == 0) {
177*9cab9fdeSChristos Margiolis bitneed[ch][sb] = -5;
178*9cab9fdeSChristos Margiolis } else {
179*9cab9fdeSChristos Margiolis if (sbc->bands == 8) {
180*9cab9fdeSChristos Margiolis loudness = sbc->scalefactor[ch][sb] -
181*9cab9fdeSChristos Margiolis loudnessoffset8[cfg->freq][sb];
182*9cab9fdeSChristos Margiolis } else {
183*9cab9fdeSChristos Margiolis loudness = sbc->scalefactor[ch][sb] -
184*9cab9fdeSChristos Margiolis loudnessoffset4[cfg->freq][sb];
185*9cab9fdeSChristos Margiolis }
186*9cab9fdeSChristos Margiolis if (loudness > 0)
187*9cab9fdeSChristos Margiolis bitneed[ch][sb] = loudness / 2;
188*9cab9fdeSChristos Margiolis else
189*9cab9fdeSChristos Margiolis bitneed[ch][sb] = loudness;
190*9cab9fdeSChristos Margiolis }
191*9cab9fdeSChristos Margiolis if (bitneed[ch][sb] > max_bitneed)
192*9cab9fdeSChristos Margiolis max_bitneed = bitneed[ch][sb];
193*9cab9fdeSChristos Margiolis }
194*9cab9fdeSChristos Margiolis }
195*9cab9fdeSChristos Margiolis }
196*9cab9fdeSChristos Margiolis
197*9cab9fdeSChristos Margiolis slicecount = bitcount = 0;
198*9cab9fdeSChristos Margiolis bitslice = max_bitneed + 1;
199*9cab9fdeSChristos Margiolis do {
200*9cab9fdeSChristos Margiolis bitslice--;
201*9cab9fdeSChristos Margiolis bitcount += slicecount;
202*9cab9fdeSChristos Margiolis slicecount = 0;
203*9cab9fdeSChristos Margiolis for (ch = start_chan; ch < sbc->channels; ch++) {
204*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
205*9cab9fdeSChristos Margiolis if ((bitneed[ch][sb] > bitslice + 1) &&
206*9cab9fdeSChristos Margiolis (bitneed[ch][sb] < bitslice + 16))
207*9cab9fdeSChristos Margiolis slicecount++;
208*9cab9fdeSChristos Margiolis else if (bitneed[ch][sb] == bitslice + 1)
209*9cab9fdeSChristos Margiolis slicecount += 2;
210*9cab9fdeSChristos Margiolis }
211*9cab9fdeSChristos Margiolis }
212*9cab9fdeSChristos Margiolis } while (bitcount + slicecount < cfg->bitpool);
213*9cab9fdeSChristos Margiolis
214*9cab9fdeSChristos Margiolis /* check if exactly one more fits */
215*9cab9fdeSChristos Margiolis if (bitcount + slicecount == cfg->bitpool) {
216*9cab9fdeSChristos Margiolis bitcount += slicecount;
217*9cab9fdeSChristos Margiolis bitslice--;
218*9cab9fdeSChristos Margiolis }
219*9cab9fdeSChristos Margiolis for (ch = start_chan; ch < sbc->channels; ch++) {
220*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
221*9cab9fdeSChristos Margiolis if (bitneed[ch][sb] < bitslice + 2) {
222*9cab9fdeSChristos Margiolis sbc->bits[ch][sb] = 0;
223*9cab9fdeSChristos Margiolis } else {
224*9cab9fdeSChristos Margiolis sbc->bits[ch][sb] = bitneed[ch][sb] - bitslice;
225*9cab9fdeSChristos Margiolis if (sbc->bits[ch][sb] > 16)
226*9cab9fdeSChristos Margiolis sbc->bits[ch][sb] = 16;
227*9cab9fdeSChristos Margiolis }
228*9cab9fdeSChristos Margiolis }
229*9cab9fdeSChristos Margiolis }
230*9cab9fdeSChristos Margiolis
231*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_DUAL)
232*9cab9fdeSChristos Margiolis ch = start_chan;
233*9cab9fdeSChristos Margiolis else
234*9cab9fdeSChristos Margiolis ch = 0;
235*9cab9fdeSChristos Margiolis sb = 0;
236*9cab9fdeSChristos Margiolis while (bitcount < cfg->bitpool && sb < sbc->bands) {
237*9cab9fdeSChristos Margiolis if ((sbc->bits[ch][sb] >= 2) && (sbc->bits[ch][sb] < 16)) {
238*9cab9fdeSChristos Margiolis sbc->bits[ch][sb]++;
239*9cab9fdeSChristos Margiolis bitcount++;
240*9cab9fdeSChristos Margiolis } else if ((bitneed[ch][sb] == bitslice + 1) &&
241*9cab9fdeSChristos Margiolis (cfg->bitpool > bitcount + 1)) {
242*9cab9fdeSChristos Margiolis sbc->bits[ch][sb] = 2;
243*9cab9fdeSChristos Margiolis bitcount += 2;
244*9cab9fdeSChristos Margiolis }
245*9cab9fdeSChristos Margiolis if (sbc->channels == 1 || start_chan == 1)
246*9cab9fdeSChristos Margiolis sb++;
247*9cab9fdeSChristos Margiolis else if (ch == 1) {
248*9cab9fdeSChristos Margiolis ch = 0;
249*9cab9fdeSChristos Margiolis sb++;
250*9cab9fdeSChristos Margiolis } else
251*9cab9fdeSChristos Margiolis ch = 1;
252*9cab9fdeSChristos Margiolis }
253*9cab9fdeSChristos Margiolis
254*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_DUAL)
255*9cab9fdeSChristos Margiolis ch = start_chan;
256*9cab9fdeSChristos Margiolis else
257*9cab9fdeSChristos Margiolis ch = 0;
258*9cab9fdeSChristos Margiolis sb = 0;
259*9cab9fdeSChristos Margiolis while (bitcount < cfg->bitpool && sb < sbc->bands) {
260*9cab9fdeSChristos Margiolis if (sbc->bits[ch][sb] < 16) {
261*9cab9fdeSChristos Margiolis sbc->bits[ch][sb]++;
262*9cab9fdeSChristos Margiolis bitcount++;
263*9cab9fdeSChristos Margiolis }
264*9cab9fdeSChristos Margiolis if (sbc->channels == 1 || start_chan == 1)
265*9cab9fdeSChristos Margiolis sb++;
266*9cab9fdeSChristos Margiolis else if (ch == 1) {
267*9cab9fdeSChristos Margiolis ch = 0;
268*9cab9fdeSChristos Margiolis sb++;
269*9cab9fdeSChristos Margiolis } else
270*9cab9fdeSChristos Margiolis ch = 1;
271*9cab9fdeSChristos Margiolis }
272*9cab9fdeSChristos Margiolis
273*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_DUAL && start_chan == 0) {
274*9cab9fdeSChristos Margiolis start_chan = 1;
275*9cab9fdeSChristos Margiolis sbc->channels = 2;
276*9cab9fdeSChristos Margiolis goto next_chan;
277*9cab9fdeSChristos Margiolis }
278*9cab9fdeSChristos Margiolis }
279*9cab9fdeSChristos Margiolis
280*9cab9fdeSChristos Margiolis static void
sbc_store_bits_crc(struct sbc_encode * sbc,uint32_t numbits,uint32_t value)281*9cab9fdeSChristos Margiolis sbc_store_bits_crc(struct sbc_encode *sbc, uint32_t numbits, uint32_t value)
282*9cab9fdeSChristos Margiolis {
283*9cab9fdeSChristos Margiolis uint32_t off = sbc->bitoffset;
284*9cab9fdeSChristos Margiolis
285*9cab9fdeSChristos Margiolis while (numbits-- && off != sbc->maxoffset) {
286*9cab9fdeSChristos Margiolis if (value & (1 << numbits)) {
287*9cab9fdeSChristos Margiolis sbc->data[off / 8] |= 1 << ((7 - off) & 7);
288*9cab9fdeSChristos Margiolis sbc->crc ^= 0x80;
289*9cab9fdeSChristos Margiolis }
290*9cab9fdeSChristos Margiolis sbc->crc *= 2;
291*9cab9fdeSChristos Margiolis if (sbc->crc & 0x100)
292*9cab9fdeSChristos Margiolis sbc->crc ^= 0x11d; /* CRC-8 polynomial */
293*9cab9fdeSChristos Margiolis
294*9cab9fdeSChristos Margiolis off++;
295*9cab9fdeSChristos Margiolis }
296*9cab9fdeSChristos Margiolis sbc->bitoffset = off;
297*9cab9fdeSChristos Margiolis }
298*9cab9fdeSChristos Margiolis
299*9cab9fdeSChristos Margiolis static int
sbc_encode(struct bt_config * cfg)300*9cab9fdeSChristos Margiolis sbc_encode(struct bt_config *cfg)
301*9cab9fdeSChristos Margiolis {
302*9cab9fdeSChristos Margiolis struct sbc_encode *sbc = cfg->handle.sbc_enc;
303*9cab9fdeSChristos Margiolis const int16_t *input = sbc->music_data;
304*9cab9fdeSChristos Margiolis float delta[2][8];
305*9cab9fdeSChristos Margiolis float levels[2][8];
306*9cab9fdeSChristos Margiolis float mask[2][8];
307*9cab9fdeSChristos Margiolis float S;
308*9cab9fdeSChristos Margiolis float *X;
309*9cab9fdeSChristos Margiolis float Z[80];
310*9cab9fdeSChristos Margiolis float Y[80];
311*9cab9fdeSChristos Margiolis float audioout;
312*9cab9fdeSChristos Margiolis int16_t left[8];
313*9cab9fdeSChristos Margiolis int16_t right[8];
314*9cab9fdeSChristos Margiolis int16_t *data;
315*9cab9fdeSChristos Margiolis int numsamples;
316*9cab9fdeSChristos Margiolis int i;
317*9cab9fdeSChristos Margiolis int k;
318*9cab9fdeSChristos Margiolis int block;
319*9cab9fdeSChristos Margiolis int chan;
320*9cab9fdeSChristos Margiolis int sb;
321*9cab9fdeSChristos Margiolis
322*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
323*9cab9fdeSChristos Margiolis
324*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->bands; i++) {
325*9cab9fdeSChristos Margiolis left[i] = *input++;
326*9cab9fdeSChristos Margiolis if (sbc->channels == 2)
327*9cab9fdeSChristos Margiolis right[i] = *input++;
328*9cab9fdeSChristos Margiolis }
329*9cab9fdeSChristos Margiolis
330*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
331*9cab9fdeSChristos Margiolis
332*9cab9fdeSChristos Margiolis /* select right or left channel */
333*9cab9fdeSChristos Margiolis if (chan == 0) {
334*9cab9fdeSChristos Margiolis X = sbc->left;
335*9cab9fdeSChristos Margiolis data = left;
336*9cab9fdeSChristos Margiolis } else {
337*9cab9fdeSChristos Margiolis X = sbc->right;
338*9cab9fdeSChristos Margiolis data = right;
339*9cab9fdeSChristos Margiolis }
340*9cab9fdeSChristos Margiolis
341*9cab9fdeSChristos Margiolis /* shift up old data */
342*9cab9fdeSChristos Margiolis for (i = (sbc->bands * 10) - 1; i > sbc->bands - 1; i--)
343*9cab9fdeSChristos Margiolis X[i] = X[i - sbc->bands];
344*9cab9fdeSChristos Margiolis k = 0;
345*9cab9fdeSChristos Margiolis for (i = sbc->bands - 1; i >= 0; i--)
346*9cab9fdeSChristos Margiolis X[i] = data[k++];
347*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->bands * 10; i++) {
348*9cab9fdeSChristos Margiolis if (sbc->bands == 8)
349*9cab9fdeSChristos Margiolis Z[i] = sbc_coeffs8[i] * X[i];
350*9cab9fdeSChristos Margiolis else
351*9cab9fdeSChristos Margiolis Z[i] = sbc_coeffs4[i] * X[i];
352*9cab9fdeSChristos Margiolis }
353*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->bands * 2; i++) {
354*9cab9fdeSChristos Margiolis Y[i] = 0;
355*9cab9fdeSChristos Margiolis for (k = 0; k < 5; k++)
356*9cab9fdeSChristos Margiolis Y[i] += Z[i + k * sbc->bands * 2];
357*9cab9fdeSChristos Margiolis }
358*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->bands; i++) {
359*9cab9fdeSChristos Margiolis S = 0;
360*9cab9fdeSChristos Margiolis for (k = 0; k < sbc->bands * 2; k++) {
361*9cab9fdeSChristos Margiolis if (sbc->bands == 8) {
362*9cab9fdeSChristos Margiolis S += cosdata8[i][k] * Y[k];
363*9cab9fdeSChristos Margiolis } else {
364*9cab9fdeSChristos Margiolis S += cosdata4[i][k] * Y[k];
365*9cab9fdeSChristos Margiolis }
366*9cab9fdeSChristos Margiolis }
367*9cab9fdeSChristos Margiolis sbc->samples[block][chan][i] = S * (1 << 15);
368*9cab9fdeSChristos Margiolis }
369*9cab9fdeSChristos Margiolis }
370*9cab9fdeSChristos Margiolis }
371*9cab9fdeSChristos Margiolis
372*9cab9fdeSChristos Margiolis calc_scalefactors(sbc);
373*9cab9fdeSChristos Margiolis
374*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_JOINT)
375*9cab9fdeSChristos Margiolis sbc->join = calc_scalefactors_joint(sbc);
376*9cab9fdeSChristos Margiolis else
377*9cab9fdeSChristos Margiolis sbc->join = 0;
378*9cab9fdeSChristos Margiolis
379*9cab9fdeSChristos Margiolis calc_bitneed(cfg);
380*9cab9fdeSChristos Margiolis
381*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
382*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
383*9cab9fdeSChristos Margiolis if (sbc->bits[chan][sb] == 0)
384*9cab9fdeSChristos Margiolis continue;
385*9cab9fdeSChristos Margiolis mask[chan][sb] = BM(sbc->bits[chan][sb]);
386*9cab9fdeSChristos Margiolis levels[chan][sb] = mask[chan][sb] *
387*9cab9fdeSChristos Margiolis (1LL << (15 - sbc->scalefactor[chan][sb]));
388*9cab9fdeSChristos Margiolis delta[chan][sb] =
389*9cab9fdeSChristos Margiolis (1LL << (sbc->scalefactor[chan][sb] + 16));
390*9cab9fdeSChristos Margiolis }
391*9cab9fdeSChristos Margiolis }
392*9cab9fdeSChristos Margiolis
393*9cab9fdeSChristos Margiolis numsamples = 0;
394*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
395*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
396*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
397*9cab9fdeSChristos Margiolis if (sbc->bits[chan][sb] == 0)
398*9cab9fdeSChristos Margiolis continue;
399*9cab9fdeSChristos Margiolis audioout = (levels[chan][sb] *
400*9cab9fdeSChristos Margiolis (delta[chan][sb] + sbc->samples[block][chan][sb]));
401*9cab9fdeSChristos Margiolis audioout /= (1LL << 32);
402*9cab9fdeSChristos Margiolis
403*9cab9fdeSChristos Margiolis audioout = roundf(audioout);
404*9cab9fdeSChristos Margiolis
405*9cab9fdeSChristos Margiolis /* range check */
406*9cab9fdeSChristos Margiolis if (audioout > mask[chan][sb])
407*9cab9fdeSChristos Margiolis audioout = mask[chan][sb];
408*9cab9fdeSChristos Margiolis
409*9cab9fdeSChristos Margiolis sbc->output[numsamples++] = audioout;
410*9cab9fdeSChristos Margiolis }
411*9cab9fdeSChristos Margiolis }
412*9cab9fdeSChristos Margiolis }
413*9cab9fdeSChristos Margiolis return (numsamples);
414*9cab9fdeSChristos Margiolis }
415*9cab9fdeSChristos Margiolis
416*9cab9fdeSChristos Margiolis static void
sbc_decode(struct bt_config * cfg)417*9cab9fdeSChristos Margiolis sbc_decode(struct bt_config *cfg)
418*9cab9fdeSChristos Margiolis {
419*9cab9fdeSChristos Margiolis struct sbc_encode *sbc = cfg->handle.sbc_enc;
420*9cab9fdeSChristos Margiolis float delta[2][8];
421*9cab9fdeSChristos Margiolis float levels[2][8];
422*9cab9fdeSChristos Margiolis float audioout;
423*9cab9fdeSChristos Margiolis float *X;
424*9cab9fdeSChristos Margiolis float *V;
425*9cab9fdeSChristos Margiolis float left[160];
426*9cab9fdeSChristos Margiolis float right[160];
427*9cab9fdeSChristos Margiolis float U[160];
428*9cab9fdeSChristos Margiolis float W[160];
429*9cab9fdeSChristos Margiolis float S[8];
430*9cab9fdeSChristos Margiolis int position;
431*9cab9fdeSChristos Margiolis int block;
432*9cab9fdeSChristos Margiolis int chan;
433*9cab9fdeSChristos Margiolis int sb;
434*9cab9fdeSChristos Margiolis int i;
435*9cab9fdeSChristos Margiolis int k;
436*9cab9fdeSChristos Margiolis
437*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
438*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
439*9cab9fdeSChristos Margiolis levels[chan][sb] = (1 << sbc->bits[chan][sb]) - 1;
440*9cab9fdeSChristos Margiolis delta[chan][sb] = (1 << sbc->scalefactor[chan][sb]);
441*9cab9fdeSChristos Margiolis }
442*9cab9fdeSChristos Margiolis }
443*9cab9fdeSChristos Margiolis
444*9cab9fdeSChristos Margiolis i = 0;
445*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
446*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
447*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
448*9cab9fdeSChristos Margiolis if (sbc->bits[chan][sb] == 0) {
449*9cab9fdeSChristos Margiolis audioout = 0;
450*9cab9fdeSChristos Margiolis } else {
451*9cab9fdeSChristos Margiolis audioout =
452*9cab9fdeSChristos Margiolis ((((sbc->output[i] * 2.0f) + 1.0f) * delta[chan][sb]) /
453*9cab9fdeSChristos Margiolis levels[chan][sb]) - delta[chan][sb];
454*9cab9fdeSChristos Margiolis }
455*9cab9fdeSChristos Margiolis sbc->output[i++] = audioout;
456*9cab9fdeSChristos Margiolis }
457*9cab9fdeSChristos Margiolis }
458*9cab9fdeSChristos Margiolis }
459*9cab9fdeSChristos Margiolis
460*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_JOINT) {
461*9cab9fdeSChristos Margiolis i = 0;
462*9cab9fdeSChristos Margiolis while (i < (sbc->blocks * sbc->bands * sbc->channels)) {
463*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
464*9cab9fdeSChristos Margiolis if (sbc->join & (1 << (sbc->bands - sb - 1))) {
465*9cab9fdeSChristos Margiolis audioout = sbc->output[i];
466*9cab9fdeSChristos Margiolis sbc->output[i] = (2.0f * sbc->output[i]) +
467*9cab9fdeSChristos Margiolis (2.0f * sbc->output[i + sbc->bands]);
468*9cab9fdeSChristos Margiolis sbc->output[i + sbc->bands] =
469*9cab9fdeSChristos Margiolis (2.0f * audioout) -
470*9cab9fdeSChristos Margiolis (2.0f * sbc->output[i + sbc->bands]);
471*9cab9fdeSChristos Margiolis sbc->output[i] /= 2.0f;
472*9cab9fdeSChristos Margiolis sbc->output[i + sbc->bands] /= 2.0f;
473*9cab9fdeSChristos Margiolis }
474*9cab9fdeSChristos Margiolis i++;
475*9cab9fdeSChristos Margiolis }
476*9cab9fdeSChristos Margiolis i += sbc->bands;
477*9cab9fdeSChristos Margiolis }
478*9cab9fdeSChristos Margiolis }
479*9cab9fdeSChristos Margiolis position = 0;
480*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
481*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
482*9cab9fdeSChristos Margiolis /* select right or left channel */
483*9cab9fdeSChristos Margiolis if (chan == 0) {
484*9cab9fdeSChristos Margiolis X = left;
485*9cab9fdeSChristos Margiolis V = sbc->left;
486*9cab9fdeSChristos Margiolis } else {
487*9cab9fdeSChristos Margiolis X = right;
488*9cab9fdeSChristos Margiolis V = sbc->right;
489*9cab9fdeSChristos Margiolis }
490*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->bands; i++)
491*9cab9fdeSChristos Margiolis S[i] = sbc->output[position++];
492*9cab9fdeSChristos Margiolis
493*9cab9fdeSChristos Margiolis for (i = (sbc->bands * 20) - 1; i >= (sbc->bands * 2); i--)
494*9cab9fdeSChristos Margiolis V[i] = V[i - (sbc->bands * 2)];
495*9cab9fdeSChristos Margiolis for (k = 0; k < sbc->bands * 2; k++) {
496*9cab9fdeSChristos Margiolis float vk = 0;
497*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->bands; i++) {
498*9cab9fdeSChristos Margiolis if (sbc->bands == 8) {
499*9cab9fdeSChristos Margiolis vk += cosdecdata8[i][k] * S[i];
500*9cab9fdeSChristos Margiolis } else {
501*9cab9fdeSChristos Margiolis vk += cosdecdata4[i][k] * S[i];
502*9cab9fdeSChristos Margiolis }
503*9cab9fdeSChristos Margiolis }
504*9cab9fdeSChristos Margiolis V[k] = vk;
505*9cab9fdeSChristos Margiolis }
506*9cab9fdeSChristos Margiolis for (i = 0; i <= 4; i++) {
507*9cab9fdeSChristos Margiolis for (k = 0; k < sbc->bands; k++) {
508*9cab9fdeSChristos Margiolis U[(i * sbc->bands * 2) + k] =
509*9cab9fdeSChristos Margiolis V[(i * sbc->bands * 4) + k];
510*9cab9fdeSChristos Margiolis U[(i * sbc->bands
511*9cab9fdeSChristos Margiolis * 2) + sbc->bands + k] =
512*9cab9fdeSChristos Margiolis V[(i * sbc->bands * 4) +
513*9cab9fdeSChristos Margiolis (sbc->bands * 3) + k];
514*9cab9fdeSChristos Margiolis }
515*9cab9fdeSChristos Margiolis }
516*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->bands * 10; i++) {
517*9cab9fdeSChristos Margiolis if (sbc->bands == 4) {
518*9cab9fdeSChristos Margiolis W[i] = U[i] * (sbc_coeffs4[i] * -4.0f);
519*9cab9fdeSChristos Margiolis } else if (sbc->bands == 8) {
520*9cab9fdeSChristos Margiolis W[i] = U[i] * (sbc_coeffs8[i] * -8.0f);
521*9cab9fdeSChristos Margiolis } else {
522*9cab9fdeSChristos Margiolis W[i] = 0;
523*9cab9fdeSChristos Margiolis }
524*9cab9fdeSChristos Margiolis }
525*9cab9fdeSChristos Margiolis
526*9cab9fdeSChristos Margiolis for (k = 0; k < sbc->bands; k++) {
527*9cab9fdeSChristos Margiolis unsigned int offset = k + (block * sbc->bands);
528*9cab9fdeSChristos Margiolis
529*9cab9fdeSChristos Margiolis X[offset] = 0;
530*9cab9fdeSChristos Margiolis for (i = 0; i < 10; i++) {
531*9cab9fdeSChristos Margiolis X[offset] += W[k + (i * sbc->bands)];
532*9cab9fdeSChristos Margiolis }
533*9cab9fdeSChristos Margiolis
534*9cab9fdeSChristos Margiolis if (X[offset] > 32767.0)
535*9cab9fdeSChristos Margiolis X[offset] = 32767.0;
536*9cab9fdeSChristos Margiolis else if (X[offset] < -32767.0)
537*9cab9fdeSChristos Margiolis X[offset] = -32767.0;
538*9cab9fdeSChristos Margiolis }
539*9cab9fdeSChristos Margiolis }
540*9cab9fdeSChristos Margiolis }
541*9cab9fdeSChristos Margiolis
542*9cab9fdeSChristos Margiolis for (i = 0, k = 0; k != (sbc->blocks * sbc->bands); k++) {
543*9cab9fdeSChristos Margiolis sbc->music_data[i++] = left[k];
544*9cab9fdeSChristos Margiolis if (sbc->channels == 2)
545*9cab9fdeSChristos Margiolis sbc->music_data[i++] = right[k];
546*9cab9fdeSChristos Margiolis }
547*9cab9fdeSChristos Margiolis }
548*9cab9fdeSChristos Margiolis
549*9cab9fdeSChristos Margiolis size_t
sbc_encode_frame(struct bt_config * cfg)550*9cab9fdeSChristos Margiolis sbc_encode_frame(struct bt_config *cfg)
551*9cab9fdeSChristos Margiolis {
552*9cab9fdeSChristos Margiolis struct sbc_encode *sbc = cfg->handle.sbc_enc;
553*9cab9fdeSChristos Margiolis uint8_t config;
554*9cab9fdeSChristos Margiolis uint8_t block;
555*9cab9fdeSChristos Margiolis uint8_t chan;
556*9cab9fdeSChristos Margiolis uint8_t sb;
557*9cab9fdeSChristos Margiolis uint8_t j;
558*9cab9fdeSChristos Margiolis uint8_t i;
559*9cab9fdeSChristos Margiolis
560*9cab9fdeSChristos Margiolis config = (cfg->freq << 6) | (cfg->blocks << 4) |
561*9cab9fdeSChristos Margiolis (cfg->chmode << 2) | (cfg->allocm << 1) | cfg->bands;
562*9cab9fdeSChristos Margiolis
563*9cab9fdeSChristos Margiolis sbc_encode(cfg);
564*9cab9fdeSChristos Margiolis
565*9cab9fdeSChristos Margiolis /* set initial CRC */
566*9cab9fdeSChristos Margiolis sbc->crc = 0x5e;
567*9cab9fdeSChristos Margiolis
568*9cab9fdeSChristos Margiolis /* reset data position and size */
569*9cab9fdeSChristos Margiolis sbc->bitoffset = 0;
570*9cab9fdeSChristos Margiolis sbc->maxoffset = sizeof(sbc->data) * 8;
571*9cab9fdeSChristos Margiolis
572*9cab9fdeSChristos Margiolis sbc_store_bits_crc(sbc, 8, SYNCWORD);
573*9cab9fdeSChristos Margiolis sbc_store_bits_crc(sbc, 8, config);
574*9cab9fdeSChristos Margiolis sbc_store_bits_crc(sbc, 8, cfg->bitpool);
575*9cab9fdeSChristos Margiolis
576*9cab9fdeSChristos Margiolis /* skip 8-bit CRC */
577*9cab9fdeSChristos Margiolis sbc->bitoffset += 8;
578*9cab9fdeSChristos Margiolis
579*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_JOINT) {
580*9cab9fdeSChristos Margiolis if (sbc->bands == 8)
581*9cab9fdeSChristos Margiolis sbc_store_bits_crc(sbc, 8, sbc->join);
582*9cab9fdeSChristos Margiolis else if (sbc->bands == 4)
583*9cab9fdeSChristos Margiolis sbc_store_bits_crc(sbc, 4, sbc->join);
584*9cab9fdeSChristos Margiolis }
585*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->channels; i++) {
586*9cab9fdeSChristos Margiolis for (j = 0; j < sbc->bands; j++)
587*9cab9fdeSChristos Margiolis sbc_store_bits_crc(sbc, 4, sbc->scalefactor[i][j]);
588*9cab9fdeSChristos Margiolis }
589*9cab9fdeSChristos Margiolis
590*9cab9fdeSChristos Margiolis /* store 8-bit CRC */
591*9cab9fdeSChristos Margiolis sbc->data[3] = (sbc->crc & 0xFF);
592*9cab9fdeSChristos Margiolis
593*9cab9fdeSChristos Margiolis i = 0;
594*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
595*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
596*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
597*9cab9fdeSChristos Margiolis if (sbc->bits[chan][sb] == 0)
598*9cab9fdeSChristos Margiolis continue;
599*9cab9fdeSChristos Margiolis
600*9cab9fdeSChristos Margiolis sbc_store_bits_crc(sbc, sbc->bits[chan][sb], sbc->output[i++]);
601*9cab9fdeSChristos Margiolis }
602*9cab9fdeSChristos Margiolis }
603*9cab9fdeSChristos Margiolis }
604*9cab9fdeSChristos Margiolis return ((sbc->bitoffset + 7) / 8);
605*9cab9fdeSChristos Margiolis }
606*9cab9fdeSChristos Margiolis
607*9cab9fdeSChristos Margiolis static uint32_t
sbc_load_bits_crc(struct sbc_encode * sbc,uint32_t numbits)608*9cab9fdeSChristos Margiolis sbc_load_bits_crc(struct sbc_encode *sbc, uint32_t numbits)
609*9cab9fdeSChristos Margiolis {
610*9cab9fdeSChristos Margiolis uint32_t off = sbc->bitoffset;
611*9cab9fdeSChristos Margiolis uint32_t value = 0;
612*9cab9fdeSChristos Margiolis
613*9cab9fdeSChristos Margiolis while (numbits-- && off != sbc->maxoffset) {
614*9cab9fdeSChristos Margiolis if (sbc->rem_data_ptr[off / 8] & (1 << ((7 - off) & 7))) {
615*9cab9fdeSChristos Margiolis value |= (1 << numbits);
616*9cab9fdeSChristos Margiolis sbc->crc ^= 0x80;
617*9cab9fdeSChristos Margiolis }
618*9cab9fdeSChristos Margiolis sbc->crc *= 2;
619*9cab9fdeSChristos Margiolis if (sbc->crc & 0x100)
620*9cab9fdeSChristos Margiolis sbc->crc ^= 0x11d; /* CRC-8 polynomial */
621*9cab9fdeSChristos Margiolis
622*9cab9fdeSChristos Margiolis off++;
623*9cab9fdeSChristos Margiolis }
624*9cab9fdeSChristos Margiolis sbc->bitoffset = off;
625*9cab9fdeSChristos Margiolis return (value);
626*9cab9fdeSChristos Margiolis }
627*9cab9fdeSChristos Margiolis
628*9cab9fdeSChristos Margiolis size_t
sbc_decode_frame(struct bt_config * cfg,int bits)629*9cab9fdeSChristos Margiolis sbc_decode_frame(struct bt_config *cfg, int bits)
630*9cab9fdeSChristos Margiolis {
631*9cab9fdeSChristos Margiolis struct sbc_encode *sbc = cfg->handle.sbc_enc;
632*9cab9fdeSChristos Margiolis uint8_t config;
633*9cab9fdeSChristos Margiolis uint8_t block;
634*9cab9fdeSChristos Margiolis uint8_t chan;
635*9cab9fdeSChristos Margiolis uint8_t sb;
636*9cab9fdeSChristos Margiolis uint8_t j;
637*9cab9fdeSChristos Margiolis uint8_t i;
638*9cab9fdeSChristos Margiolis
639*9cab9fdeSChristos Margiolis sbc->rem_off = 0;
640*9cab9fdeSChristos Margiolis sbc->rem_len = 0;
641*9cab9fdeSChristos Margiolis
642*9cab9fdeSChristos Margiolis config = (cfg->freq << 6) | (cfg->blocks << 4) |
643*9cab9fdeSChristos Margiolis (cfg->chmode << 2) | (cfg->allocm << 1) | cfg->bands;
644*9cab9fdeSChristos Margiolis
645*9cab9fdeSChristos Margiolis /* set initial CRC */
646*9cab9fdeSChristos Margiolis sbc->crc = 0x5e;
647*9cab9fdeSChristos Margiolis
648*9cab9fdeSChristos Margiolis /* reset data position and size */
649*9cab9fdeSChristos Margiolis sbc->bitoffset = 0;
650*9cab9fdeSChristos Margiolis sbc->maxoffset = bits;
651*9cab9fdeSChristos Margiolis
652*9cab9fdeSChristos Margiolis /* verify SBC header */
653*9cab9fdeSChristos Margiolis if (sbc->maxoffset < (8 * 4))
654*9cab9fdeSChristos Margiolis return (0);
655*9cab9fdeSChristos Margiolis if (sbc_load_bits_crc(sbc, 8) != SYNCWORD)
656*9cab9fdeSChristos Margiolis return (0);
657*9cab9fdeSChristos Margiolis if (sbc_load_bits_crc(sbc, 8) != config)
658*9cab9fdeSChristos Margiolis return (0);
659*9cab9fdeSChristos Margiolis cfg->bitpool = sbc_load_bits_crc(sbc, 8);
660*9cab9fdeSChristos Margiolis
661*9cab9fdeSChristos Margiolis (void)sbc_load_bits_crc(sbc, 8);/* CRC */
662*9cab9fdeSChristos Margiolis
663*9cab9fdeSChristos Margiolis if (cfg->chmode == MODE_JOINT) {
664*9cab9fdeSChristos Margiolis if (sbc->bands == 8)
665*9cab9fdeSChristos Margiolis sbc->join = sbc_load_bits_crc(sbc, 8);
666*9cab9fdeSChristos Margiolis else if (sbc->bands == 4)
667*9cab9fdeSChristos Margiolis sbc->join = sbc_load_bits_crc(sbc, 4);
668*9cab9fdeSChristos Margiolis else
669*9cab9fdeSChristos Margiolis sbc->join = 0;
670*9cab9fdeSChristos Margiolis } else {
671*9cab9fdeSChristos Margiolis sbc->join = 0;
672*9cab9fdeSChristos Margiolis }
673*9cab9fdeSChristos Margiolis
674*9cab9fdeSChristos Margiolis for (i = 0; i < sbc->channels; i++) {
675*9cab9fdeSChristos Margiolis for (j = 0; j < sbc->bands; j++)
676*9cab9fdeSChristos Margiolis sbc->scalefactor[i][j] = sbc_load_bits_crc(sbc, 4);
677*9cab9fdeSChristos Margiolis }
678*9cab9fdeSChristos Margiolis
679*9cab9fdeSChristos Margiolis calc_bitneed(cfg);
680*9cab9fdeSChristos Margiolis
681*9cab9fdeSChristos Margiolis i = 0;
682*9cab9fdeSChristos Margiolis for (block = 0; block < sbc->blocks; block++) {
683*9cab9fdeSChristos Margiolis for (chan = 0; chan < sbc->channels; chan++) {
684*9cab9fdeSChristos Margiolis for (sb = 0; sb < sbc->bands; sb++) {
685*9cab9fdeSChristos Margiolis if (sbc->bits[chan][sb] == 0) {
686*9cab9fdeSChristos Margiolis i++;
687*9cab9fdeSChristos Margiolis continue;
688*9cab9fdeSChristos Margiolis }
689*9cab9fdeSChristos Margiolis sbc->output[i++] =
690*9cab9fdeSChristos Margiolis sbc_load_bits_crc(sbc, sbc->bits[chan][sb]);
691*9cab9fdeSChristos Margiolis }
692*9cab9fdeSChristos Margiolis }
693*9cab9fdeSChristos Margiolis }
694*9cab9fdeSChristos Margiolis
695*9cab9fdeSChristos Margiolis sbc_decode(cfg);
696*9cab9fdeSChristos Margiolis
697*9cab9fdeSChristos Margiolis sbc->rem_off = 0;
698*9cab9fdeSChristos Margiolis sbc->rem_len = sbc->blocks * sbc->channels * sbc->bands;
699*9cab9fdeSChristos Margiolis
700*9cab9fdeSChristos Margiolis return ((sbc->bitoffset + 7) / 8);
701*9cab9fdeSChristos Margiolis }
702