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 16 #ifndef FFMPEG_AAC_ENCODER_PLUGIN_H 17 #define FFMPEG_AAC_ENCODER_PLUGIN_H 18 19 #include <functional> 20 #include <mutex> 21 #include <vector> 22 #include <memory> 23 #include "plugin/codec_plugin.h" 24 #include "plugin/plugin_definition.h" 25 #include "ffmpeg_utils.h" 26 #include "ffmpeg_convert.h" 27 #include "ffmpeg_converter.h" 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 #include "libavcodec/avcodec.h" 33 #include "libavutil/audio_fifo.h" 34 #ifdef __cplusplus 35 }; 36 #endif 37 38 namespace OHOS { 39 namespace Media { 40 namespace Plugins { 41 namespace Ffmpeg { 42 class FFmpegAACEncoderPlugin : public CodecPlugin { 43 public: 44 explicit FFmpegAACEncoderPlugin(const std::string& name); 45 46 ~FFmpegAACEncoderPlugin(); 47 48 Status Init() override; 49 50 Status Prepare() override; 51 52 Status Reset() override; 53 54 Status Start() override; 55 56 Status Stop() override; 57 58 Status SetParameter(const std::shared_ptr<Meta> &meta) override; 59 60 Status GetParameter(std::shared_ptr<Meta> &meta) override; 61 62 Status QueueInputBuffer(const std::shared_ptr<AVBuffer> &inputBuffer) override; 63 64 Status QueueOutputBuffer(std::shared_ptr<AVBuffer> &outputBuffer) override; 65 66 Status GetInputBuffers(std::vector<std::shared_ptr<AVBuffer>> &inputBuffers) override; 67 68 Status GetOutputBuffers(std::vector<std::shared_ptr<AVBuffer>> &outputBuffers) override; 69 70 Status Flush() override; 71 72 Status Release() override; 73 SetDataCallback(DataCallback * dataCallback)74 Status SetDataCallback(DataCallback *dataCallback) override 75 { 76 dataCallback_ = dataCallback; 77 return Status::OK; 78 } 79 80 private: 81 Status AllocateContext(const std::string &name); 82 Status OpenContext(); 83 84 std::shared_ptr<AVCodecContext> GetCodecContext() const; 85 Status CloseCtxLocked(); 86 int32_t GetMaxInputSize() const noexcept; 87 Status PcmFillFrame(const std::shared_ptr<AVBuffer> &inputBuffer); 88 Status PushInFifo(const std::shared_ptr<AVBuffer> &inputBuffer); 89 Status ReceiveBuffer(std::shared_ptr<AVBuffer> &outBuffer); 90 Status ReceivePacketSucc(std::shared_ptr<AVBuffer> &outBuffer); 91 Status SendOutputBuffer(std::shared_ptr<AVBuffer> &outputBuffer); 92 Status GetAdtsHeader(std::string &adtsHeader, int32_t &headerSize, std::shared_ptr<AVCodecContext> ctx, 93 int aacLength); 94 Status InitFrame(); 95 Status InitContext(); 96 Status ReAllocateContext(); 97 bool CheckSampleRate(const int sampleRate); 98 bool CheckResample() const; 99 bool CheckSampleFormat(); 100 bool CheckBitRate() const; 101 bool CheckFormat(); 102 bool CheckChannelLayout(); 103 bool AudioSampleFormat2AVSampleFormat(const AudioSampleFormat &audioFmt, AVSampleFormat &fmt); 104 Status SendEncoder(const std::shared_ptr<AVBuffer> &inputBuffer); 105 Status GetMetaData(const std::shared_ptr<Meta> &meta); 106 Status SendFrameToFfmpeg(); 107 108 mutable std::mutex parameterMutex_{}; 109 Meta audioParameter_; 110 bool needResample_; 111 bool codecContextValid_; 112 mutable std::mutex avMutex_{}; 113 std::shared_ptr<const AVCodec> avCodec_{}; 114 std::shared_ptr<AVCodecContext> avCodecContext_{}; 115 AVAudioFifo *fifo_; 116 std::shared_ptr<AVFrame> cachedFrame_{}; 117 std::shared_ptr<AVPacket> avPacket_{}; 118 119 std::vector<uint8_t> paddedBuffer_{}; 120 size_t paddedBufferSize_{0}; 121 std::shared_ptr<AVBuffer> outBuffer_{nullptr}; 122 DataCallback *dataCallback_{nullptr}; 123 int64_t preBufferGroupPts_{0}; 124 int64_t curBufferGroupPts_{0}; 125 int32_t bufferNum_{1}; 126 int32_t bufferIndex_{1}; 127 int64_t bufferGroupPtsDistance{0}; 128 int64_t prevPts_; 129 bool needReformat_{false}; 130 mutable std::mutex bufferMetaMutex_{}; 131 std::shared_ptr<Meta> bufferMeta_{nullptr}; 132 std::shared_ptr<Ffmpeg::Resample> resample_{nullptr}; 133 AVSampleFormat srcFmt_{AVSampleFormat::AV_SAMPLE_FMT_NONE}; 134 AudioSampleFormat audioSampleFormat_; 135 AudioChannelLayout srcLayout_; 136 uint32_t fullInputFrameSize_{0}; 137 uint32_t srcBytesPerSample_{0}; 138 139 std::string aacName_; 140 int32_t channels_; 141 int32_t sampleRate_; 142 int64_t bitRate_; 143 int32_t maxInputSize_; 144 int32_t maxOutputSize_; 145 FILE *outfile; 146 }; 147 } // namespace Ffmpeg 148 } // namespace Plugins 149 } // namespace Media 150 } // namespace OHOS 151 152 #endif // FFMPEG_AAC_ENCODER_PLUGIN_H 153