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