bitalloc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 The Android Open Source Project
  4. * Copyright 2003 - 2004 Open Interface North America, Inc. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. ******************************************************************************/
  19. /**********************************************************************************
  20. $Revision: #1 $
  21. ***********************************************************************************/
  22. /**
  23. @file
  24. The functions in this file relate to the allocation of available bits to
  25. subbands within the SBC/eSBC frame, along with support functions for computing
  26. frame length and bitrate.
  27. @ingroup codec_internal
  28. */
  29. /**
  30. @addtogroup codec_internal
  31. @{
  32. */
  33. #include "common/bt_target.h"
  34. #include "oi_utils.h"
  35. #include <oi_codec_sbc_private.h>
  36. #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE)
  37. OI_UINT32 OI_SBC_MaxBitpool(OI_CODEC_SBC_FRAME_INFO *frame)
  38. {
  39. switch (frame->mode) {
  40. case SBC_MONO:
  41. case SBC_DUAL_CHANNEL:
  42. return 16 * frame->nrof_subbands;
  43. case SBC_STEREO:
  44. case SBC_JOINT_STEREO:
  45. return 32 * frame->nrof_subbands;
  46. }
  47. ERROR(("Invalid frame mode %d", frame->mode));
  48. OI_ASSERT(FALSE);
  49. return 0; /* Should never be reached */
  50. }
  51. PRIVATE OI_UINT16 internal_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame)
  52. {
  53. OI_UINT16 nbits = frame->nrof_blocks * frame->bitpool;
  54. OI_UINT16 nrof_subbands = frame->nrof_subbands;
  55. OI_UINT16 result = nbits;
  56. if (frame->mode == SBC_JOINT_STEREO) {
  57. result += nrof_subbands + (8 * nrof_subbands);
  58. } else {
  59. if (frame->mode == SBC_DUAL_CHANNEL) {
  60. result += nbits;
  61. }
  62. if (frame->mode == SBC_MONO) {
  63. result += 4 * nrof_subbands;
  64. } else {
  65. result += 8 * nrof_subbands;
  66. }
  67. }
  68. return SBC_HEADER_LEN + (result + 7) / 8;
  69. }
  70. PRIVATE OI_UINT32 internal_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame)
  71. {
  72. OI_UINT blocksbands;
  73. blocksbands = frame->nrof_subbands * frame->nrof_blocks;
  74. return DIVIDE(8 * internal_CalculateFramelen(frame) * frame->frequency, blocksbands);
  75. }
  76. INLINE OI_UINT16 OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO *frame, OI_UINT *headerLen_)
  77. {
  78. OI_UINT headerLen = SBC_HEADER_LEN + frame->nrof_subbands * frame->nrof_channels / 2;
  79. if (frame->mode == SBC_JOINT_STEREO) {
  80. headerLen++;
  81. }
  82. *headerLen_ = headerLen;
  83. return internal_CalculateFramelen(frame);
  84. }
  85. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  86. /*
  87. * Computes the bit need for each sample and as also returns a counts of bit needs that are greater
  88. * than one. This count is used in the first phase of bit allocation.
  89. *
  90. * We also compute a preferred bitpool value that this is the minimum bitpool needed to guarantee
  91. * lossless representation of the audio data. The preferred bitpool may be larger than the bits
  92. * actually required but the only input we have are the scale factors. For example, it takes 2 bits
  93. * to represent values in the range -1 .. +1 but the scale factor is 0. To guarantee lossless
  94. * representation we add 2 to each scale factor and sum them to come up with the preferred bitpool.
  95. * This is not ideal because 0 requires 0 bits but we currently have no way of knowing this.
  96. *
  97. * @param bitneed Array to return bitneeds for each subband
  98. *
  99. * @param ch Channel 0 or 1
  100. *
  101. * @param preferredBitpool Returns the number of reserved bits
  102. *
  103. * @return The SBC bit need
  104. *
  105. */
  106. OI_UINT computeBitneed(OI_CODEC_SBC_COMMON_CONTEXT *common,
  107. OI_UINT8 *bitneeds,
  108. OI_UINT ch,
  109. OI_UINT *preferredBitpool)
  110. {
  111. static const OI_INT8 offset4[4][4] = {
  112. { -1, 0, 0, 0 },
  113. { -2, 0, 0, 1 },
  114. { -2, 0, 0, 1 },
  115. { -2, 0, 0, 1 }
  116. };
  117. static const OI_INT8 offset8[4][8] = {
  118. { -2, 0, 0, 0, 0, 0, 0, 1 },
  119. { -3, 0, 0, 0, 0, 0, 1, 2 },
  120. { -4, 0, 0, 0, 0, 0, 1, 2 },
  121. { -4, 0, 0, 0, 0, 0, 1, 2 }
  122. };
  123. const OI_UINT nrof_subbands = common->frameInfo.nrof_subbands;
  124. OI_UINT sb;
  125. OI_INT8 *scale_factor = &common->scale_factor[ch ? nrof_subbands : 0];
  126. OI_UINT bitcount = 0;
  127. OI_UINT8 maxBits = 0;
  128. OI_UINT8 prefBits = 0;
  129. if (common->frameInfo.alloc == SBC_SNR) {
  130. for (sb = 0; sb < nrof_subbands; sb++) {
  131. OI_INT bits = scale_factor[sb];
  132. if (bits > maxBits) {
  133. maxBits = bits;
  134. }
  135. if ((bitneeds[sb] = bits) > 1) {
  136. bitcount += bits;
  137. }
  138. prefBits += 2 + bits;
  139. }
  140. } else {
  141. const OI_INT8 *offset;
  142. if (nrof_subbands == 4) {
  143. offset = offset4[common->frameInfo.freqIndex];
  144. } else {
  145. offset = offset8[common->frameInfo.freqIndex];
  146. }
  147. for (sb = 0; sb < nrof_subbands; sb++) {
  148. OI_INT bits = scale_factor[sb];
  149. if (bits > maxBits) {
  150. maxBits = bits;
  151. }
  152. prefBits += 2 + bits;
  153. if (bits) {
  154. bits -= offset[sb];
  155. if (bits > 0) {
  156. bits /= 2;
  157. }
  158. bits += 5;
  159. }
  160. if ((bitneeds[sb] = bits) > 1) {
  161. bitcount += bits;
  162. }
  163. }
  164. }
  165. common->maxBitneed = OI_MAX(maxBits, common->maxBitneed);
  166. *preferredBitpool += prefBits;
  167. return bitcount;
  168. }
  169. /*
  170. * Explanation of the adjustToFitBitpool inner loop.
  171. *
  172. * The inner loop computes the effect of adjusting the bit allocation up or
  173. * down. Allocations must be 0 or in the range 2..16. This is accomplished by
  174. * the following code:
  175. *
  176. * for (s = bands - 1; s >= 0; --s) {
  177. * OI_INT bits = bitadjust + bitneeds[s];
  178. * bits = bits < 2 ? 0 : bits;
  179. * bits = bits > 16 ? 16 : bits;
  180. * count += bits;
  181. * }
  182. *
  183. * This loop can be optimized to perform 4 operations at a time as follows:
  184. *
  185. * Adjustment is computed as a 7 bit signed value and added to the bitneed.
  186. *
  187. * Negative allocations are zeroed by masking. (n & 0x40) >> 6 puts the
  188. * sign bit into bit 0, adding this to 0x7F give us a mask of 0x80
  189. * for -ve values and 0x7F for +ve values.
  190. *
  191. * n &= 0x7F + (n & 0x40) >> 6)
  192. *
  193. * Allocations greater than 16 are truncated to 16. Adjusted allocations are in
  194. * the range 0..31 so we know that bit 4 indicates values >= 16. We use this bit
  195. * to create a mask that zeroes bits 0 .. 3 if bit 4 is set.
  196. *
  197. * n &= (15 + (n >> 4))
  198. *
  199. * Allocations of 1 are disallowed. Add and shift creates a mask that
  200. * eliminates the illegal value
  201. *
  202. * n &= ((n + 14) >> 4) | 0x1E
  203. *
  204. * These operations can be performed in 8 bits without overflowing so we can
  205. * operate on 4 values at once.
  206. */
  207. /*
  208. * Encoder/Decoder
  209. *
  210. * Computes adjustment +/- of bitneeds to fill bitpool and returns overall
  211. * adjustment and excess bits.
  212. *
  213. * @param bitpool The bitpool we have to work within
  214. *
  215. * @param bitneeds An array of bit needs (more acturately allocation prioritities) for each
  216. * subband across all blocks in the SBC frame
  217. *
  218. * @param subbands The number of subbands over which the adkustment is calculated. For mono and
  219. * dual mode this is 4 or 8, for stereo or joint stereo this is 8 or 16.
  220. *
  221. * @param bitcount A starting point for the adjustment
  222. *
  223. * @param excess Returns the excess bits after the adjustment
  224. *
  225. * @return The adjustment.
  226. */
  227. OI_INT adjustToFitBitpool(const OI_UINT bitpool,
  228. OI_UINT32 *bitneeds,
  229. const OI_UINT subbands,
  230. OI_UINT bitcount,
  231. OI_UINT *excess)
  232. {
  233. OI_INT maxBitadjust = 0;
  234. OI_INT bitadjust = (bitcount > bitpool) ? -8 : 8;
  235. OI_INT chop = 8;
  236. /*
  237. * This is essentially a binary search for the optimal adjustment value.
  238. */
  239. while ((bitcount != bitpool) && chop) {
  240. OI_UINT32 total = 0;
  241. OI_UINT count;
  242. OI_UINT32 adjust4;
  243. OI_INT i;
  244. adjust4 = bitadjust & 0x7F;
  245. adjust4 |= (adjust4 << 8);
  246. adjust4 |= (adjust4 << 16);
  247. for (i = (subbands / 4 - 1); i >= 0; --i) {
  248. OI_UINT32 mask;
  249. OI_UINT32 n = bitneeds[i] + adjust4;
  250. mask = 0x7F7F7F7F + ((n & 0x40404040) >> 6);
  251. n &= mask;
  252. mask = 0x0F0F0F0F + ((n & 0x10101010) >> 4);
  253. n &= mask;
  254. mask = (((n + 0x0E0E0E0E) >> 4) | 0x1E1E1E1E);
  255. n &= mask;
  256. total += n;
  257. }
  258. count = (total & 0xFFFF) + (total >> 16);
  259. count = (count & 0xFF) + (count >> 8);
  260. chop >>= 1;
  261. if (count > bitpool) {
  262. bitadjust -= chop;
  263. } else {
  264. maxBitadjust = bitadjust;
  265. bitcount = count;
  266. bitadjust += chop;
  267. }
  268. }
  269. *excess = bitpool - bitcount;
  270. return maxBitadjust;
  271. }
  272. /*
  273. * The bit allocator trys to avoid single bit allocations except as a last resort. So in the case
  274. * where a bitneed of 1 was passed over during the adsjustment phase 2 bits are now allocated.
  275. */
  276. INLINE OI_INT allocAdjustedBits(OI_UINT8 *dest,
  277. OI_INT bits,
  278. OI_INT excess)
  279. {
  280. if (bits < 16) {
  281. if (bits > 1) {
  282. if (excess) {
  283. ++bits;
  284. --excess;
  285. }
  286. } else if ((bits == 1) && (excess > 1)) {
  287. bits = 2;
  288. excess -= 2;
  289. } else {
  290. bits = 0;
  291. }
  292. } else {
  293. bits = 16;
  294. }
  295. *dest = (OI_UINT8)bits;
  296. return excess;
  297. }
  298. /*
  299. * Excess bits not allocated by allocaAdjustedBits are allocated round-robin.
  300. */
  301. INLINE OI_INT allocExcessBits(OI_UINT8 *dest,
  302. OI_INT excess)
  303. {
  304. if (*dest < 16) {
  305. *dest += 1;
  306. return excess - 1;
  307. } else {
  308. return excess;
  309. }
  310. }
  311. void oneChannelBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common,
  312. BITNEED_UNION1 *bitneeds,
  313. OI_UINT ch,
  314. OI_UINT bitcount)
  315. {
  316. const OI_UINT8 nrof_subbands = common->frameInfo.nrof_subbands;
  317. OI_UINT excess;
  318. OI_UINT sb;
  319. OI_INT bitadjust;
  320. OI_UINT8 RESTRICT *allocBits;
  321. {
  322. OI_UINT ex;
  323. bitadjust = adjustToFitBitpool(common->frameInfo.bitpool, bitneeds->uint32, nrof_subbands, bitcount, &ex);
  324. /* We want the compiler to put excess into a register */
  325. excess = ex;
  326. }
  327. /*
  328. * Allocate adjusted bits
  329. */
  330. allocBits = &common->bits.uint8[ch ? nrof_subbands : 0];
  331. sb = 0;
  332. while (sb < nrof_subbands) {
  333. excess = allocAdjustedBits(&allocBits[sb], bitneeds->uint8[sb] + bitadjust, excess);
  334. ++sb;
  335. }
  336. sb = 0;
  337. while (excess) {
  338. excess = allocExcessBits(&allocBits[sb], excess);
  339. ++sb;
  340. }
  341. }
  342. void monoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common)
  343. {
  344. BITNEED_UNION1 bitneeds;
  345. OI_UINT bitcount;
  346. OI_UINT bitpoolPreference = 0;
  347. bitcount = computeBitneed(common, bitneeds.uint8, 0, &bitpoolPreference);
  348. oneChannelBitAllocation(common, &bitneeds, 0, bitcount);
  349. }
  350. /**
  351. @}
  352. */
  353. #endif /* #if (defined(SBC_DEC_INCLUDED) && SBC_DEC_INCLUDED == TRUE) */