• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "format_converter.h"
17 #include "audio_stream_info.h"
18 #include <string>
19 #include <vector>
20 
21 namespace OHOS {
22 namespace AudioStandard {
23 #define PCM_FLOAT_EPS 1e-6f
24 #define BIT_16 16
25 static constexpr int32_t VOLUME_SHIFT_NUMBER = 16; // 1 >> 16 = 65536, max volume
26 
CapMax(float v)27 static float CapMax(float v)
28 {
29     float value = v;
30     if (v >= 1.0f) {
31         value = 1.0f - PCM_FLOAT_EPS;
32     } else if (v <= -1.0f) {
33         value = -1.0f + PCM_FLOAT_EPS;
34     }
35     return value;
36 }
37 
ConvertFromFloatTo16Bit(const float * a)38 static int16_t ConvertFromFloatTo16Bit(const float *a)
39 {
40     float tmp = *a;
41     float v = CapMax(tmp) * (1 << (BIT_16 - 1));
42     return static_cast<int16_t>(v);
43 }
44 
DataAccumulationFromVolume(const std::vector<AudioStreamData> & srcDataList,const AudioStreamData & dstData)45 void FormatConverter::DataAccumulationFromVolume(const std::vector<AudioStreamData> &srcDataList,
46     const AudioStreamData &dstData)
47 {
48     size_t srcListSize = srcDataList.size();
49     size_t dataLength = dstData.bufferDesc.dataLength;
50     dataLength /= 2; // SAMPLE_S16LE--> 2 byte
51     int16_t *dstPtr = reinterpret_cast<int16_t *>(dstData.bufferDesc.buffer);
52     for (size_t offset = 0; dataLength > 0; dataLength--) {
53         int32_t sum = 0;
54         for (size_t i = 0; i < srcListSize; i++) {
55             int32_t vol = srcDataList[i].volumeStart; // change to modify volume of each channel
56             int16_t *srcPtr = reinterpret_cast<int16_t *>(srcDataList[i].bufferDesc.buffer) + offset;
57             sum += (*srcPtr * static_cast<int64_t>(vol)) >> VOLUME_SHIFT_NUMBER; // 1/65536
58         }
59         offset++;
60         *dstPtr++ = sum > INT16_MAX ? INT16_MAX : (sum < INT16_MIN ? INT16_MIN : sum);
61     }
62 }
63 
S16MonoToS16Stereo(const BufferDesc & srcDesc,const BufferDesc & dstDesc)64 int32_t FormatConverter::S16MonoToS16Stereo(const BufferDesc &srcDesc, const BufferDesc &dstDesc)
65 {
66     size_t half = 2; // mono(1) -> stereo(2)
67     if (srcDesc.bufLength != dstDesc.bufLength / half || srcDesc.buffer == nullptr || dstDesc.buffer == nullptr) {
68         return -1;
69     }
70     int16_t *stcPtr = reinterpret_cast<int16_t *>(srcDesc.buffer);
71     int16_t *dstPtr = reinterpret_cast<int16_t *>(dstDesc.buffer);
72     size_t count = srcDesc.bufLength / sizeof(int16_t);
73     for (size_t idx = 0; idx < count; idx++) {
74         *(dstPtr++) = *stcPtr;
75         *(dstPtr++) = *stcPtr++;
76     }
77     return 0;
78 }
79 
S16StereoToS16Mono(const BufferDesc & srcDesc,const BufferDesc & dstDesc)80 int32_t FormatConverter::S16StereoToS16Mono(const BufferDesc &srcDesc, const BufferDesc &dstDesc)
81 {
82     size_t half = 2; // stereo(2) -> mono(1)
83     if (dstDesc.bufLength != srcDesc.bufLength / half || srcDesc.buffer == nullptr || dstDesc.buffer == nullptr) {
84         return -1;
85     }
86     int16_t *stcPtr = reinterpret_cast<int16_t *>(srcDesc.buffer);
87     int16_t *dstPtr = reinterpret_cast<int16_t *>(dstDesc.buffer);
88     size_t count = srcDesc.bufLength / half / sizeof(int16_t);
89     for (size_t idx = 0; idx < count; idx++) {
90         *(dstPtr++) = (*stcPtr + *(stcPtr + 1)) / 2; // To obtain mono channel, add left to right, then divide by 2
91         stcPtr += 2; // ptr++ on mono is equivalent to ptr+=2 on stereo
92     }
93     return 0;
94 }
95 
S16StereoToF32Stereo(const BufferDesc & srcDesc,const BufferDesc & dstDesc)96 int32_t FormatConverter::S16StereoToF32Stereo(const BufferDesc &srcDesc, const BufferDesc &dstDesc)
97 {
98     size_t half = 2;
99     if (srcDesc.bufLength != dstDesc.bufLength / half || srcDesc.buffer == nullptr || dstDesc.buffer == nullptr) {
100         return -1;
101     }
102     int16_t *srcPtr = reinterpret_cast<int16_t *>(srcDesc.buffer);
103     float *dstPtr = reinterpret_cast<float *>(dstDesc.buffer);
104     size_t count = srcDesc.bufLength / sizeof(int16_t);
105     const float FLOAT_SCALE = 1.0f / (1 << (BIT_16 - 1));
106     for (size_t idx = 0; idx < count; idx++) {
107         *dstPtr = (*srcPtr) * FLOAT_SCALE;
108         dstPtr++;
109         srcPtr++;
110     }
111     return 0;
112 }
113 
S16StereoToF32Mono(const BufferDesc & srcDesc,const BufferDesc & dstDesc)114 int32_t FormatConverter::S16StereoToF32Mono(const BufferDesc &srcDesc, const BufferDesc &dstDesc)
115 {
116     size_t half = 2;
117     if (srcDesc.bufLength != dstDesc.bufLength || srcDesc.buffer == nullptr || dstDesc.buffer == nullptr) {
118         return -1;
119     }
120     int16_t *srcPtr = reinterpret_cast<int16_t *>(srcDesc.buffer);
121     float *dstPtr = reinterpret_cast<float *>(dstDesc.buffer);
122     size_t count = srcDesc.bufLength / half / sizeof(int16_t);
123     const float FLOAT_SCALE = 1.0f / (1 << (BIT_16 - 1));
124     const size_t SRC_INCREMENT = 2;
125     for (size_t idx = 0; idx < count; idx++) {
126         *dstPtr = (static_cast<float>(*srcPtr + *(srcPtr + 1)) / half) * FLOAT_SCALE;
127         dstPtr++;
128         srcPtr += SRC_INCREMENT;
129     }
130     return 0;
131 }
132 
F32MonoToS16Stereo(const BufferDesc & srcDesc,const BufferDesc & dstDesc)133 int32_t FormatConverter::F32MonoToS16Stereo(const BufferDesc &srcDesc, const BufferDesc &dstDesc)
134 {
135     size_t quarter = 4;
136     if (srcDesc.bufLength != dstDesc.bufLength || srcDesc.buffer == nullptr || dstDesc.buffer == nullptr ||
137         srcDesc.bufLength % quarter != 0) {
138         return -1;
139     }
140     float *stcPtr = reinterpret_cast<float *>(srcDesc.buffer);
141     int16_t *dstPtr = reinterpret_cast<int16_t *>(dstDesc.buffer);
142     size_t count = srcDesc.bufLength / quarter;
143 
144     for (size_t idx = 0; idx < count; idx++) {
145         int16_t temp = ConvertFromFloatTo16Bit(stcPtr);
146         stcPtr++;
147         *(dstPtr++) = temp;
148         *(dstPtr++) = temp;
149     }
150     return 0;
151 }
152 
F32StereoToS16Stereo(const BufferDesc & srcDesc,const BufferDesc & dstDesc)153 int32_t FormatConverter::F32StereoToS16Stereo(const BufferDesc &srcDesc, const BufferDesc &dstDesc)
154 {
155     size_t half = 2;
156     if (srcDesc.bufLength / half != dstDesc.bufLength || srcDesc.buffer == nullptr || dstDesc.buffer == nullptr ||
157         dstDesc.bufLength % half != 0) {
158         return -1;
159     }
160     float *stcPtr = reinterpret_cast<float *>(srcDesc.buffer);
161     int16_t *dstPtr = reinterpret_cast<int16_t *>(dstDesc.buffer);
162     size_t count = srcDesc.bufLength / half / half;
163 
164     for (size_t idx = 0; idx < count; idx++) {
165         int16_t temp = ConvertFromFloatTo16Bit(stcPtr);
166         stcPtr++;
167         *(dstPtr++) = temp;
168     }
169     return 0;
170 }
171 
S32MonoToS16Mono(std::vector<char> & audioBuffer,std::vector<char> & audioBufferConverted)172 int32_t FormatConverter::S32MonoToS16Mono(std::vector<char> &audioBuffer, std::vector<char> &audioBufferConverted)
173 {
174     size_t half = 2;
175     int32_t size = audioBuffer.size();
176     if (size == 0) {
177         return -1;
178     }
179 
180     audioBufferConverted.resize(size / half);
181     int32_t *stcPtr = reinterpret_cast<int32_t *>(audioBuffer.data());
182     int16_t *dstPtr = reinterpret_cast<int16_t *>(audioBufferConverted.data());
183     size_t count = size / sizeof(int32_t);
184 
185     double maxInt32 = INT32_MAX;
186     double maxInt16 = INT16_MAX;
187     for (size_t idx = 0; idx < count; idx++) {
188         int16_t temp = static_cast<int16_t>((static_cast<double>(*stcPtr) / maxInt32) * maxInt16);
189         *(dstPtr++) = temp;
190         stcPtr++;
191     }
192     return 0;
193 }
194 
S32StereoToS16Stereo(std::vector<char> & audioBuffer,std::vector<char> & audioBufferConverted)195 int32_t FormatConverter::S32StereoToS16Stereo(std::vector<char> &audioBuffer, std::vector<char> &audioBufferConverted)
196 {
197     size_t half = 2;
198     int32_t size = audioBuffer.size();
199     if (size == 0) {
200         return -1;
201     }
202 
203     audioBufferConverted.resize(size / half);
204     int32_t *stcPtr = reinterpret_cast<int32_t *>(audioBuffer.data());
205     int16_t *dstPtr = reinterpret_cast<int16_t *>(audioBufferConverted.data());
206     size_t count = size / sizeof(int32_t);
207 
208     double maxInt32 = INT32_MAX;
209     double maxInt16 = INT16_MAX;
210     for (size_t idx = 0; idx < count; idx++) {
211         int16_t temp = static_cast<int16_t>((static_cast<double>(*stcPtr) / maxInt32) * maxInt16);
212         *(dstPtr++) = temp;
213         stcPtr++;
214     }
215     return 0;
216 }
217 
218 } // namespace AudioStandard
219 } // namespace OHOS
220