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 AUDIO_FFMPEG_AAC_ENCODER_PLUGIN_H 17 #define AUDIO_FFMPEG_AAC_ENCODER_PLUGIN_H 18 19 #include <mutex> 20 #include "audio_base_codec.h" 21 #include "avcodec_codec_name.h" 22 #include "audio_base_codec.h" 23 #include "nocopyable.h" 24 #include "audio_resample.h" 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 #include "libavcodec/avcodec.h" 29 #ifdef __cplusplus 30 }; 31 #endif 32 33 namespace OHOS { 34 namespace MediaAVCodec { 35 class AudioFFMpegAacEncoderPlugin : public AudioBaseCodec::CodecRegister<AudioFFMpegAacEncoderPlugin> { 36 public: 37 AudioFFMpegAacEncoderPlugin(); 38 ~AudioFFMpegAacEncoderPlugin() override; 39 40 int32_t Init(const Format &format) override; 41 int32_t ProcessSendData(const std::shared_ptr<AudioBufferInfo> &inputBuffer) override; 42 int32_t ProcessRecieveData(std::shared_ptr<AudioBufferInfo> &outBuffer) override; 43 int32_t Reset() override; 44 int32_t Release() override; 45 int32_t Flush() override; 46 int32_t GetInputBufferSize() const override; 47 int32_t GetOutputBufferSize() const override; 48 Format GetFormat() const noexcept override; 49 std::string_view GetCodecType() const noexcept override; 50 Identify()51 const static std::string Identify() 52 { 53 return std::string(AVCodecCodecName::AUDIO_ENCODER_AAC_NAME); 54 } 55 56 private: 57 Format format_; 58 int32_t maxInputSize_; 59 std::shared_ptr<AVCodec> avCodec_; 60 std::shared_ptr<AVCodecContext> avCodecContext_; 61 std::shared_ptr<AVFrame> cachedFrame_; 62 std::shared_ptr<AVPacket> avPacket_; 63 mutable std::mutex avMutext_; 64 int64_t prevPts_; 65 std::shared_ptr<AudioResample> resample_; 66 bool needResample_; 67 AVSampleFormat srcFmt_; 68 int64_t srcLayout_; 69 bool codecContextValid_; 70 71 private: 72 bool CheckFormat(const Format &format); 73 bool CheckBitRate(const Format &format) const; 74 void SetFormat(const Format &format) noexcept; 75 int32_t AllocateContext(const std::string &name); 76 int32_t InitContext(const Format &format); 77 int32_t OpenContext(); 78 int32_t InitFrame(); 79 int32_t SendBuffer(const std::shared_ptr<AudioBufferInfo> &inputBuffer); 80 int32_t ReceiveBuffer(std::shared_ptr<AudioBufferInfo> &outBuffer); 81 int32_t ReceivePacketSucc(std::shared_ptr<AudioBufferInfo> &outBuffer); 82 int32_t PcmFillFrame(const std::shared_ptr<AudioBufferInfo> &inputBuffer); 83 int32_t CloseCtxLocked(); 84 int32_t ReAllocateContext(); 85 bool CheckResample() const; 86 bool CheckSampleRate(const int sampleRate); 87 bool CheckSampleFormat(const Format &format); 88 bool CheckChannelLayout(const Format &format, int channels); 89 int32_t GetAdtsHeader(std::string &adtsHeader, int32_t &headerSize, std::shared_ptr<AVCodecContext> ctx, 90 int aacLength); 91 }; 92 } // namespace MediaAVCodec 93 } // namespace OHOS 94 #endif