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 /** @file
26 @ingroup codec_internal
27 */
28
29 /**@addgroup codec_internal*/
30 /**@{*/
31
32 #include <oi_codec_sbc_private.h>
33
dualBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT * common)34 static void dualBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT* common) {
35 OI_UINT bitcountL;
36 OI_UINT bitcountR;
37 OI_UINT bitpoolPreferenceL = 0;
38 OI_UINT bitpoolPreferenceR = 0;
39 BITNEED_UNION1 bitneedsL;
40 BITNEED_UNION1 bitneedsR;
41
42 bitcountL = computeBitneed(common, bitneedsL.uint8, 0, &bitpoolPreferenceL);
43 bitcountR = computeBitneed(common, bitneedsR.uint8, 1, &bitpoolPreferenceR);
44
45 oneChannelBitAllocation(common, &bitneedsL, 0, bitcountL);
46 oneChannelBitAllocation(common, &bitneedsR, 1, bitcountR);
47 }
48
stereoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT * common)49 static void stereoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT* common) {
50 const OI_UINT nrof_subbands = common->frameInfo.nrof_subbands;
51 BITNEED_UNION2 bitneeds;
52 OI_UINT excess;
53 OI_INT bitadjust;
54 OI_UINT bitcount;
55 OI_UINT sbL;
56 OI_UINT sbR;
57 OI_UINT bitpoolPreference = 0;
58
59 bitcount = computeBitneed(common, &bitneeds.uint8[0], 0, &bitpoolPreference);
60 bitcount += computeBitneed(common, &bitneeds.uint8[nrof_subbands], 1, &bitpoolPreference);
61
62 {
63 OI_UINT ex;
64 bitadjust = adjustToFitBitpool(common->frameInfo.bitpool, bitneeds.uint32, 2 * nrof_subbands,
65 bitcount, &ex);
66 /* We want the compiler to put excess into a register */
67 excess = ex;
68 }
69 sbL = 0;
70 sbR = nrof_subbands;
71 while (sbL < nrof_subbands) {
72 excess = allocAdjustedBits(&common->bits.uint8[sbL], bitneeds.uint8[sbL] + bitadjust, excess);
73 ++sbL;
74 excess = allocAdjustedBits(&common->bits.uint8[sbR], bitneeds.uint8[sbR] + bitadjust, excess);
75 ++sbR;
76 }
77 sbL = 0;
78 sbR = nrof_subbands;
79 while (excess) {
80 excess = allocExcessBits(&common->bits.uint8[sbL], excess);
81 ++sbL;
82 if (!excess) {
83 break;
84 }
85 excess = allocExcessBits(&common->bits.uint8[sbR], excess);
86 ++sbR;
87 }
88 }
89
90 static const BIT_ALLOC balloc[] = {
91 monoBitAllocation, /* SBC_MONO */
92 dualBitAllocation, /* SBC_DUAL_CHANNEL */
93 stereoBitAllocation, /* SBC_STEREO */
94 stereoBitAllocation /* SBC_JOINT_STEREO */
95 };
96
OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT * common)97 PRIVATE void OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT* common) {
98 OI_ASSERT(common->frameInfo.bitpool <= OI_SBC_MaxBitpool(&common->frameInfo));
99 OI_ASSERT(common->frameInfo.mode < OI_ARRAYSIZE(balloc));
100
101 /*
102 * Using an array of function pointers prevents the compiler from creating a
103 * suboptimal
104 * monolithic inlined bit allocation function.
105 */
106 balloc[common->frameInfo.mode](common);
107 }
108
OI_CODEC_SBC_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO * frame)109 uint32_t OI_CODEC_SBC_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO* frame) {
110 return internal_CalculateBitrate(frame);
111 }
112
113 /*
114 * Return the current maximum bitneed and clear it.
115 */
OI_CODEC_SBC_GetMaxBitneed(OI_CODEC_SBC_COMMON_CONTEXT * common)116 uint8_t OI_CODEC_SBC_GetMaxBitneed(OI_CODEC_SBC_COMMON_CONTEXT* common) {
117 uint8_t max = common->maxBitneed;
118
119 common->maxBitneed = 0;
120 return max;
121 }
122
123 /*
124 * Calculates the bitpool size for a given frame length
125 */
OI_CODEC_SBC_CalculateBitpool(OI_CODEC_SBC_FRAME_INFO * frame,uint16_t frameLen)126 uint16_t OI_CODEC_SBC_CalculateBitpool(OI_CODEC_SBC_FRAME_INFO* frame, uint16_t frameLen) {
127 uint16_t nrof_subbands = frame->nrof_subbands;
128 uint16_t nrof_blocks = frame->nrof_blocks;
129 uint16_t hdr;
130 uint16_t bits;
131
132 if (frame->mode == SBC_JOINT_STEREO) {
133 hdr = 9 * nrof_subbands;
134 } else {
135 if (frame->mode == SBC_MONO) {
136 hdr = 4 * nrof_subbands;
137 } else {
138 hdr = 8 * nrof_subbands;
139 }
140 if (frame->mode == SBC_DUAL_CHANNEL) {
141 nrof_blocks *= 2;
142 }
143 }
144 bits = 8 * (frameLen - SBC_HEADER_LEN) - hdr;
145 return DIVIDE(bits, nrof_blocks);
146 }
147
OI_CODEC_SBC_CalculatePcmBytes(OI_CODEC_SBC_COMMON_CONTEXT * common)148 uint16_t OI_CODEC_SBC_CalculatePcmBytes(OI_CODEC_SBC_COMMON_CONTEXT* common) {
149 return sizeof(int16_t) * common->pcmStride * common->frameInfo.nrof_subbands *
150 common->frameInfo.nrof_blocks;
151 }
152
OI_CODEC_SBC_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO * frame)153 uint16_t OI_CODEC_SBC_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO* frame) {
154 return internal_CalculateFramelen(frame);
155 }
156
157 /**@}*/
158