1 /*
2  * Copyright (C) 2023 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 AV_CODEC_AUDIO_RESAMPLE_H
17 #define AV_CODEC_AUDIO_RESAMPLE_H
18 
19 #include <vector>
20 #include <memory>
21 #include "avcodec_errors.h"
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 #include "libswresample/swresample.h"
26 #ifdef __cplusplus
27 };
28 #endif
29 
30 namespace OHOS {
31 namespace MediaAVCodec {
32 struct ResamplePara {
33     uint32_t channels {2}; // 2: STEREO
34     int32_t sampleRate {0};
35     int32_t bitsPerSample {0};
36     AVChannelLayout channelLayout;
37     AVSampleFormat srcFmt {AV_SAMPLE_FMT_NONE};
38     int32_t destSamplesPerFrame {0};
39     AVSampleFormat destFmt {AV_SAMPLE_FMT_S16};
40 };
41 
42 class AudioResample {
43 public:
44     int32_t Init(const ResamplePara& resamplePara);
45     int32_t Convert(const uint8_t* srcBuffer, const size_t srcLength, uint8_t*& destBuffer, size_t& destLength);
46     int32_t InitSwrContext(const ResamplePara& resamplePara);
47     int32_t ConvertFrame(AVFrame *outputFrame, const AVFrame *inputFrame);
48 
49 private:
50     ResamplePara resamplePara_ {};
51     std::vector<uint8_t> resampleCache_ {};
52     std::vector<uint8_t*> resampleChannelAddr_ {};
53     std::shared_ptr<SwrContext> swrCtx_ {nullptr};
54 };
55 } // namespace MediaAVCodec
56 } // namespace OHOS
57 #endif
58