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 16 #ifndef SBC_ENCODER_H 17 #define SBC_ENCODER_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <sys/types.h> 22 #include "sbc_codec.h" 23 #include "sbc_constant.h" 24 #include "sbc_frame.h" 25 26 namespace sbc { 27 class Encoder : public IEncoderBase { 28 public: 29 static constexpr int BUFFER_SIZE = 328; 30 explicit Encoder(); 31 virtual ~Encoder(); 32 ssize_t SBCEncode(const CodecParam& codecParam, const uint8_t* in, size_t iLength, uint8_t* out, 33 size_t oLength, size_t* written) override; 34 35 private: 36 void Init(const Frame& frame); 37 static size_t CalculateFrameLength(const CodecParam& codecParam); 38 static size_t CalculateCodecSize(const CodecParam& codecParam); 39 void UpdateCodecFormat(const CodecParam& codecParam); 40 void Analyze4SubbandsInternal(int16_t *x, int32_t *outData, int increseValue); 41 void Analyze8SubbandsInternal(int16_t *x, int32_t *outData, int increseValue); 42 static void AnalyzeFourForPolyphaseFilter(int32_t *temp, const int16_t *inData, const int16_t *consts); 43 static void AnalyzeFourForScaling(int32_t *temp1, int16_t *temp2); 44 static void AnalyzeFourForCosTransform(int32_t *temp1, int16_t *temp2, const int16_t *consts); 45 void AnalyzeFourFunction(const int16_t *inData, int32_t *outData, const int16_t *consts) const; 46 static void AnalyzeEightForPolyphaseFilter(int32_t *temp, const int16_t *inData, const int16_t *consts); 47 static void AnalyzeEightForScaling(int32_t *temp1, int16_t *temp2); 48 static void AnalyzeEightForCosTransform(int32_t *temp1, int16_t *temp2, const int16_t *consts); 49 void AnalyzeEightFunction(const int16_t *inData, int32_t *outData, const int16_t *consts) const; 50 int Analyze4Subbands(int position, int16_t x[2][BUFFER_SIZE], Frame& frame, int increment); 51 int Analyze8Subbands(int position, int16_t x[2][BUFFER_SIZE], Frame& frame, int increment); 52 void Get8SubbandSamplingPointInternal(const uint8_t* pcm, int16_t(*x)[BUFFER_SIZE], 53 int *samples, int channels, int bigEndian); 54 void Get8SubbandSamplingPoint16(const uint8_t* pcm, int16_t(*x)[BUFFER_SIZE], 55 int *samples, int channels, int bigEndian); 56 void Get8SubbandSamplingPoint8(const uint8_t* pcm, int16_t(*x)[BUFFER_SIZE], 57 int *samples, int channels, int bigEndian); 58 int Get8SubbandSamplingPoint(const uint8_t* pcm, int16_t(*x)[BUFFER_SIZE], 59 int *samples, int channels, int bigEndian); 60 int Get4SubbandSamplingPoint(const uint8_t* pcm, int16_t x[2][BUFFER_SIZE], 61 int samples, int channels, int bigEndian); 62 void CalculateScalefactors(int32_t samples[16][2][8], uint32_t scaleFactor[2][8], 63 int blocks, int channels, int subbands) const; 64 void CalculateScalefactorsJointInternal(uint32_t &x, uint32_t &y, int32_t &tmp0, int32_t &tmp1); 65 void CalculateScalefactorsJointForTheRestSubband(uint32_t &x, uint32_t &y, int32_t &tmp0, 66 int32_t &tmp1, int &joint); 67 int CalculateScalefactorsJoint(void); 68 69 bool initialized_ {}; 70 Frame frame_ {}; 71 int position_ {}; 72 uint8_t increment_ {}; 73 int16_t x_[2][BUFFER_SIZE] {}; 74 }; 75 } // namespace sbc 76 #endif // SBC_ENCODER_H 77