bitalloc.c 12 KB

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