Lines Matching +full:8 +full:- +full:ch

1 /*-
4 * This software is dedicated to the memory of -
5 * Baron James Anlezark (Barry) - 1 Jan 1949 - 13 May 2012.
49 #define ABS(x) (((x) < 0) ? -(x) : (x))
51 #define BM(x) ((1LL << (x)) - 1LL)
54 static const int loudnessoffset8[4][8] = {
55 {-2, 0, 0, 0, 0, 0, 0, 1},
56 {-3, 0, 0, 0, 0, 0, 1, 2},
57 {-4, 0, 0, 0, 0, 0, 1, 2},
58 {-4, 0, 0, 0, 0, 0, 1, 2},
62 {-1, 0, 0, 0},
63 {-2, 0, 0, 1},
64 {-2, 0, 0, 1},
65 {-2, 0, 0, 1},
80 for (sb = 0; sb != sbc->bands - 1; sb++) { in calc_scalefactors_joint()
81 for (block = 0; block < sbc->blocks; block++) { in calc_scalefactors_joint()
82 sb_j[block][0] = (sbc->samples[block][0][sb] + in calc_scalefactors_joint()
83 sbc->samples[block][1][sb]) / 2.0f; in calc_scalefactors_joint()
84 sb_j[block][1] = (sbc->samples[block][0][sb] - in calc_scalefactors_joint()
85 sbc->samples[block][1][sb]) / 2.0f; in calc_scalefactors_joint()
90 for (block = 0; block < sbc->blocks; block++) { in calc_scalefactors_joint()
100 x = 16 - lz; in calc_scalefactors_joint()
107 y = 16 - lz; in calc_scalefactors_joint()
109 if ((sbc->scalefactor[0][sb] + sbc->scalefactor[1][sb]) > x + y) { in calc_scalefactors_joint()
110 joint |= 1 << (sbc->bands - sb - 1); in calc_scalefactors_joint()
111 sbc->scalefactor[0][sb] = x; in calc_scalefactors_joint()
112 sbc->scalefactor[1][sb] = y; in calc_scalefactors_joint()
113 for (block = 0; block < sbc->blocks; block++) { in calc_scalefactors_joint()
114 sbc->samples[block][0][sb] = sb_j[block][0]; in calc_scalefactors_joint()
115 sbc->samples[block][1][sb] = sb_j[block][1]; in calc_scalefactors_joint()
126 uint8_t ch; in calc_scalefactors() local
129 for (ch = 0; ch != sbc->channels; ch++) { in calc_scalefactors()
130 for (sb = 0; sb != sbc->bands; sb++) { in calc_scalefactors()
134 for (block = 0; block != sbc->blocks; block++) in calc_scalefactors()
135 x |= (uint32_t)ABS(sbc->samples[block][ch][sb]); in calc_scalefactors()
141 sbc->scalefactor[ch][sb] = 16 - lx; in calc_scalefactors()
149 struct sbc_encode *sbc = cfg->handle.sbc_enc; in calc_bitneed()
150 int32_t bitneed[2][8]; in calc_bitneed()
154 int ch, sb, start_chan = 0; in calc_bitneed() local
156 if (cfg->chmode == MODE_DUAL) in calc_bitneed()
157 sbc->channels = 1; in calc_bitneed()
164 if (cfg->allocm == ALLOC_SNR) { in calc_bitneed()
165 for (ch = start_chan; ch < sbc->channels; ch++) { in calc_bitneed()
166 for (sb = 0; sb < sbc->bands; sb++) { in calc_bitneed()
167 bitneed[ch][sb] = sbc->scalefactor[ch][sb]; in calc_bitneed()
169 if (bitneed[ch][sb] > max_bitneed) in calc_bitneed()
170 max_bitneed = bitneed[ch][sb]; in calc_bitneed()
174 for (ch = start_chan; ch < sbc->channels; ch++) { in calc_bitneed()
175 for (sb = 0; sb < sbc->bands; sb++) { in calc_bitneed()
176 if (sbc->scalefactor[ch][sb] == 0) { in calc_bitneed()
177 bitneed[ch][sb] = -5; in calc_bitneed()
179 if (sbc->bands == 8) { in calc_bitneed()
180 loudness = sbc->scalefactor[ch][sb] - in calc_bitneed()
181 loudnessoffset8[cfg->freq][sb]; in calc_bitneed()
183 loudness = sbc->scalefactor[ch][sb] - in calc_bitneed()
184 loudnessoffset4[cfg->freq][sb]; in calc_bitneed()
187 bitneed[ch][sb] = loudness / 2; in calc_bitneed()
189 bitneed[ch][sb] = loudness; in calc_bitneed()
191 if (bitneed[ch][sb] > max_bitneed) in calc_bitneed()
192 max_bitneed = bitneed[ch][sb]; in calc_bitneed()
200 bitslice--; in calc_bitneed()
203 for (ch = start_chan; ch < sbc->channels; ch++) { in calc_bitneed()
204 for (sb = 0; sb < sbc->bands; sb++) { in calc_bitneed()
205 if ((bitneed[ch][sb] > bitslice + 1) && in calc_bitneed()
206 (bitneed[ch][sb] < bitslice + 16)) in calc_bitneed()
208 else if (bitneed[ch][sb] == bitslice + 1) in calc_bitneed()
212 } while (bitcount + slicecount < cfg->bitpool); in calc_bitneed()
215 if (bitcount + slicecount == cfg->bitpool) { in calc_bitneed()
217 bitslice--; in calc_bitneed()
219 for (ch = start_chan; ch < sbc->channels; ch++) { in calc_bitneed()
220 for (sb = 0; sb < sbc->bands; sb++) { in calc_bitneed()
221 if (bitneed[ch][sb] < bitslice + 2) { in calc_bitneed()
222 sbc->bits[ch][sb] = 0; in calc_bitneed()
224 sbc->bits[ch][sb] = bitneed[ch][sb] - bitslice; in calc_bitneed()
225 if (sbc->bits[ch][sb] > 16) in calc_bitneed()
226 sbc->bits[ch][sb] = 16; in calc_bitneed()
231 if (cfg->chmode == MODE_DUAL) in calc_bitneed()
232 ch = start_chan; in calc_bitneed()
234 ch = 0; in calc_bitneed()
236 while (bitcount < cfg->bitpool && sb < sbc->bands) { in calc_bitneed()
237 if ((sbc->bits[ch][sb] >= 2) && (sbc->bits[ch][sb] < 16)) { in calc_bitneed()
238 sbc->bits[ch][sb]++; in calc_bitneed()
240 } else if ((bitneed[ch][sb] == bitslice + 1) && in calc_bitneed()
241 (cfg->bitpool > bitcount + 1)) { in calc_bitneed()
242 sbc->bits[ch][sb] = 2; in calc_bitneed()
245 if (sbc->channels == 1 || start_chan == 1) in calc_bitneed()
247 else if (ch == 1) { in calc_bitneed()
248 ch = 0; in calc_bitneed()
251 ch = 1; in calc_bitneed()
254 if (cfg->chmode == MODE_DUAL) in calc_bitneed()
255 ch = start_chan; in calc_bitneed()
257 ch = 0; in calc_bitneed()
259 while (bitcount < cfg->bitpool && sb < sbc->bands) { in calc_bitneed()
260 if (sbc->bits[ch][sb] < 16) { in calc_bitneed()
261 sbc->bits[ch][sb]++; in calc_bitneed()
264 if (sbc->channels == 1 || start_chan == 1) in calc_bitneed()
266 else if (ch == 1) { in calc_bitneed()
267 ch = 0; in calc_bitneed()
270 ch = 1; in calc_bitneed()
273 if (cfg->chmode == MODE_DUAL && start_chan == 0) { in calc_bitneed()
275 sbc->channels = 2; in calc_bitneed()
283 uint32_t off = sbc->bitoffset; in sbc_store_bits_crc()
285 while (numbits-- && off != sbc->maxoffset) { in sbc_store_bits_crc()
287 sbc->data[off / 8] |= 1 << ((7 - off) & 7); in sbc_store_bits_crc()
288 sbc->crc ^= 0x80; in sbc_store_bits_crc()
290 sbc->crc *= 2; in sbc_store_bits_crc()
291 if (sbc->crc & 0x100) in sbc_store_bits_crc()
292 sbc->crc ^= 0x11d; /* CRC-8 polynomial */ in sbc_store_bits_crc()
296 sbc->bitoffset = off; in sbc_store_bits_crc()
302 struct sbc_encode *sbc = cfg->handle.sbc_enc; in sbc_encode()
303 const int16_t *input = sbc->music_data; in sbc_encode()
304 float delta[2][8]; in sbc_encode()
305 float levels[2][8]; in sbc_encode()
306 float mask[2][8]; in sbc_encode()
312 int16_t left[8]; in sbc_encode()
313 int16_t right[8]; in sbc_encode()
322 for (block = 0; block < sbc->blocks; block++) { in sbc_encode()
324 for (i = 0; i < sbc->bands; i++) { in sbc_encode()
326 if (sbc->channels == 2) in sbc_encode()
330 for (chan = 0; chan < sbc->channels; chan++) { in sbc_encode()
334 X = sbc->left; in sbc_encode()
337 X = sbc->right; in sbc_encode()
342 for (i = (sbc->bands * 10) - 1; i > sbc->bands - 1; i--) in sbc_encode()
343 X[i] = X[i - sbc->bands]; in sbc_encode()
345 for (i = sbc->bands - 1; i >= 0; i--) in sbc_encode()
347 for (i = 0; i < sbc->bands * 10; i++) { in sbc_encode()
348 if (sbc->bands == 8) in sbc_encode()
353 for (i = 0; i < sbc->bands * 2; i++) { in sbc_encode()
356 Y[i] += Z[i + k * sbc->bands * 2]; in sbc_encode()
358 for (i = 0; i < sbc->bands; i++) { in sbc_encode()
360 for (k = 0; k < sbc->bands * 2; k++) { in sbc_encode()
361 if (sbc->bands == 8) { in sbc_encode()
367 sbc->samples[block][chan][i] = S * (1 << 15); in sbc_encode()
374 if (cfg->chmode == MODE_JOINT) in sbc_encode()
375 sbc->join = calc_scalefactors_joint(sbc); in sbc_encode()
377 sbc->join = 0; in sbc_encode()
381 for (chan = 0; chan < sbc->channels; chan++) { in sbc_encode()
382 for (sb = 0; sb < sbc->bands; sb++) { in sbc_encode()
383 if (sbc->bits[chan][sb] == 0) in sbc_encode()
385 mask[chan][sb] = BM(sbc->bits[chan][sb]); in sbc_encode()
387 (1LL << (15 - sbc->scalefactor[chan][sb])); in sbc_encode()
389 (1LL << (sbc->scalefactor[chan][sb] + 16)); in sbc_encode()
394 for (block = 0; block < sbc->blocks; block++) { in sbc_encode()
395 for (chan = 0; chan < sbc->channels; chan++) { in sbc_encode()
396 for (sb = 0; sb < sbc->bands; sb++) { in sbc_encode()
397 if (sbc->bits[chan][sb] == 0) in sbc_encode()
400 (delta[chan][sb] + sbc->samples[block][chan][sb])); in sbc_encode()
409 sbc->output[numsamples++] = audioout; in sbc_encode()
419 struct sbc_encode *sbc = cfg->handle.sbc_enc; in sbc_decode()
420 float delta[2][8]; in sbc_decode()
421 float levels[2][8]; in sbc_decode()
429 float S[8]; in sbc_decode()
437 for (chan = 0; chan < sbc->channels; chan++) { in sbc_decode()
438 for (sb = 0; sb < sbc->bands; sb++) { in sbc_decode()
439 levels[chan][sb] = (1 << sbc->bits[chan][sb]) - 1; in sbc_decode()
440 delta[chan][sb] = (1 << sbc->scalefactor[chan][sb]); in sbc_decode()
445 for (block = 0; block < sbc->blocks; block++) { in sbc_decode()
446 for (chan = 0; chan < sbc->channels; chan++) { in sbc_decode()
447 for (sb = 0; sb < sbc->bands; sb++) { in sbc_decode()
448 if (sbc->bits[chan][sb] == 0) { in sbc_decode()
452 ((((sbc->output[i] * 2.0f) + 1.0f) * delta[chan][sb]) / in sbc_decode()
453 levels[chan][sb]) - delta[chan][sb]; in sbc_decode()
455 sbc->output[i++] = audioout; in sbc_decode()
460 if (cfg->chmode == MODE_JOINT) { in sbc_decode()
462 while (i < (sbc->blocks * sbc->bands * sbc->channels)) { in sbc_decode()
463 for (sb = 0; sb < sbc->bands; sb++) { in sbc_decode()
464 if (sbc->join & (1 << (sbc->bands - sb - 1))) { in sbc_decode()
465 audioout = sbc->output[i]; in sbc_decode()
466 sbc->output[i] = (2.0f * sbc->output[i]) + in sbc_decode()
467 (2.0f * sbc->output[i + sbc->bands]); in sbc_decode()
468 sbc->output[i + sbc->bands] = in sbc_decode()
469 (2.0f * audioout) - in sbc_decode()
470 (2.0f * sbc->output[i + sbc->bands]); in sbc_decode()
471 sbc->output[i] /= 2.0f; in sbc_decode()
472 sbc->output[i + sbc->bands] /= 2.0f; in sbc_decode()
476 i += sbc->bands; in sbc_decode()
480 for (block = 0; block < sbc->blocks; block++) { in sbc_decode()
481 for (chan = 0; chan < sbc->channels; chan++) { in sbc_decode()
485 V = sbc->left; in sbc_decode()
488 V = sbc->right; in sbc_decode()
490 for (i = 0; i < sbc->bands; i++) in sbc_decode()
491 S[i] = sbc->output[position++]; in sbc_decode()
493 for (i = (sbc->bands * 20) - 1; i >= (sbc->bands * 2); i--) in sbc_decode()
494 V[i] = V[i - (sbc->bands * 2)]; in sbc_decode()
495 for (k = 0; k < sbc->bands * 2; k++) { in sbc_decode()
497 for (i = 0; i < sbc->bands; i++) { in sbc_decode()
498 if (sbc->bands == 8) { in sbc_decode()
507 for (k = 0; k < sbc->bands; k++) { in sbc_decode()
508 U[(i * sbc->bands * 2) + k] = in sbc_decode()
509 V[(i * sbc->bands * 4) + k]; in sbc_decode()
510 U[(i * sbc->bands in sbc_decode()
511 * 2) + sbc->bands + k] = in sbc_decode()
512 V[(i * sbc->bands * 4) + in sbc_decode()
513 (sbc->bands * 3) + k]; in sbc_decode()
516 for (i = 0; i < sbc->bands * 10; i++) { in sbc_decode()
517 if (sbc->bands == 4) { in sbc_decode()
518 W[i] = U[i] * (sbc_coeffs4[i] * -4.0f); in sbc_decode()
519 } else if (sbc->bands == 8) { in sbc_decode()
520 W[i] = U[i] * (sbc_coeffs8[i] * -8.0f); in sbc_decode()
526 for (k = 0; k < sbc->bands; k++) { in sbc_decode()
527 unsigned int offset = k + (block * sbc->bands); in sbc_decode()
531 X[offset] += W[k + (i * sbc->bands)]; in sbc_decode()
536 else if (X[offset] < -32767.0) in sbc_decode()
537 X[offset] = -32767.0; in sbc_decode()
542 for (i = 0, k = 0; k != (sbc->blocks * sbc->bands); k++) { in sbc_decode()
543 sbc->music_data[i++] = left[k]; in sbc_decode()
544 if (sbc->channels == 2) in sbc_decode()
545 sbc->music_data[i++] = right[k]; in sbc_decode()
552 struct sbc_encode *sbc = cfg->handle.sbc_enc; in sbc_encode_frame()
560 config = (cfg->freq << 6) | (cfg->blocks << 4) | in sbc_encode_frame()
561 (cfg->chmode << 2) | (cfg->allocm << 1) | cfg->bands; in sbc_encode_frame()
566 sbc->crc = 0x5e; in sbc_encode_frame()
569 sbc->bitoffset = 0; in sbc_encode_frame()
570 sbc->maxoffset = sizeof(sbc->data) * 8; in sbc_encode_frame()
572 sbc_store_bits_crc(sbc, 8, SYNCWORD); in sbc_encode_frame()
573 sbc_store_bits_crc(sbc, 8, config); in sbc_encode_frame()
574 sbc_store_bits_crc(sbc, 8, cfg->bitpool); in sbc_encode_frame()
576 /* skip 8-bit CRC */ in sbc_encode_frame()
577 sbc->bitoffset += 8; in sbc_encode_frame()
579 if (cfg->chmode == MODE_JOINT) { in sbc_encode_frame()
580 if (sbc->bands == 8) in sbc_encode_frame()
581 sbc_store_bits_crc(sbc, 8, sbc->join); in sbc_encode_frame()
582 else if (sbc->bands == 4) in sbc_encode_frame()
583 sbc_store_bits_crc(sbc, 4, sbc->join); in sbc_encode_frame()
585 for (i = 0; i < sbc->channels; i++) { in sbc_encode_frame()
586 for (j = 0; j < sbc->bands; j++) in sbc_encode_frame()
587 sbc_store_bits_crc(sbc, 4, sbc->scalefactor[i][j]); in sbc_encode_frame()
590 /* store 8-bit CRC */ in sbc_encode_frame()
591 sbc->data[3] = (sbc->crc & 0xFF); in sbc_encode_frame()
594 for (block = 0; block < sbc->blocks; block++) { in sbc_encode_frame()
595 for (chan = 0; chan < sbc->channels; chan++) { in sbc_encode_frame()
596 for (sb = 0; sb < sbc->bands; sb++) { in sbc_encode_frame()
597 if (sbc->bits[chan][sb] == 0) in sbc_encode_frame()
600 sbc_store_bits_crc(sbc, sbc->bits[chan][sb], sbc->output[i++]); in sbc_encode_frame()
604 return ((sbc->bitoffset + 7) / 8); in sbc_encode_frame()
610 uint32_t off = sbc->bitoffset; in sbc_load_bits_crc()
613 while (numbits-- && off != sbc->maxoffset) { in sbc_load_bits_crc()
614 if (sbc->rem_data_ptr[off / 8] & (1 << ((7 - off) & 7))) { in sbc_load_bits_crc()
616 sbc->crc ^= 0x80; in sbc_load_bits_crc()
618 sbc->crc *= 2; in sbc_load_bits_crc()
619 if (sbc->crc & 0x100) in sbc_load_bits_crc()
620 sbc->crc ^= 0x11d; /* CRC-8 polynomial */ in sbc_load_bits_crc()
624 sbc->bitoffset = off; in sbc_load_bits_crc()
631 struct sbc_encode *sbc = cfg->handle.sbc_enc; in sbc_decode_frame()
639 sbc->rem_off = 0; in sbc_decode_frame()
640 sbc->rem_len = 0; in sbc_decode_frame()
642 config = (cfg->freq << 6) | (cfg->blocks << 4) | in sbc_decode_frame()
643 (cfg->chmode << 2) | (cfg->allocm << 1) | cfg->bands; in sbc_decode_frame()
646 sbc->crc = 0x5e; in sbc_decode_frame()
649 sbc->bitoffset = 0; in sbc_decode_frame()
650 sbc->maxoffset = bits; in sbc_decode_frame()
653 if (sbc->maxoffset < (8 * 4)) in sbc_decode_frame()
655 if (sbc_load_bits_crc(sbc, 8) != SYNCWORD) in sbc_decode_frame()
657 if (sbc_load_bits_crc(sbc, 8) != config) in sbc_decode_frame()
659 cfg->bitpool = sbc_load_bits_crc(sbc, 8); in sbc_decode_frame()
661 (void)sbc_load_bits_crc(sbc, 8);/* CRC */ in sbc_decode_frame()
663 if (cfg->chmode == MODE_JOINT) { in sbc_decode_frame()
664 if (sbc->bands == 8) in sbc_decode_frame()
665 sbc->join = sbc_load_bits_crc(sbc, 8); in sbc_decode_frame()
666 else if (sbc->bands == 4) in sbc_decode_frame()
667 sbc->join = sbc_load_bits_crc(sbc, 4); in sbc_decode_frame()
669 sbc->join = 0; in sbc_decode_frame()
671 sbc->join = 0; in sbc_decode_frame()
674 for (i = 0; i < sbc->channels; i++) { in sbc_decode_frame()
675 for (j = 0; j < sbc->bands; j++) in sbc_decode_frame()
676 sbc->scalefactor[i][j] = sbc_load_bits_crc(sbc, 4); in sbc_decode_frame()
682 for (block = 0; block < sbc->blocks; block++) { in sbc_decode_frame()
683 for (chan = 0; chan < sbc->channels; chan++) { in sbc_decode_frame()
684 for (sb = 0; sb < sbc->bands; sb++) { in sbc_decode_frame()
685 if (sbc->bits[chan][sb] == 0) { in sbc_decode_frame()
689 sbc->output[i++] = in sbc_decode_frame()
690 sbc_load_bits_crc(sbc, sbc->bits[chan][sb]); in sbc_decode_frame()
697 sbc->rem_off = 0; in sbc_decode_frame()
698 sbc->rem_len = sbc->blocks * sbc->channels * sbc->bands; in sbc_decode_frame()
700 return ((sbc->bitoffset + 7) / 8); in sbc_decode_frame()