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_FRAME_H
17 #define SBC_FRAME_H
18 
19 #include <sys/types.h>
20 #include <cstddef>
21 #include <cstdint>
22 
23 namespace sbc {
24 
25 struct sbcInnerInfo {
26     int channel;
27     int subband;
28 };
29 
30 class Frame {
31 public:
32     uint8_t frequency_ {};
33     uint8_t blockMode_ {};
34     uint8_t blocks_ {};
35     uint8_t channelMode_ {};
36     uint8_t channels_ {};
37     uint8_t allocation_ {};
38     uint8_t subbandMode_ {};
39     uint8_t subbands_ {};
40     uint8_t bitpool_ {};
41     uint8_t joint_ {};
42     uint32_t scaleFactor_[2][8] {};
43     int32_t audioSamples_[16][2][8] {};
44     int32_t samples_[16][2][8] {};
45     uint16_t codeSize_ {};
46     uint16_t length_ {};
47 
48     Frame();
49     bool IsValid() const;
50     int Unpack(const uint8_t* bufStream, size_t size);
51     ssize_t Pack(uint8_t* bufStream, const Frame& frame, int joint);
52 
53 private:
54     ssize_t PackFrameInternal(const Frame& frame, uint8_t* bufStream, int subbands, int channels, int joint);
55     int UnpackFrameStream(Frame& frame, const uint8_t* bufStream, size_t len);
56     void SbcCalculateBits(const Frame& frame, int (*bits)[8]);
57     void SbcCaculateLevelsAndSampleDelta(const Frame& frame, int (*bits)[8],
58                                          uint32_t (*levels)[8], uint32_t (*sampleDelta)[8]);
59     void SbcCalculateBitsNormal(const Frame& frame, int (*bits)[8]);
60     void SbcCalculateBitsStereo(const Frame& frame, int (*bits)[8]);
61     int SbcCalculateBitsMax(const Frame& frame, const int channel, const int subbands, int (*bits)[8]);
62     int SbcCalculateBitsMaxStereo(const Frame& frame, const int subbands, int (*bits)[8]);
63     int SbcCalculateBitsMaxStereoSNR(const Frame& frame, const int subbands, int (*bits)[8]);
64     int SbcCalculateBitsMaxStereoLoudness(const Frame& frame, const int subbands, int (*bits)[8]);
65     void SbcCalculateBitsSliceStereo(const uint8_t bitPool, const int subbands, int (*bits)[8], int *bitCount,
66                                      int *bitSlice) const;
67     int SbcCalculateBitsSliceCountStereo(const int subbands, int (*bits)[8], int *bitSlice);
68     static void SbcCalculateBitsInnerStereo(const int subbands, int bitSlice, int (*bitNeed)[8], int (*bits)[8]);
69     int SbcCalculateBitsSliceNormal(const uint8_t bitPool, const int subbands, int (*bits)[8], int channel,
70                                     int *bitSlice) const;
71     static int SbcCalculateLoudness(const Frame& frame, int channel, int subband);
72     static uint8_t SbcCrc8(const uint8_t* data, size_t len);
73     uint AdjustSbcConsumeValue(uint consumed);
74     int32_t GetSbcAudioSample(int audioSample, uint32_t scale, uint32_t level);
75     uint8_t CalculateSbcCrc(
76         Frame &frame, const uint8_t *bufStream, sbcInnerInfo sbcInnerInfo, uint &consumed, uint &crcpos);
77 };
78 } // namespace sbc
79 #endif // SBC_FRAME_H
80