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.

388 lines
14KB

  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-2006 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 basic "building bricks"
  30. */
  31. #include <stdint.h>
  32. #include <limits.h>
  33. #include <string.h>
  34. #include "libavutil/common.h"
  35. #include "libavutil/intmath.h"
  36. #include "libavutil/intreadwrite.h"
  37. #include "sbc.h"
  38. #include "sbcdsp.h"
  39. #include "sbcdsp_data.h"
  40. /*
  41. * A reference C code of analysis filter with SIMD-friendly tables
  42. * reordering and code layout. This code can be used to develop platform
  43. * specific SIMD optimizations. Also it may be used as some kind of test
  44. * for compiler autovectorization capabilities (who knows, if the compiler
  45. * is very good at this stuff, hand optimized assembly may be not strictly
  46. * needed for some platform).
  47. *
  48. * Note: It is also possible to make a simple variant of analysis filter,
  49. * which needs only a single constants table without taking care about
  50. * even/odd cases. This simple variant of filter can be implemented without
  51. * input data permutation. The only thing that would be lost is the
  52. * possibility to use pairwise SIMD multiplications. But for some simple
  53. * CPU cores without SIMD extensions it can be useful. If anybody is
  54. * interested in implementing such variant of a filter, sourcecode from
  55. * bluez versions 4.26/4.27 can be used as a reference and the history of
  56. * the changes in git repository done around that time may be worth checking.
  57. */
  58. static av_always_inline void sbc_analyze_simd(const int16_t *in, int32_t *out,
  59. const int16_t *consts,
  60. unsigned subbands)
  61. {
  62. int32_t t1[8];
  63. int16_t t2[8];
  64. int i, j, hop = 0;
  65. /* rounding coefficient */
  66. for (i = 0; i < subbands; i++)
  67. t1[i] = 1 << (SBC_PROTO_FIXED_SCALE - 1);
  68. /* low pass polyphase filter */
  69. for (hop = 0; hop < 10*subbands; hop += 2*subbands)
  70. for (i = 0; i < 2*subbands; i++)
  71. t1[i >> 1] += in[hop + i] * consts[hop + i];
  72. /* scaling */
  73. for (i = 0; i < subbands; i++)
  74. t2[i] = t1[i] >> SBC_PROTO_FIXED_SCALE;
  75. memset(t1, 0, sizeof(t1));
  76. /* do the cos transform */
  77. for (i = 0; i < subbands/2; i++)
  78. for (j = 0; j < 2*subbands; j++)
  79. t1[j>>1] += t2[i * 2 + (j&1)] * consts[10*subbands + i*2*subbands + j];
  80. for (i = 0; i < subbands; i++)
  81. out[i] = t1[i] >> (SBC_COS_TABLE_FIXED_SCALE - SCALE_OUT_BITS);
  82. }
  83. static void sbc_analyze_4_simd(const int16_t *in, int32_t *out,
  84. const int16_t *consts)
  85. {
  86. sbc_analyze_simd(in, out, consts, 4);
  87. }
  88. static void sbc_analyze_8_simd(const int16_t *in, int32_t *out,
  89. const int16_t *consts)
  90. {
  91. sbc_analyze_simd(in, out, consts, 8);
  92. }
  93. static inline void sbc_analyze_4b_4s_simd(SBCDSPContext *s,
  94. int16_t *x, int32_t *out, int out_stride)
  95. {
  96. /* Analyze blocks */
  97. s->sbc_analyze_4(x + 12, out, ff_sbcdsp_analysis_consts_fixed4_simd_odd);
  98. out += out_stride;
  99. s->sbc_analyze_4(x + 8, out, ff_sbcdsp_analysis_consts_fixed4_simd_even);
  100. out += out_stride;
  101. s->sbc_analyze_4(x + 4, out, ff_sbcdsp_analysis_consts_fixed4_simd_odd);
  102. out += out_stride;
  103. s->sbc_analyze_4(x + 0, out, ff_sbcdsp_analysis_consts_fixed4_simd_even);
  104. }
  105. static inline void sbc_analyze_4b_8s_simd(SBCDSPContext *s,
  106. int16_t *x, int32_t *out, int out_stride)
  107. {
  108. /* Analyze blocks */
  109. s->sbc_analyze_8(x + 24, out, ff_sbcdsp_analysis_consts_fixed8_simd_odd);
  110. out += out_stride;
  111. s->sbc_analyze_8(x + 16, out, ff_sbcdsp_analysis_consts_fixed8_simd_even);
  112. out += out_stride;
  113. s->sbc_analyze_8(x + 8, out, ff_sbcdsp_analysis_consts_fixed8_simd_odd);
  114. out += out_stride;
  115. s->sbc_analyze_8(x + 0, out, ff_sbcdsp_analysis_consts_fixed8_simd_even);
  116. }
  117. static inline void sbc_analyze_1b_8s_simd_even(SBCDSPContext *s,
  118. int16_t *x, int32_t *out,
  119. int out_stride);
  120. static inline void sbc_analyze_1b_8s_simd_odd(SBCDSPContext *s,
  121. int16_t *x, int32_t *out,
  122. int out_stride)
  123. {
  124. s->sbc_analyze_8(x, out, ff_sbcdsp_analysis_consts_fixed8_simd_odd);
  125. s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_even;
  126. }
  127. static inline void sbc_analyze_1b_8s_simd_even(SBCDSPContext *s,
  128. int16_t *x, int32_t *out,
  129. int out_stride)
  130. {
  131. s->sbc_analyze_8(x, out, ff_sbcdsp_analysis_consts_fixed8_simd_even);
  132. s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_odd;
  133. }
  134. /*
  135. * Input data processing functions. The data is endian converted if needed,
  136. * channels are deintrleaved and audio samples are reordered for use in
  137. * SIMD-friendly analysis filter function. The results are put into "X"
  138. * array, getting appended to the previous data (or it is better to say
  139. * prepended, as the buffer is filled from top to bottom). Old data is
  140. * discarded when neededed, but availability of (10 * nrof_subbands)
  141. * contiguous samples is always guaranteed for the input to the analysis
  142. * filter. This is achieved by copying a sufficient part of old data
  143. * to the top of the buffer on buffer wraparound.
  144. */
  145. static int sbc_enc_process_input_4s(int position, const uint8_t *pcm,
  146. int16_t X[2][SBC_X_BUFFER_SIZE],
  147. int nsamples, int nchannels)
  148. {
  149. int c;
  150. /* handle X buffer wraparound */
  151. if (position < nsamples) {
  152. for (c = 0; c < nchannels; c++)
  153. memcpy(&X[c][SBC_X_BUFFER_SIZE - 40], &X[c][position],
  154. 36 * sizeof(int16_t));
  155. position = SBC_X_BUFFER_SIZE - 40;
  156. }
  157. /* copy/permutate audio samples */
  158. for (; nsamples >= 8; nsamples -= 8, pcm += 16 * nchannels) {
  159. position -= 8;
  160. for (c = 0; c < nchannels; c++) {
  161. int16_t *x = &X[c][position];
  162. x[0] = AV_RN16(pcm + 14*nchannels + 2*c);
  163. x[1] = AV_RN16(pcm + 6*nchannels + 2*c);
  164. x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
  165. x[3] = AV_RN16(pcm + 8*nchannels + 2*c);
  166. x[4] = AV_RN16(pcm + 0*nchannels + 2*c);
  167. x[5] = AV_RN16(pcm + 4*nchannels + 2*c);
  168. x[6] = AV_RN16(pcm + 2*nchannels + 2*c);
  169. x[7] = AV_RN16(pcm + 10*nchannels + 2*c);
  170. }
  171. }
  172. return position;
  173. }
  174. static int sbc_enc_process_input_8s(int position, const uint8_t *pcm,
  175. int16_t X[2][SBC_X_BUFFER_SIZE],
  176. int nsamples, int nchannels)
  177. {
  178. int c;
  179. /* handle X buffer wraparound */
  180. if (position < nsamples) {
  181. for (c = 0; c < nchannels; c++)
  182. memcpy(&X[c][SBC_X_BUFFER_SIZE - 72], &X[c][position],
  183. 72 * sizeof(int16_t));
  184. position = SBC_X_BUFFER_SIZE - 72;
  185. }
  186. if (position % 16 == 8) {
  187. position -= 8;
  188. nsamples -= 8;
  189. for (c = 0; c < nchannels; c++) {
  190. int16_t *x = &X[c][position];
  191. x[0] = AV_RN16(pcm + 14*nchannels + 2*c);
  192. x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
  193. x[3] = AV_RN16(pcm + 0*nchannels + 2*c);
  194. x[4] = AV_RN16(pcm + 10*nchannels + 2*c);
  195. x[5] = AV_RN16(pcm + 2*nchannels + 2*c);
  196. x[6] = AV_RN16(pcm + 8*nchannels + 2*c);
  197. x[7] = AV_RN16(pcm + 4*nchannels + 2*c);
  198. x[8] = AV_RN16(pcm + 6*nchannels + 2*c);
  199. }
  200. pcm += 16 * nchannels;
  201. }
  202. /* copy/permutate audio samples */
  203. for (; nsamples >= 16; nsamples -= 16, pcm += 32 * nchannels) {
  204. position -= 16;
  205. for (c = 0; c < nchannels; c++) {
  206. int16_t *x = &X[c][position];
  207. x[0] = AV_RN16(pcm + 30*nchannels + 2*c);
  208. x[1] = AV_RN16(pcm + 14*nchannels + 2*c);
  209. x[2] = AV_RN16(pcm + 28*nchannels + 2*c);
  210. x[3] = AV_RN16(pcm + 16*nchannels + 2*c);
  211. x[4] = AV_RN16(pcm + 26*nchannels + 2*c);
  212. x[5] = AV_RN16(pcm + 18*nchannels + 2*c);
  213. x[6] = AV_RN16(pcm + 24*nchannels + 2*c);
  214. x[7] = AV_RN16(pcm + 20*nchannels + 2*c);
  215. x[8] = AV_RN16(pcm + 22*nchannels + 2*c);
  216. x[9] = AV_RN16(pcm + 6*nchannels + 2*c);
  217. x[10] = AV_RN16(pcm + 12*nchannels + 2*c);
  218. x[11] = AV_RN16(pcm + 0*nchannels + 2*c);
  219. x[12] = AV_RN16(pcm + 10*nchannels + 2*c);
  220. x[13] = AV_RN16(pcm + 2*nchannels + 2*c);
  221. x[14] = AV_RN16(pcm + 8*nchannels + 2*c);
  222. x[15] = AV_RN16(pcm + 4*nchannels + 2*c);
  223. }
  224. }
  225. if (nsamples == 8) {
  226. position -= 8;
  227. for (c = 0; c < nchannels; c++) {
  228. int16_t *x = &X[c][position];
  229. x[-7] = AV_RN16(pcm + 14*nchannels + 2*c);
  230. x[1] = AV_RN16(pcm + 6*nchannels + 2*c);
  231. x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
  232. x[3] = AV_RN16(pcm + 0*nchannels + 2*c);
  233. x[4] = AV_RN16(pcm + 10*nchannels + 2*c);
  234. x[5] = AV_RN16(pcm + 2*nchannels + 2*c);
  235. x[6] = AV_RN16(pcm + 8*nchannels + 2*c);
  236. x[7] = AV_RN16(pcm + 4*nchannels + 2*c);
  237. }
  238. }
  239. return position;
  240. }
  241. static void sbc_calc_scalefactors(int32_t sb_sample_f[16][2][8],
  242. uint32_t scale_factor[2][8],
  243. int blocks, int channels, int subbands)
  244. {
  245. int ch, sb, blk;
  246. for (ch = 0; ch < channels; ch++) {
  247. for (sb = 0; sb < subbands; sb++) {
  248. uint32_t x = 1 << SCALE_OUT_BITS;
  249. for (blk = 0; blk < blocks; blk++) {
  250. int32_t tmp = FFABS(sb_sample_f[blk][ch][sb]);
  251. if (tmp != 0)
  252. x |= tmp - 1;
  253. }
  254. scale_factor[ch][sb] = (31 - SCALE_OUT_BITS) - ff_clz(x);
  255. }
  256. }
  257. }
  258. static int sbc_calc_scalefactors_j(int32_t sb_sample_f[16][2][8],
  259. uint32_t scale_factor[2][8],
  260. int blocks, int subbands)
  261. {
  262. int blk, joint = 0;
  263. int32_t tmp0, tmp1;
  264. uint32_t x, y;
  265. /* last subband does not use joint stereo */
  266. int sb = subbands - 1;
  267. x = 1 << SCALE_OUT_BITS;
  268. y = 1 << SCALE_OUT_BITS;
  269. for (blk = 0; blk < blocks; blk++) {
  270. tmp0 = FFABS(sb_sample_f[blk][0][sb]);
  271. tmp1 = FFABS(sb_sample_f[blk][1][sb]);
  272. if (tmp0 != 0)
  273. x |= tmp0 - 1;
  274. if (tmp1 != 0)
  275. y |= tmp1 - 1;
  276. }
  277. scale_factor[0][sb] = (31 - SCALE_OUT_BITS) - ff_clz(x);
  278. scale_factor[1][sb] = (31 - SCALE_OUT_BITS) - ff_clz(y);
  279. /* the rest of subbands can use joint stereo */
  280. while (--sb >= 0) {
  281. int32_t sb_sample_j[16][2];
  282. x = 1 << SCALE_OUT_BITS;
  283. y = 1 << SCALE_OUT_BITS;
  284. for (blk = 0; blk < blocks; blk++) {
  285. tmp0 = sb_sample_f[blk][0][sb];
  286. tmp1 = sb_sample_f[blk][1][sb];
  287. sb_sample_j[blk][0] = (tmp0 >> 1) + (tmp1 >> 1);
  288. sb_sample_j[blk][1] = (tmp0 >> 1) - (tmp1 >> 1);
  289. tmp0 = FFABS(tmp0);
  290. tmp1 = FFABS(tmp1);
  291. if (tmp0 != 0)
  292. x |= tmp0 - 1;
  293. if (tmp1 != 0)
  294. y |= tmp1 - 1;
  295. }
  296. scale_factor[0][sb] = (31 - SCALE_OUT_BITS) -
  297. ff_clz(x);
  298. scale_factor[1][sb] = (31 - SCALE_OUT_BITS) -
  299. ff_clz(y);
  300. x = 1 << SCALE_OUT_BITS;
  301. y = 1 << SCALE_OUT_BITS;
  302. for (blk = 0; blk < blocks; blk++) {
  303. tmp0 = FFABS(sb_sample_j[blk][0]);
  304. tmp1 = FFABS(sb_sample_j[blk][1]);
  305. if (tmp0 != 0)
  306. x |= tmp0 - 1;
  307. if (tmp1 != 0)
  308. y |= tmp1 - 1;
  309. }
  310. x = (31 - SCALE_OUT_BITS) - ff_clz(x);
  311. y = (31 - SCALE_OUT_BITS) - ff_clz(y);
  312. /* decide whether to use joint stereo for this subband */
  313. if ((scale_factor[0][sb] + scale_factor[1][sb]) > x + y) {
  314. joint |= 1 << (subbands - 1 - sb);
  315. scale_factor[0][sb] = x;
  316. scale_factor[1][sb] = y;
  317. for (blk = 0; blk < blocks; blk++) {
  318. sb_sample_f[blk][0][sb] = sb_sample_j[blk][0];
  319. sb_sample_f[blk][1][sb] = sb_sample_j[blk][1];
  320. }
  321. }
  322. }
  323. /* bitmask with the information about subbands using joint stereo */
  324. return joint;
  325. }
  326. /*
  327. * Detect CPU features and setup function pointers
  328. */
  329. av_cold void ff_sbcdsp_init(SBCDSPContext *s)
  330. {
  331. /* Default implementation for analyze functions */
  332. s->sbc_analyze_4 = sbc_analyze_4_simd;
  333. s->sbc_analyze_8 = sbc_analyze_8_simd;
  334. s->sbc_analyze_4s = sbc_analyze_4b_4s_simd;
  335. if (s->increment == 1)
  336. s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_odd;
  337. else
  338. s->sbc_analyze_8s = sbc_analyze_4b_8s_simd;
  339. /* Default implementation for input reordering / deinterleaving */
  340. s->sbc_enc_process_input_4s = sbc_enc_process_input_4s;
  341. s->sbc_enc_process_input_8s = sbc_enc_process_input_8s;
  342. /* Default implementation for scale factors calculation */
  343. s->sbc_calc_scalefactors = sbc_calc_scalefactors;
  344. s->sbc_calc_scalefactors_j = sbc_calc_scalefactors_j;
  345. if (ARCH_ARM)
  346. ff_sbcdsp_init_arm(s);
  347. if (ARCH_X86)
  348. ff_sbcdsp_init_x86(s);
  349. }