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 #ifndef MEDIA_AUDIO_ENCODER_H
17 #define MEDIA_AUDIO_ENCODER_H
18 
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include "ffmpeg_api_wrap.h"
23 
24 namespace OHOS {
25 namespace Media {
26 namespace MediaMonitor {
27 
28 enum SampleFormat : uint8_t {
29     U8 = 0,
30     S16LE = 1,
31     S24LE = 2,
32     S32LE = 3,
33     F32LE = 4,
34 };
35 
36 using AudioEncodeConfig = struct AudioEncodeConfig {
37     int64_t bitRate {0};
38     uint32_t sampleRate {0};
39     uint32_t channels {1};
40     SampleFormat sampleFmt {S16LE};
41     AVCodecID audioCodecId {AV_CODEC_ID_NONE};
42 };
43 
44 using VideoEncodeConfig = struct VideoEncodeConfig {
45     int64_t bitRate {0};
46     AVCodecID videoCodecId {AV_CODEC_ID_NONE};
47 };
48 
49 using ResamplePara = struct ResamplePara {
50     uint32_t channels {1};
51     uint32_t sampleRate {0};
52     uint64_t channelLayout {0};
53     AVSampleFormat srcFfFmt {AV_SAMPLE_FMT_NONE};
54     AVSampleFormat destFmt {AV_SAMPLE_FMT_S16};
55 };
56 
57 class SampleConvert {
58 public:
59     SampleConvert(std::shared_ptr<FFmpegApiWrap> apiWrap);
60     ~SampleConvert();
61     int32_t Init(const ResamplePara &param);
62     int32_t Convert(const uint8_t *src, size_t size, AVFrame *frame);
63     void Release();
64 private:
65     bool isInit_ = false;
66     ResamplePara resamplePara_;
67     std::shared_ptr<SwrContext> swrCtx_ = nullptr;
68     std::shared_ptr<FFmpegApiWrap> apiWrap_ = nullptr;
69 };
70 
71 class MediaAudioEncoder {
72 public:
MediaAudioEncoder()73     MediaAudioEncoder() {};
~MediaAudioEncoder()74     ~MediaAudioEncoder() {};
75     size_t PcmDataSize();
76     int32_t EncodePcmFiles(const std::string &fileDir);
77 private:
78     int32_t EncodePcmToFlac(const std::string &in);
79     int32_t Init(const std::string &inputFile);
80     int32_t WritePcm(const uint8_t *buffer, size_t size);
81     void Release();
82     bool IsSupportAudioArgs(std::string &audioArg, const std::vector<std::string> &supportList);
83     int32_t ParseAudioArgs(const std::string &fileName, AudioEncodeConfig &config);
84     int32_t GetAudioConfig(const std::string &inFullName, AudioEncodeConfig &config);
85     int32_t InitMux();
86     int32_t InitAudioEncode(const AudioEncodeConfig &audioConfig);
87     int32_t InitFrame();
88     int32_t InitPacket();
89     int32_t InitSampleConvert();
90     int32_t FillFrameFromBuffer(const uint8_t *buffer, size_t size);
91     int32_t WriteFrame();
92     void CopyS24ToS32(int32_t *dst, const uint8_t *src, size_t count);
93     void ResetEncoderCtx();
94     bool DeleteSrcFile(const std::string &filePath);
95     bool isInit_ = false;
96     std::string fileName_;
97     SampleFormat srcSampleFormat_ = SampleFormat::S16LE;
98     std::shared_ptr<AVFormatContext> formatContext_ = nullptr;
99     std::shared_ptr<AVCodecContext> audioCodecContext_ = nullptr;
100     std::shared_ptr<AVCodecContext> videoCodecContext_ = nullptr;
101     std::shared_ptr<AVPacket> avPacket_ = nullptr;
102     std::shared_ptr<AVFrame> avFrame_ = nullptr;
103     std::shared_ptr<SampleConvert> sampleConvert_ = nullptr;
104     std::shared_ptr<FFmpegApiWrap> apiWrap_ = nullptr;
105 };
106 
107 } // namespace MediaMonitor
108 } // namespace Media
109 } // namespace OHOS
110 #endif // MEDIA_AUDIO_ENCODER_H