1 /*
2  * Copyright (c) 2023-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 #ifndef HISTREAMER_PLUGIN_CONVERT_H
16 #define HISTREAMER_PLUGIN_CONVERT_H
17 #undef memcpy_s
18 #include <memory>
19 #include <vector>
20 #include "plugin/common/plugin_types.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 #include "libavcodec/avcodec.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/error.h"
28 #include "libavutil/frame.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/pixfmt.h"
32 #include "libswresample/swresample.h"
33 #include "libswscale/swscale.h"
34 #ifdef __cplusplus
35 };
36 #endif
37 
38 namespace OHOS {
39 namespace Media {
40 namespace Plugin {
41 namespace Ffmpeg {
42 struct ResamplePara {
43     uint32_t channels {2}; // 2: STEREO
44     uint32_t sampleRate {0};
45     uint32_t bitsPerSample {0};
46     int64_t channelLayout {0};
47     AVSampleFormat srcFfFmt {AV_SAMPLE_FMT_NONE};
48     uint32_t destSamplesPerFrame {0};
49     AVSampleFormat destFmt {AV_SAMPLE_FMT_S16};
50 };
51 
52 class Resample {
53 public:
54     Status Init(const ResamplePara& resamplePara);
55     Status Convert(const uint8_t* srcBuffer, const size_t srcLength, uint8_t*& destBuffer, size_t& destLength);
56 private:
57     ResamplePara resamplePara_ {};
58 #if defined(_WIN32) || !defined(OHOS_LITE)
59     std::vector<uint8_t> resampleCache_ {};
60     std::vector<uint8_t*> resampleChannelAddr_ {};
61     std::shared_ptr<SwrContext> swrCtx_ {nullptr};
62 #endif
63 };
64 
65 #if defined(VIDEO_SUPPORT)
66 struct ScalePara {
67     int32_t srcWidth {0};
68     int32_t srcHeight {0};
69     AVPixelFormat srcFfFmt {AVPixelFormat::AV_PIX_FMT_NONE};
70     int32_t dstWidth {0};
71     int32_t dstHeight {0};
72     AVPixelFormat dstFfFmt {AVPixelFormat::AV_PIX_FMT_RGBA};
73     int32_t align {16};
74 };
75 
76 struct Scale {
77 public:
78     Status Init(const ScalePara& scalePara, uint8_t** dstData, int32_t* dstLineSize);
79     Status Convert(uint8_t** srcData, const int32_t* srcLineSize, uint8_t** dstData, int32_t* dstLineSize);
80 private:
81     ScalePara scalePara_ {};
82     std::shared_ptr<SwsContext> swsCtx_ {nullptr};
83 };
84 #endif
85 } // namespace Ffmpeg
86 } // namespace Plugin
87 } // namespace Media
88 } // namespace OHOS
89 #endif // HISTREAMER_PLUGIN_CONVERT_H
90