You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
8.8KB

  1. /*
  2. * Bluetooth low-complexity, subband codec (SBC)
  3. *
  4. * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
  5. * Copyright (C) 2012-2013 Intel Corporation
  6. * Copyright (C) 2008-2010 Nokia Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
  9. * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /**
  28. * @file
  29. * SBC common functions for the encoder and decoder
  30. */
  31. #include "avcodec.h"
  32. #include "sbc.h"
  33. /* A2DP specification: Appendix B, page 69 */
  34. static const int sbc_offset4[4][4] = {
  35. { -1, 0, 0, 0 },
  36. { -2, 0, 0, 1 },
  37. { -2, 0, 0, 1 },
  38. { -2, 0, 0, 1 }
  39. };
  40. /* A2DP specification: Appendix B, page 69 */
  41. static const int sbc_offset8[4][8] = {
  42. { -2, 0, 0, 0, 0, 0, 0, 1 },
  43. { -3, 0, 0, 0, 0, 0, 1, 2 },
  44. { -4, 0, 0, 0, 0, 0, 1, 2 },
  45. { -4, 0, 0, 0, 0, 0, 1, 2 }
  46. };
  47. /*
  48. * Calculates the CRC-8 of the first len bits in data
  49. */
  50. uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
  51. {
  52. size_t byte_length = len >> 3;
  53. int bit_length = len & 7;
  54. uint8_t crc;
  55. crc = av_crc(ctx, 0x0F, data, byte_length);
  56. if (bit_length) {
  57. uint8_t bits = data[byte_length];
  58. while (bit_length--) {
  59. int8_t mask = bits ^ crc;
  60. crc = (crc << 1) ^ ((mask >> 7) & 0x1D);
  61. bits <<= 1;
  62. }
  63. }
  64. return crc;
  65. }
  66. /*
  67. * Code straight from the spec to calculate the bits array
  68. * Takes a pointer to the frame in question and a pointer to the bits array
  69. */
  70. void ff_sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
  71. {
  72. int subbands = frame->subbands;
  73. uint8_t sf = frame->frequency;
  74. if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
  75. int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
  76. int ch, sb;
  77. for (ch = 0; ch < frame->channels; ch++) {
  78. max_bitneed = 0;
  79. if (frame->allocation == SNR) {
  80. for (sb = 0; sb < subbands; sb++) {
  81. bitneed[ch][sb] = frame->scale_factor[ch][sb];
  82. if (bitneed[ch][sb] > max_bitneed)
  83. max_bitneed = bitneed[ch][sb];
  84. }
  85. } else {
  86. for (sb = 0; sb < subbands; sb++) {
  87. if (frame->scale_factor[ch][sb] == 0)
  88. bitneed[ch][sb] = -5;
  89. else {
  90. if (subbands == 4)
  91. loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
  92. else
  93. loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
  94. if (loudness > 0)
  95. bitneed[ch][sb] = loudness / 2;
  96. else
  97. bitneed[ch][sb] = loudness;
  98. }
  99. if (bitneed[ch][sb] > max_bitneed)
  100. max_bitneed = bitneed[ch][sb];
  101. }
  102. }
  103. bitcount = 0;
  104. slicecount = 0;
  105. bitslice = max_bitneed + 1;
  106. do {
  107. bitslice--;
  108. bitcount += slicecount;
  109. slicecount = 0;
  110. for (sb = 0; sb < subbands; sb++) {
  111. if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
  112. slicecount++;
  113. else if (bitneed[ch][sb] == bitslice + 1)
  114. slicecount += 2;
  115. }
  116. } while (bitcount + slicecount < frame->bitpool);
  117. if (bitcount + slicecount == frame->bitpool) {
  118. bitcount += slicecount;
  119. bitslice--;
  120. }
  121. for (sb = 0; sb < subbands; sb++) {
  122. if (bitneed[ch][sb] < bitslice + 2)
  123. bits[ch][sb] = 0;
  124. else {
  125. bits[ch][sb] = bitneed[ch][sb] - bitslice;
  126. if (bits[ch][sb] > 16)
  127. bits[ch][sb] = 16;
  128. }
  129. }
  130. for (sb = 0; bitcount < frame->bitpool &&
  131. sb < subbands; sb++) {
  132. if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
  133. bits[ch][sb]++;
  134. bitcount++;
  135. } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
  136. bits[ch][sb] = 2;
  137. bitcount += 2;
  138. }
  139. }
  140. for (sb = 0; bitcount < frame->bitpool &&
  141. sb < subbands; sb++) {
  142. if (bits[ch][sb] < 16) {
  143. bits[ch][sb]++;
  144. bitcount++;
  145. }
  146. }
  147. }
  148. } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
  149. int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
  150. int ch, sb;
  151. max_bitneed = 0;
  152. if (frame->allocation == SNR) {
  153. for (ch = 0; ch < 2; ch++) {
  154. for (sb = 0; sb < subbands; sb++) {
  155. bitneed[ch][sb] = frame->scale_factor[ch][sb];
  156. if (bitneed[ch][sb] > max_bitneed)
  157. max_bitneed = bitneed[ch][sb];
  158. }
  159. }
  160. } else {
  161. for (ch = 0; ch < 2; ch++) {
  162. for (sb = 0; sb < subbands; sb++) {
  163. if (frame->scale_factor[ch][sb] == 0)
  164. bitneed[ch][sb] = -5;
  165. else {
  166. if (subbands == 4)
  167. loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
  168. else
  169. loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
  170. if (loudness > 0)
  171. bitneed[ch][sb] = loudness / 2;
  172. else
  173. bitneed[ch][sb] = loudness;
  174. }
  175. if (bitneed[ch][sb] > max_bitneed)
  176. max_bitneed = bitneed[ch][sb];
  177. }
  178. }
  179. }
  180. bitcount = 0;
  181. slicecount = 0;
  182. bitslice = max_bitneed + 1;
  183. do {
  184. bitslice--;
  185. bitcount += slicecount;
  186. slicecount = 0;
  187. for (ch = 0; ch < 2; ch++) {
  188. for (sb = 0; sb < subbands; sb++) {
  189. if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
  190. slicecount++;
  191. else if (bitneed[ch][sb] == bitslice + 1)
  192. slicecount += 2;
  193. }
  194. }
  195. } while (bitcount + slicecount < frame->bitpool);
  196. if (bitcount + slicecount == frame->bitpool) {
  197. bitcount += slicecount;
  198. bitslice--;
  199. }
  200. for (ch = 0; ch < 2; ch++) {
  201. for (sb = 0; sb < subbands; sb++) {
  202. if (bitneed[ch][sb] < bitslice + 2) {
  203. bits[ch][sb] = 0;
  204. } else {
  205. bits[ch][sb] = bitneed[ch][sb] - bitslice;
  206. if (bits[ch][sb] > 16)
  207. bits[ch][sb] = 16;
  208. }
  209. }
  210. }
  211. ch = 0;
  212. sb = 0;
  213. while (bitcount < frame->bitpool) {
  214. if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
  215. bits[ch][sb]++;
  216. bitcount++;
  217. } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
  218. bits[ch][sb] = 2;
  219. bitcount += 2;
  220. }
  221. if (ch == 1) {
  222. ch = 0;
  223. sb++;
  224. if (sb >= subbands)
  225. break;
  226. } else
  227. ch = 1;
  228. }
  229. ch = 0;
  230. sb = 0;
  231. while (bitcount < frame->bitpool) {
  232. if (bits[ch][sb] < 16) {
  233. bits[ch][sb]++;
  234. bitcount++;
  235. }
  236. if (ch == 1) {
  237. ch = 0;
  238. sb++;
  239. if (sb >= subbands)
  240. break;
  241. } else
  242. ch = 1;
  243. }
  244. }
  245. }