1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "include/sbc_encoder.h"
16 #include <memory>
17 #include "include/sbc_constant.h"
18 #include "include/sbc_math.h"
19 #include "include/sbc_tables.h"
20 #include "include/sbc_frame.h"
21 #include "new"
22 #include "securec.h"
23 #include "sys/types.h"
24
25 namespace sbc {
26 const int SUBBAND_4 = 4;
27 const int SUBBAND_8 = 8;
28
29 const int CHANNEL_1 = 1;
30 const int CHANNEL_2 = 2;
31
32 const int NOT_JOINT_STEREO = 0;
33 const int JOINT_STEREO = 1;
34
35 const int INCREMENT_VALUE = 4;
36 const int SCALE_OUT_BITS = 15;
37
38 const int VALUE_0 = 0;
39 const int VALUE_1 = 1;
40 const int VALUE_2 = 2;
41 const int VALUE_3 = 3;
42 const int VALUE_4 = 4;
43 const int VALUE_5 = 5;
44 const int VALUE_6 = 6;
45 const uint VALUE_7 = 7;
46 const int VALUE_8 = 8;
47 const int VALUE_9 = 9;
48 const int VALUE_10 = 10;
49 const int VALUE_11 = 11;
50 const int VALUE_12 = 12;
51 const int VALUE_13 = 13;
52 const int VALUE_14 = 14;
53 const int VALUE_15 = 15;
54 const int VALUE_16 = 16;
55 const int VALUE_24 = 24;
56 const int VALUE_31 = 31;
57 const int VALUE_32 = 32;
58 const int VALUE_36 = 36;
59 const int VALUE_40 = 40;
60 const int VALUE_72 = 72;
61 const int CHANNEL_NUM = 2;
62 const int BLOCK_NUM = 16;
63 const int SUBBAND_NUM = 8;
64
65
66 const int COEFFICIENT_40 = 40;
67 const int COEFFICIENT_80 = 80;
68
Encoder()69 Encoder::Encoder()
70 {
71 initialized_ = false;
72 }
73
~Encoder()74 Encoder::~Encoder()
75 {}
76
CreateEncode()77 extern "C" Encoder* CreateEncode()
78 {
79 std::unique_ptr<Encoder> encoder = std::make_unique<Encoder>();
80 return encoder.release();
81 }
82
DestroyEncode(Encoder * p)83 extern "C" void DestroyEncode(Encoder *p)
84 {
85 delete p;
86 }
87
Init(const Frame & frame)88 void Encoder::Init(const Frame &frame)
89 {
90 (void)memset_s(x_, sizeof(x_), VALUE_0, sizeof(x_));
91 increment_ = INCREMENT_VALUE;
92 position_ = (BUFFER_SIZE - frame.subbands_ * VALUE_9) & ~VALUE_7;
93 }
94
CalculateFrameLength(const CodecParam & codecParam)95 size_t Encoder::CalculateFrameLength(const CodecParam &codecParam)
96 {
97 uint8_t subbands = codecParam.subbands ? SUBBAND_8 : SUBBAND_4;
98 uint8_t blocks = (codecParam.blocks * VALUE_4) + VALUE_4;
99 uint8_t joint = (codecParam.channelMode == SBC_CHANNEL_MODE_STEREO) ? JOINT_STEREO : NOT_JOINT_STEREO;
100 uint8_t channels = (codecParam.channelMode == SBC_CHANNEL_MODE_MONO) ? CHANNEL_1 : CHANNEL_2;
101 uint8_t bitpool = codecParam.bitpool;
102 size_t size = VALUE_4 + (VALUE_4 * subbands * channels) / VALUE_8;
103
104 if (channels == CHANNEL_1 || codecParam.channelMode == SBC_CHANNEL_MODE_DUAL_CHANNEL) {
105 size += ((blocks * channels * bitpool) + VALUE_7) / VALUE_8;
106 } else {
107 size += (((joint ? subbands : VALUE_0) + blocks * bitpool) + VALUE_7) / VALUE_8;
108 }
109 return size;
110 }
111
CalculateCodecSize(const CodecParam & codecParam)112 size_t Encoder::CalculateCodecSize(const CodecParam &codecParam)
113 {
114 uint16_t subbands = codecParam.subbands ? SUBBAND_8 : SUBBAND_4;
115 uint16_t channels = (codecParam.channelMode == SBC_CHANNEL_MODE_MONO) ? CHANNEL_1 : CHANNEL_2;
116 uint16_t blocks = (codecParam.blocks * VALUE_4) + VALUE_4;
117
118 return subbands * blocks * channels * VALUE_2;
119 }
120
UpdateCodecFormat(const CodecParam & codecParam)121 void Encoder::UpdateCodecFormat(const CodecParam &codecParam)
122 {
123 if (!initialized_) {
124 frame_.frequency_ = codecParam.frequency;
125 frame_.channelMode_ = codecParam.channelMode;
126 frame_.channels_ = ((codecParam.channelMode == SBC_CHANNEL_MODE_MONO) ? CHANNEL_1 : CHANNEL_2);
127 frame_.allocation_ = codecParam.allocation;
128 frame_.subbandMode_ = codecParam.subbands;
129 frame_.subbands_ = codecParam.subbands ? SUBBAND_8 : SUBBAND_4;
130 frame_.blockMode_ = codecParam.blocks;
131 frame_.blocks_ = (codecParam.blocks * VALUE_4) + VALUE_4;
132 frame_.bitpool_ = codecParam.bitpool;
133 frame_.codeSize_ = CalculateCodecSize(codecParam);
134 frame_.length_ = CalculateFrameLength(codecParam);
135 Init(frame_);
136 initialized_ = true;
137 } else {
138 frame_.length_ = CalculateFrameLength(codecParam);
139 frame_.bitpool_ = codecParam.bitpool;
140 }
141 }
142
SBCEncode(const CodecParam & codecParam,const uint8_t * in,size_t iLength,uint8_t * out,size_t oLength,size_t * written)143 ssize_t Encoder::SBCEncode(const CodecParam &codecParam, const uint8_t* in, size_t iLength,
144 uint8_t* out, size_t oLength, size_t* written)
145 {
146 int samples = VALUE_0;
147 ssize_t frameLen = VALUE_0;
148
149 if (written) {
150 *written = VALUE_0;
151 }
152
153 UpdateCodecFormat(codecParam);
154 int pcmSamples = frame_.subbands_ * frame_.blocks_;
155
156 if (!out || oLength < frame_.length_) {
157 return SBC_ERROR_INVALID_FRAME;
158 }
159
160 if (frame_.subbands_ == SUBBAND_8) {
161 position_ = Get8SubbandSamplingPoint(in, x_,
162 &pcmSamples,
163 frame_.channels_,
164 codecParam.endian);
165 samples = Analyze8Subbands(position_, x_, frame_, increment_);
166 } else {
167 position_ = Get4SubbandSamplingPoint(in, x_,
168 pcmSamples,
169 frame_.channels_,
170 codecParam.endian);
171 samples = Analyze4Subbands(position_, x_, frame_, increment_);
172 }
173
174 if (frame_.channelMode_ == SBC_CHANNEL_MODE_JOINT_STEREO) {
175 int j = CalculateScalefactorsJoint();
176 frameLen = frame_.Pack(out, frame_, j);
177 } else {
178 CalculateScalefactors(frame_.audioSamples_, frame_.scaleFactor_, frame_.blocks_,
179 frame_.channels_, frame_.subbands_);
180 frameLen = frame_.Pack(out, frame_, VALUE_0);
181 }
182
183 if (written) {
184 *written = frameLen;
185 }
186
187 return samples * frame_.channels_ * VALUE_2;
188 }
189
AnalyzeFourForPolyphaseFilter(int32_t * temp,const int16_t * inData,const int16_t * consts)190 void Encoder::AnalyzeFourForPolyphaseFilter(int32_t *temp, const int16_t *inData, const int16_t *consts)
191 {
192 for (int hop = VALUE_0; hop < COEFFICIENT_40; hop += VALUE_8) {
193 temp[VALUE_0] += static_cast<int32_t>(inData[hop] * consts[hop]);
194 temp[VALUE_0] += static_cast<int32_t>(inData[hop + VALUE_1] * consts[hop + VALUE_1]);
195 temp[VALUE_1] += static_cast<int32_t>(inData[hop + VALUE_2] * consts[hop + VALUE_2]);
196 temp[VALUE_1] += static_cast<int32_t>(inData[hop + VALUE_3] * consts[hop + VALUE_3]);
197 temp[VALUE_2] += static_cast<int32_t>(inData[hop + VALUE_4] * consts[hop + VALUE_4]);
198 temp[VALUE_2] += static_cast<int32_t>(inData[hop + VALUE_5] * consts[hop + VALUE_5]);
199 temp[VALUE_3] += static_cast<int32_t>(inData[hop + VALUE_6] * consts[hop + VALUE_6]);
200 temp[VALUE_3] += static_cast<int32_t>(inData[hop + VALUE_7] * consts[hop + VALUE_7]);
201 }
202 }
203
AnalyzeFourForScaling(int32_t * temp1,int16_t * temp2)204 void Encoder::AnalyzeFourForScaling(int32_t *temp1, int16_t *temp2)
205 {
206 temp2[VALUE_0] = temp1[VALUE_0] >> PROTO_BAND4_SCALE;
207 temp2[VALUE_1] = temp1[VALUE_1] >> PROTO_BAND4_SCALE;
208 temp2[VALUE_2] = temp1[VALUE_2] >> PROTO_BAND4_SCALE;
209 temp2[VALUE_3] = temp1[VALUE_3] >> PROTO_BAND4_SCALE;
210 }
211
AnalyzeFourForCosTransform(int32_t * temp1,int16_t * temp2,const int16_t * consts)212 void Encoder::AnalyzeFourForCosTransform(int32_t *temp1, int16_t *temp2, const int16_t *consts)
213 {
214 temp1[VALUE_0] = static_cast<int32_t>(temp2[VALUE_0] * consts[COEFFICIENT_40 + VALUE_0]);
215 temp1[VALUE_0] += static_cast<int32_t>(temp2[VALUE_1] * consts[COEFFICIENT_40 + VALUE_1]);
216 temp1[VALUE_1] = static_cast<int32_t>(temp2[VALUE_0] * consts[COEFFICIENT_40 + VALUE_2]);
217 temp1[VALUE_1] += static_cast<int32_t>(temp2[VALUE_1] * consts[COEFFICIENT_40 + VALUE_3]);
218 temp1[VALUE_2] = static_cast<int32_t>(temp2[VALUE_0] * consts[COEFFICIENT_40 + VALUE_4]);
219 temp1[VALUE_2] += static_cast<int32_t>(temp2[VALUE_1] * consts[COEFFICIENT_40 + VALUE_5]);
220 temp1[VALUE_3] = static_cast<int32_t>(temp2[VALUE_0] * consts[COEFFICIENT_40 + VALUE_6]);
221 temp1[VALUE_3] += static_cast<int32_t>(temp2[VALUE_1] * consts[COEFFICIENT_40 + VALUE_7]);
222
223 temp1[VALUE_0] += static_cast<int32_t>(temp2[VALUE_2] * consts[COEFFICIENT_40 + VALUE_8]);
224 temp1[VALUE_0] += static_cast<int32_t>(temp2[VALUE_3] * consts[COEFFICIENT_40 + VALUE_9]);
225 temp1[VALUE_1] += static_cast<int32_t>(temp2[VALUE_2] * consts[COEFFICIENT_40 + VALUE_10]);
226 temp1[VALUE_1] += static_cast<int32_t>(temp2[VALUE_3] * consts[COEFFICIENT_40 + VALUE_11]);
227 temp1[VALUE_2] += static_cast<int32_t>(temp2[VALUE_2] * consts[COEFFICIENT_40 + VALUE_12]);
228 temp1[VALUE_2] += static_cast<int32_t>(temp2[VALUE_3] * consts[COEFFICIENT_40 + VALUE_13]);
229 temp1[VALUE_3] += static_cast<int32_t>(temp2[VALUE_2] * consts[COEFFICIENT_40 + VALUE_14]);
230 temp1[VALUE_3] += static_cast<int32_t>(temp2[VALUE_3] * consts[COEFFICIENT_40 + VALUE_15]);
231 }
232
233
AnalyzeFourFunction(const int16_t * inData,int32_t * outData,const int16_t * consts) const234 void Encoder::AnalyzeFourFunction(const int16_t *inData, int32_t *outData, const int16_t *consts) const
235 {
236 int32_t t1[VALUE_4] = {};
237 int16_t t2[VALUE_4] = {};
238
239 t1[VALUE_0] = t1[VALUE_1] = t1[VALUE_2] = t1[VALUE_3] =
240 static_cast<int32_t>(VALUE_1 << (PROTO_BAND4_SCALE - VALUE_1));
241
242 AnalyzeFourForPolyphaseFilter(t1, inData, consts);
243 AnalyzeFourForScaling(t1, t2);
244 AnalyzeFourForCosTransform(t1, t2, consts);
245
246 outData[VALUE_0] = t1[VALUE_0] >> (COS_TABLE_BAND4_SCALE - SCALE_OUT_BITS);
247 outData[VALUE_1] = t1[VALUE_1] >> (COS_TABLE_BAND4_SCALE - SCALE_OUT_BITS);
248 outData[VALUE_2] = t1[VALUE_2] >> (COS_TABLE_BAND4_SCALE - SCALE_OUT_BITS);
249 outData[VALUE_3] = t1[VALUE_3] >> (COS_TABLE_BAND4_SCALE - SCALE_OUT_BITS);
250 }
251
AnalyzeEightForPolyphaseFilter(int32_t * temp,const int16_t * inData,const int16_t * consts)252 void Encoder::AnalyzeEightForPolyphaseFilter(int32_t *temp, const int16_t *inData, const int16_t *consts)
253 {
254 for (int hop = VALUE_0; hop < COEFFICIENT_80; hop += VALUE_16) {
255 temp[VALUE_0] += static_cast<int32_t>(inData[hop] * consts[hop]);
256 temp[VALUE_0] += static_cast<int32_t>(inData[hop + VALUE_1] * consts[hop + VALUE_1]);
257 temp[VALUE_1] += static_cast<int32_t>(inData[hop + VALUE_2] * consts[hop + VALUE_2]);
258 temp[VALUE_1] += static_cast<int32_t>(inData[hop + VALUE_3] * consts[hop + VALUE_3]);
259 temp[VALUE_2] += static_cast<int32_t>(inData[hop + VALUE_4] * consts[hop + VALUE_4]);
260 temp[VALUE_2] += static_cast<int32_t>(inData[hop + VALUE_5] * consts[hop + VALUE_5]);
261 temp[VALUE_3] += static_cast<int32_t>(inData[hop + VALUE_6] * consts[hop + VALUE_6]);
262 temp[VALUE_3] += static_cast<int32_t>(inData[hop + VALUE_7] * consts[hop + VALUE_7]);
263 temp[VALUE_4] += static_cast<int32_t>(inData[hop + VALUE_8] * consts[hop + VALUE_8]);
264 temp[VALUE_4] += static_cast<int32_t>(inData[hop + VALUE_9] * consts[hop + VALUE_9]);
265 temp[VALUE_5] += static_cast<int32_t>(inData[hop + VALUE_10] * consts[hop + VALUE_10]);
266 temp[VALUE_5] += static_cast<int32_t>(inData[hop + VALUE_11] * consts[hop + VALUE_11]);
267 temp[VALUE_6] += static_cast<int32_t>(inData[hop + VALUE_12] * consts[hop + VALUE_12]);
268 temp[VALUE_6] += static_cast<int32_t>(inData[hop + VALUE_13] * consts[hop + VALUE_13]);
269 temp[VALUE_7] += static_cast<int32_t>(inData[hop + VALUE_14] * consts[hop + VALUE_14]);
270 temp[VALUE_7] += static_cast<int32_t>(inData[hop + VALUE_15] * consts[hop + VALUE_15]);
271 }
272 }
273
AnalyzeEightForScaling(int32_t * temp1,int16_t * temp2)274 void Encoder::AnalyzeEightForScaling(int32_t *temp1, int16_t *temp2)
275 {
276 temp2[VALUE_0] = temp1[VALUE_0] >> PROTO_BAND8_SCALE;
277 temp2[VALUE_1] = temp1[VALUE_1] >> PROTO_BAND8_SCALE;
278 temp2[VALUE_2] = temp1[VALUE_2] >> PROTO_BAND8_SCALE;
279 temp2[VALUE_3] = temp1[VALUE_3] >> PROTO_BAND8_SCALE;
280 temp2[VALUE_4] = temp1[VALUE_4] >> PROTO_BAND8_SCALE;
281 temp2[VALUE_5] = temp1[VALUE_5] >> PROTO_BAND8_SCALE;
282 temp2[VALUE_6] = temp1[VALUE_6] >> PROTO_BAND8_SCALE;
283 temp2[VALUE_7] = temp1[VALUE_7] >> PROTO_BAND8_SCALE;
284 }
285
AnalyzeEightForCosTransform(int32_t * temp1,int16_t * temp2,const int16_t * consts)286 void Encoder::AnalyzeEightForCosTransform(int32_t *temp1, int16_t *temp2, const int16_t *consts)
287 {
288 temp1[VALUE_0] = temp1[VALUE_1] = temp1[VALUE_2] = temp1[VALUE_3] = temp1[VALUE_4] =
289 temp1[VALUE_5] = temp1[VALUE_6] = temp1[VALUE_7] = VALUE_0;
290 for (int i = VALUE_0; i < VALUE_4; i++) {
291 temp1[VALUE_0] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
292 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_0]);
293 temp1[VALUE_0] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
294 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_1]);
295 temp1[VALUE_1] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
296 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_2]);
297 temp1[VALUE_1] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
298 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_3]);
299 temp1[VALUE_2] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
300 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_4]);
301 temp1[VALUE_2] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
302 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_5]);
303 temp1[VALUE_3] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
304 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_6]);
305 temp1[VALUE_3] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
306 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_7]);
307 temp1[VALUE_4] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
308 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_8]);
309 temp1[VALUE_4] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
310 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_9]);
311 temp1[VALUE_5] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
312 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_10]);
313 temp1[VALUE_5] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
314 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_11]);
315 temp1[VALUE_6] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
316 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_12]);
317 temp1[VALUE_6] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
318 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_13]);
319 temp1[VALUE_7] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_0] *
320 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_14]);
321 temp1[VALUE_7] += static_cast<int32_t>(temp2[i * VALUE_2 + VALUE_1] *
322 consts[COEFFICIENT_80 + i * VALUE_16 + VALUE_15]);
323 }
324 }
325
AnalyzeEightFunction(const int16_t * inData,int32_t * outData,const int16_t * consts) const326 void Encoder::AnalyzeEightFunction(const int16_t *inData, int32_t *outData, const int16_t *consts) const
327 {
328 int32_t t1[VALUE_8] = {};
329 int16_t t2[VALUE_8] = {};
330
331 t1[VALUE_0] = t1[VALUE_1] = t1[VALUE_2] = t1[VALUE_3] = t1[VALUE_4] =
332 t1[VALUE_5] = t1[VALUE_6] = t1[VALUE_7] = static_cast<int32_t>(VALUE_1 << (PROTO_BAND8_SCALE - VALUE_1));
333 AnalyzeEightForPolyphaseFilter(t1, inData, consts);
334 AnalyzeEightForScaling(t1, t2);
335 AnalyzeEightForCosTransform(t1, t2, consts);
336 for (int i = VALUE_0; i < VALUE_8; i++) {
337 outData[i] = t1[i] >> (COS_TABLE_BAND8_SCALE - SCALE_OUT_BITS);
338 }
339 }
340
Analyze4SubbandsInternal(int16_t * x,int32_t * outData,int increseValue)341 void Encoder::Analyze4SubbandsInternal(int16_t *x, int32_t *outData, int increseValue)
342 {
343 /* Analyze blocks */
344 AnalyzeFourFunction(x + VALUE_12, outData, ANALYSIS_CONSTS_BAND4_ODD_MODE);
345 outData += increseValue;
346 AnalyzeFourFunction(x + VALUE_8, outData, ANALYSIS_CONSTS_BAND4_EVEN_MODE);
347 outData += increseValue;
348 AnalyzeFourFunction(x + VALUE_4, outData, ANALYSIS_CONSTS_BAND4_ODD_MODE);
349 outData += increseValue;
350 AnalyzeFourFunction(x + VALUE_0, outData, ANALYSIS_CONSTS_BAND4_EVEN_MODE);
351 }
352
Analyze8SubbandsInternal(int16_t * x,int32_t * outData,int increseValue)353 void Encoder::Analyze8SubbandsInternal(int16_t *x, int32_t *outData, int increseValue)
354 {
355 /* Analyze blocks */
356 AnalyzeEightFunction(x + VALUE_24, outData, ANALYSIS_CONSTS_BAND8_ODD_MODE);
357 outData += increseValue;
358 AnalyzeEightFunction(x + VALUE_16, outData, ANALYSIS_CONSTS_BAND8_EVEN_MODE);
359 outData += increseValue;
360 AnalyzeEightFunction(x + VALUE_8, outData, ANALYSIS_CONSTS_BAND8_ODD_MODE);
361 outData += increseValue;
362 AnalyzeEightFunction(x + VALUE_0, outData, ANALYSIS_CONSTS_BAND8_EVEN_MODE);
363 }
364
Analyze4Subbands(int position,int16_t x[CHANNEL_NUM][BUFFER_SIZE],Frame & frame,int increment)365 int Encoder::Analyze4Subbands(int position, int16_t x[CHANNEL_NUM][BUFFER_SIZE],
366 Frame& frame, int increment)
367 {
368 int16_t *fourBandBuffer = nullptr;
369 for (int ch = VALUE_0; ch < frame.channels_; ch++) {
370 fourBandBuffer = &x[ch][position - SUBBAND_4 * increment + frame.blocks_ * SUBBAND_4];
371 for (int blk = VALUE_0; blk < frame.blocks_; blk += increment) {
372 Analyze4SubbandsInternal(fourBandBuffer, frame.audioSamples_[blk][ch],
373 frame.audioSamples_[blk + VALUE_1][ch] - frame.audioSamples_[blk][ch]);
374 fourBandBuffer -= SUBBAND_4 * increment;
375 }
376 }
377 return frame.blocks_ * SUBBAND_4;
378 }
379
Analyze8Subbands(int position,int16_t x[CHANNEL_NUM][BUFFER_SIZE],Frame & frame,int increment)380 int Encoder::Analyze8Subbands(int position, int16_t x[CHANNEL_NUM][BUFFER_SIZE],
381 Frame& frame, int increment)
382 {
383 int16_t *eightBandBuff = nullptr;
384 for (int ch = VALUE_0; ch < frame.channels_; ch++) {
385 eightBandBuff = &x[ch][position - SUBBAND_8 * increment + frame.blocks_ * SUBBAND_8];
386 for (int blk = VALUE_0; blk < frame.blocks_; blk += increment) {
387 Analyze8SubbandsInternal(eightBandBuff, frame.audioSamples_[blk][ch],
388 frame.audioSamples_[blk + VALUE_1][ch] - frame.audioSamples_[blk][ch]);
389 eightBandBuff -= SUBBAND_8 * increment;
390 }
391 }
392 return frame.blocks_ * SUBBAND_8;
393 }
394
UnalignedBigEndian(const uint8_t * ptr)395 static int16_t UnalignedBigEndian(const uint8_t *ptr)
396 {
397 return static_cast<int16_t>((ptr[VALUE_0] << VALUE_8) | ptr[VALUE_1]);
398 }
399
UnalignedLittleEndian(const uint8_t * ptr)400 static int16_t UnalignedLittleEndian(const uint8_t *ptr)
401 {
402 return static_cast<int16_t>(ptr[VALUE_0] | (ptr[VALUE_1] << VALUE_8));
403 }
404
Get4SubbandSamplingPoint(const uint8_t * pcm,int16_t x[CHANNEL_NUM][BUFFER_SIZE],int samples,int channels,int bigEndian)405 int Encoder::Get4SubbandSamplingPoint(const uint8_t *pcm, int16_t x[CHANNEL_NUM][BUFFER_SIZE],
406 int samples, int channels, int bigEndian)
407 {
408 if (position_ < samples) {
409 if (channels > VALUE_0) {
410 (void)memcpy_s(&x[VALUE_0][BUFFER_SIZE - VALUE_40], VALUE_36 * sizeof(int16_t), &x[VALUE_0][position_],
411 VALUE_36 * sizeof(int16_t));
412 }
413 if (channels > VALUE_1) {
414 (void)memcpy_s(&x[VALUE_1][BUFFER_SIZE - VALUE_40], VALUE_36 * sizeof(int16_t), &x[VALUE_1][position_],
415 VALUE_36 * sizeof(int16_t));
416 }
417 position_ = BUFFER_SIZE - VALUE_40;
418 }
419 #define PCM(i) (bigEndian ? UnalignedBigEndian(pcm + (i) * VALUE_2) : UnalignedLittleEndian(pcm + (i) * VALUE_2))
420 while ((samples -= VALUE_8) >= VALUE_0) {
421 position_ -= VALUE_8;
422 if (channels > VALUE_0) {
423 int16_t *fourBandBuffer = &x[VALUE_0][position_];
424 fourBandBuffer[VALUE_0] = PCM(VALUE_0 + VALUE_7 * channels);
425 fourBandBuffer[VALUE_1] = PCM(VALUE_0 + VALUE_3 * channels);
426 fourBandBuffer[VALUE_2] = PCM(VALUE_0 + VALUE_6 * channels);
427 fourBandBuffer[VALUE_3] = PCM(VALUE_0 + VALUE_4 * channels);
428 fourBandBuffer[VALUE_4] = PCM(VALUE_0 + VALUE_0 * channels);
429 fourBandBuffer[VALUE_5] = PCM(VALUE_0 + VALUE_2 * channels);
430 fourBandBuffer[VALUE_6] = PCM(VALUE_0 + VALUE_1 * channels);
431 fourBandBuffer[VALUE_7] = PCM(VALUE_0 + VALUE_5 * channels);
432 }
433 if (channels > VALUE_1) {
434 int16_t *fourBandBuffer = &x[VALUE_1][position_];
435 fourBandBuffer[VALUE_0] = PCM(VALUE_1 + VALUE_7 * channels);
436 fourBandBuffer[VALUE_1] = PCM(VALUE_1 + VALUE_3 * channels);
437 fourBandBuffer[VALUE_2] = PCM(VALUE_1 + VALUE_6 * channels);
438 fourBandBuffer[VALUE_3] = PCM(VALUE_1 + VALUE_4 * channels);
439 fourBandBuffer[VALUE_4] = PCM(VALUE_1 + VALUE_0 * channels);
440 fourBandBuffer[VALUE_5] = PCM(VALUE_1 + VALUE_2 * channels);
441 fourBandBuffer[VALUE_6] = PCM(VALUE_1 + VALUE_1 * channels);
442 fourBandBuffer[VALUE_7] = PCM(VALUE_1 + VALUE_5 * channels);
443 }
444 pcm += VALUE_16 * channels;
445 }
446 return position_;
447 }
448
Get8SubbandSamplingPointInternal(const uint8_t * pcm,int16_t (* x)[BUFFER_SIZE],int * samples,int channels,int bigEndian)449 void Encoder::Get8SubbandSamplingPointInternal(const uint8_t *pcm, int16_t (*x)[BUFFER_SIZE],
450 int *samples, int channels, int bigEndian)
451 {
452 #define PCM(i) (bigEndian ? UnalignedBigEndian(pcm + (i) * VALUE_2) : UnalignedLittleEndian(pcm + (i) * VALUE_2))
453
454 if (position_ % VALUE_16 == VALUE_8) {
455 position_ -= VALUE_8;
456 *samples -= VALUE_8;
457 if (channels > VALUE_0) {
458 int16_t *eightBandBuffer = &x[VALUE_0][position_];
459 eightBandBuffer[VALUE_0] = PCM(VALUE_0 + (VALUE_15 - VALUE_8) * channels);
460 eightBandBuffer[VALUE_2] = PCM(VALUE_0 + (VALUE_14 - VALUE_8) * channels);
461 eightBandBuffer[VALUE_3] = PCM(VALUE_0 + (VALUE_8 - VALUE_8) * channels);
462 eightBandBuffer[VALUE_4] = PCM(VALUE_0 + (VALUE_13 - VALUE_8) * channels);
463 eightBandBuffer[VALUE_5] = PCM(VALUE_0 + (VALUE_9 - VALUE_8) * channels);
464 eightBandBuffer[VALUE_6] = PCM(VALUE_0 + (VALUE_12 - VALUE_8) * channels);
465 eightBandBuffer[VALUE_7] = PCM(VALUE_0 + (VALUE_10 - VALUE_8) * channels);
466 eightBandBuffer[VALUE_8] = PCM(VALUE_0 + (VALUE_11 - VALUE_8) * channels);
467 }
468 if (channels > VALUE_1) {
469 int16_t *eightBandBuffer = &x[VALUE_1][position_];
470 eightBandBuffer[VALUE_0] = PCM(VALUE_1 + (VALUE_15 - VALUE_8) * channels);
471 eightBandBuffer[VALUE_2] = PCM(VALUE_1 + (VALUE_14 - VALUE_8) * channels);
472 eightBandBuffer[VALUE_3] = PCM(VALUE_1 + (VALUE_8 - VALUE_8) * channels);
473 eightBandBuffer[VALUE_4] = PCM(VALUE_1 + (VALUE_13 - VALUE_8) * channels);
474 eightBandBuffer[VALUE_5] = PCM(VALUE_1 + (VALUE_9 - VALUE_8) * channels);
475 eightBandBuffer[VALUE_6] = PCM(VALUE_1 + (VALUE_12 - VALUE_8) * channels);
476 eightBandBuffer[VALUE_7] = PCM(VALUE_1 + (VALUE_10 - VALUE_8) * channels);
477 eightBandBuffer[VALUE_8] = PCM(VALUE_1 + (VALUE_11 - VALUE_8) * channels);
478 }
479
480 pcm += VALUE_16 * channels;
481 }
482 #undef PCM
483 }
484
Get8SubbandSamplingPoint16(const uint8_t * pcm,int16_t (* x)[BUFFER_SIZE],int * samples,int channels,int bigEndian)485 void Encoder::Get8SubbandSamplingPoint16(const uint8_t *pcm, int16_t (*x)[BUFFER_SIZE],
486 int *samples, int channels, int bigEndian)
487 {
488 #define PCM(i) (bigEndian ? UnalignedBigEndian(pcm + (i) * VALUE_2) : UnalignedLittleEndian(pcm + (i) * VALUE_2))
489
490 while (*samples >= VALUE_16) {
491 position_ -= VALUE_16;
492 if (channels > VALUE_0) {
493 int16_t *eightBandBuffer = &x[VALUE_0][position_];
494 eightBandBuffer[VALUE_0] = PCM(VALUE_0 + VALUE_15 * channels);
495 eightBandBuffer[VALUE_1] = PCM(VALUE_0 + VALUE_7 * channels);
496 eightBandBuffer[VALUE_2] = PCM(VALUE_0 + VALUE_14 * channels);
497 eightBandBuffer[VALUE_3] = PCM(VALUE_0 + VALUE_8 * channels);
498 eightBandBuffer[VALUE_4] = PCM(VALUE_0 + VALUE_13 * channels);
499 eightBandBuffer[VALUE_5] = PCM(VALUE_0 + VALUE_9 * channels);
500 eightBandBuffer[VALUE_6] = PCM(VALUE_0 + VALUE_12 * channels);
501 eightBandBuffer[VALUE_7] = PCM(VALUE_0 + VALUE_10 * channels);
502 eightBandBuffer[VALUE_8] = PCM(VALUE_0 + VALUE_11 * channels);
503 eightBandBuffer[VALUE_9] = PCM(VALUE_0 + VALUE_3 * channels);
504 eightBandBuffer[VALUE_10] = PCM(VALUE_0 + VALUE_6 * channels);
505 eightBandBuffer[VALUE_11] = PCM(VALUE_0 + VALUE_0 * channels);
506 eightBandBuffer[VALUE_12] = PCM(VALUE_0 + VALUE_5 * channels);
507 eightBandBuffer[VALUE_13] = PCM(VALUE_0 + VALUE_1 * channels);
508 eightBandBuffer[VALUE_14] = PCM(VALUE_0 + VALUE_4 * channels);
509 eightBandBuffer[VALUE_15] = PCM(VALUE_0 + VALUE_2 * channels);
510 }
511 if (channels > VALUE_1) {
512 int16_t *eightBandBuffer = &x[VALUE_1][position_];
513 eightBandBuffer[VALUE_0] = PCM(VALUE_1 + VALUE_15 * channels);
514 eightBandBuffer[VALUE_1] = PCM(VALUE_1 + VALUE_7 * channels);
515 eightBandBuffer[VALUE_2] = PCM(VALUE_1 + VALUE_14 * channels);
516 eightBandBuffer[VALUE_3] = PCM(VALUE_1 + VALUE_8 * channels);
517 eightBandBuffer[VALUE_4] = PCM(VALUE_1 + VALUE_13 * channels);
518 eightBandBuffer[VALUE_5] = PCM(VALUE_1 + VALUE_9 * channels);
519 eightBandBuffer[VALUE_6] = PCM(VALUE_1 + VALUE_12 * channels);
520 eightBandBuffer[VALUE_7] = PCM(VALUE_1 + VALUE_10 * channels);
521 eightBandBuffer[VALUE_8] = PCM(VALUE_1 + VALUE_11 * channels);
522 eightBandBuffer[VALUE_9] = PCM(VALUE_1 + VALUE_3 * channels);
523 eightBandBuffer[VALUE_10] = PCM(VALUE_1 + VALUE_6 * channels);
524 eightBandBuffer[VALUE_11] = PCM(VALUE_1 + VALUE_0 * channels);
525 eightBandBuffer[VALUE_12] = PCM(VALUE_1 + VALUE_5 * channels);
526 eightBandBuffer[VALUE_13] = PCM(VALUE_1 + VALUE_1 * channels);
527 eightBandBuffer[VALUE_14] = PCM(VALUE_1 + VALUE_4 * channels);
528 eightBandBuffer[VALUE_15] = PCM(VALUE_1 + VALUE_2 * channels);
529 }
530 pcm += VALUE_32 * channels;
531 *samples -= VALUE_16;
532 }
533 #undef PCM
534 }
535
Get8SubbandSamplingPoint8(const uint8_t * pcm,int16_t (* x)[BUFFER_SIZE],int * samples,int channels,int bigEndian)536 void Encoder::Get8SubbandSamplingPoint8(const uint8_t *pcm, int16_t (*x)[BUFFER_SIZE],
537 int *samples, int channels, int bigEndian)
538 {
539 #define PCM(i) (bigEndian ? UnalignedBigEndian(pcm + (i) * VALUE_2) : UnalignedLittleEndian(pcm + (i) * VALUE_2))
540
541 if (*samples == VALUE_8) {
542 position_ -= VALUE_8;
543 if (channels > VALUE_0) {
544 int16_t *eightBandBuffer = &x[VALUE_0][position_];
545 eightBandBuffer[-VALUE_7] = PCM(VALUE_0 + VALUE_7 * channels);
546 eightBandBuffer[VALUE_1] = PCM(VALUE_0 + VALUE_3 * channels);
547 eightBandBuffer[VALUE_2] = PCM(VALUE_0 + VALUE_6 * channels);
548 eightBandBuffer[VALUE_3] = PCM(VALUE_0 + VALUE_0 * channels);
549 eightBandBuffer[VALUE_4] = PCM(VALUE_0 + VALUE_5 * channels);
550 eightBandBuffer[VALUE_5] = PCM(VALUE_0 + VALUE_1 * channels);
551 eightBandBuffer[VALUE_6] = PCM(VALUE_0 + VALUE_4 * channels);
552 eightBandBuffer[VALUE_7] = PCM(VALUE_0 + VALUE_2 * channels);
553 }
554 if (channels > VALUE_1) {
555 int16_t *eightBandBuffer = &x[VALUE_1][position_];
556 eightBandBuffer[-VALUE_7] = PCM(VALUE_1 + VALUE_7 * channels);
557 eightBandBuffer[VALUE_1] = PCM(VALUE_1 + VALUE_3 * channels);
558 eightBandBuffer[VALUE_2] = PCM(VALUE_1 + VALUE_6 * channels);
559 eightBandBuffer[VALUE_3] = PCM(VALUE_1 + VALUE_0 * channels);
560 eightBandBuffer[VALUE_4] = PCM(VALUE_1 + VALUE_5 * channels);
561 eightBandBuffer[VALUE_5] = PCM(VALUE_1 + VALUE_1 * channels);
562 eightBandBuffer[VALUE_6] = PCM(VALUE_1 + VALUE_4 * channels);
563 eightBandBuffer[VALUE_7] = PCM(VALUE_1 + VALUE_2 * channels);
564 }
565 }
566 #undef PCM
567 }
568
Get8SubbandSamplingPoint(const uint8_t * pcm,int16_t (* x)[BUFFER_SIZE],int * samples,int channels,int bigEndian)569 int Encoder::Get8SubbandSamplingPoint(const uint8_t *pcm, int16_t (*x)[BUFFER_SIZE],
570 int *samples, int channels, int bigEndian)
571 {
572 if (position_ < *samples) {
573 if (channels > VALUE_0) {
574 (void)memcpy_s(&x[VALUE_0][BUFFER_SIZE - VALUE_72], VALUE_72 * sizeof(int16_t), &x[VALUE_0][position_],
575 VALUE_72 * sizeof(int16_t));
576 }
577 if (channels > VALUE_1) {
578 (void)memcpy_s(&x[VALUE_1][BUFFER_SIZE - VALUE_72], VALUE_72 * sizeof(int16_t), &x[VALUE_1][position_],
579 VALUE_72 * sizeof(int16_t));
580 }
581 position_ = BUFFER_SIZE - VALUE_72;
582 }
583
584 Get8SubbandSamplingPointInternal(pcm, x, samples, channels, bigEndian);
585
586 Get8SubbandSamplingPoint16(pcm, x, samples, channels, bigEndian);
587
588 Get8SubbandSamplingPoint8(pcm, x, samples, channels, bigEndian);
589
590 return position_;
591 }
592
SbcClz(uint32_t x)593 static int SbcClz(uint32_t x)
594 {
595 #ifdef __GNUC__
596 return __builtin_clz(x);
597 #else
598 int cnt = VALUE_0;
599 while (x) {
600 cnt++;
601 x >>= VALUE_1;
602 }
603 return VALUE_32 - cnt;
604 #endif
605 }
606
CalculateScalefactors(int32_t samples[BLOCK_NUM][CHANNEL_NUM][SUBBAND_NUM],uint32_t scaleFactor[CHANNEL_NUM][SUBBAND_NUM],int blocks,int channels,int subbands) const607 void Encoder::CalculateScalefactors(int32_t samples[BLOCK_NUM][CHANNEL_NUM][SUBBAND_NUM],
608 uint32_t scaleFactor[CHANNEL_NUM][SUBBAND_NUM],
609 int blocks, int channels, int subbands) const
610 {
611 for (int ch = VALUE_0; ch < channels; ch++) {
612 for (int sb = VALUE_0; sb < subbands; sb++) {
613 uint32_t x = VALUE_1 << SCALE_OUT_BITS;
614 for (int blk = VALUE_0; blk < blocks; blk++) {
615 int32_t tmp = FABS(samples[blk][ch][sb]);
616 if (tmp != VALUE_0) {
617 x |= tmp - VALUE_1;
618 }
619 }
620 scaleFactor[ch][sb] = (VALUE_31 - SCALE_OUT_BITS) - SbcClz(x);
621 }
622 }
623 }
624
CalculateScalefactorsJointInternal(uint32_t & x,uint32_t & y,int32_t & tmp0,int32_t & tmp1)625 void Encoder::CalculateScalefactorsJointInternal(uint32_t &x, uint32_t &y, int32_t &tmp0, int32_t &tmp1)
626 {
627 int sb = frame_.subbands_ - VALUE_1;
628 for (int blk = VALUE_0; blk < frame_.blocks_; blk++) {
629 tmp0 = FABS(frame_.audioSamples_[blk][VALUE_0][sb]);
630 tmp1 = FABS(frame_.audioSamples_[blk][VALUE_1][sb]);
631 if (tmp0 != VALUE_0) {
632 x |= tmp0 - VALUE_1;
633 }
634 if (tmp1 != VALUE_0) {
635 y |= tmp1 - VALUE_1;
636 }
637 }
638 frame_.scaleFactor_[VALUE_0][sb] = (VALUE_31 - SCALE_OUT_BITS) - SbcClz(x);
639 frame_.scaleFactor_[VALUE_1][sb] = (VALUE_31 - SCALE_OUT_BITS) - SbcClz(y);
640 }
641
CalculateScalefactorsJointForTheRestSubband(uint32_t & x,uint32_t & y,int32_t & tmp0,int32_t & tmp1,int & joint)642 void Encoder::CalculateScalefactorsJointForTheRestSubband(uint32_t &x, uint32_t &y,
643 int32_t &tmp0, int32_t &tmp1,
644 int &joint)
645 {
646 int sb = frame_.subbands_ - VALUE_1;
647 while (--sb >= VALUE_0) {
648 int32_t sbSampleJoint[VALUE_16][VALUE_2];
649 x = VALUE_1 << SCALE_OUT_BITS;
650 y = VALUE_1 << SCALE_OUT_BITS;
651 for (int blk = VALUE_0; blk < frame_.blocks_; blk++) {
652 tmp0 = frame_.audioSamples_[blk][VALUE_0][sb];
653 tmp1 = frame_.audioSamples_[blk][VALUE_1][sb];
654 sbSampleJoint[blk][VALUE_0] = ASR(tmp0, VALUE_1) + ASR(tmp1, VALUE_1);
655 sbSampleJoint[blk][VALUE_1] = ASR(tmp0, VALUE_1) - ASR(tmp1, VALUE_1);
656 tmp0 = FABS(tmp0);
657 tmp1 = FABS(tmp1);
658 if (tmp0 != VALUE_0) {
659 x |= tmp0 - VALUE_1;
660 }
661 if (tmp1 != VALUE_0) {
662 y |= tmp1 - VALUE_1;
663 }
664 }
665 frame_.scaleFactor_[VALUE_0][sb] = (VALUE_31 - SCALE_OUT_BITS) - SbcClz(x);
666 frame_.scaleFactor_[VALUE_1][sb] = (VALUE_31 - SCALE_OUT_BITS) - SbcClz(y);
667 x = VALUE_1 << SCALE_OUT_BITS;
668 y = VALUE_1 << SCALE_OUT_BITS;
669 for (int blk = VALUE_0; blk < frame_.blocks_; blk++) {
670 tmp0 = FABS(sbSampleJoint[blk][VALUE_0]);
671 tmp1 = FABS(sbSampleJoint[blk][VALUE_1]);
672 if (tmp0 != VALUE_0) {
673 x |= tmp0 - VALUE_1;
674 }
675 if (tmp1 != VALUE_0) {
676 y |= tmp1 - VALUE_1;
677 }
678 }
679 x = (VALUE_31 - SCALE_OUT_BITS) - SbcClz(x);
680 y = (VALUE_31 - SCALE_OUT_BITS) - SbcClz(y);
681
682 if ((frame_.scaleFactor_[VALUE_0][sb] + frame_.scaleFactor_[VALUE_1][sb]) > x + y) {
683 joint |= VALUE_1 << (frame_.subbands_ - VALUE_1 - sb);
684 frame_.scaleFactor_[VALUE_0][sb] = x;
685 frame_.scaleFactor_[VALUE_1][sb] = y;
686 for (int blk = VALUE_0; blk < frame_.blocks_; blk++) {
687 frame_.audioSamples_[blk][VALUE_0][sb] = sbSampleJoint[blk][VALUE_0];
688 frame_.audioSamples_[blk][VALUE_1][sb] = sbSampleJoint[blk][VALUE_1];
689 }
690 }
691 }
692 }
693
694
CalculateScalefactorsJoint(void)695 int Encoder::CalculateScalefactorsJoint(void)
696 {
697 int joint = VALUE_0;
698 int32_t tmp0 = 0;
699 int32_t tmp1 = 0;
700 uint32_t x = 0;
701 uint32_t y = 0;
702
703 x = VALUE_1 << SCALE_OUT_BITS;
704 y = VALUE_1 << SCALE_OUT_BITS;
705 CalculateScalefactorsJointInternal(x, y, tmp0, tmp1);
706 CalculateScalefactorsJointForTheRestSubband(x, y, tmp0, tmp1, joint);
707 return joint;
708 }
709 } // namespace sbc
710