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_DECODER_PLUGIN 17 #define AUDIO_FFMPEG_DECODER_PLUGIN 18 19 #include <mutex> 20 #include "audio_base_codec.h" 21 #include "nocopyable.h" 22 #include "audio_resample.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 #include <libavcodec/avcodec.h> 28 #ifdef __cplusplus 29 }; 30 #endif 31 32 namespace OHOS { 33 namespace MediaAVCodec { 34 class AudioFfmpegDecoderPlugin : public NoCopyable { 35 public: 36 AudioFfmpegDecoderPlugin(); 37 38 ~AudioFfmpegDecoderPlugin(); 39 40 int32_t ProcessSendData(const std::shared_ptr<AudioBufferInfo> &inputBuffer); 41 42 int32_t ProcessRecieveData(std::shared_ptr<AudioBufferInfo> &outBuffer); 43 44 int32_t Reset(); 45 46 int32_t Release(); 47 48 int32_t Flush(); 49 50 int32_t AllocateContext(const std::string &name); 51 52 int32_t InitContext(const Format &format); 53 54 int32_t OpenContext(); 55 56 Format GetFormat() const noexcept; 57 58 std::shared_ptr<AVCodecContext> GetCodecContext() const noexcept; 59 60 std::shared_ptr<AVPacket> GetCodecAVPacket() const noexcept; 61 62 std::shared_ptr<AVFrame> GetCodecCacheFrame() const noexcept; 63 64 int32_t CloseCtxLocked(); 65 66 int32_t GetMaxInputSize() const noexcept; 67 68 bool HasExtraData() const noexcept; 69 70 void EnableResample(AVSampleFormat destFmt); 71 72 int32_t SetCodecExtradata(); 73 74 private: 75 bool hasExtra_; 76 int32_t maxInputSize_; 77 int32_t bufferNum_; 78 int32_t bufferIndex_; 79 int64_t preBufferGroupPts_; 80 int64_t curBufferGroupPts_; 81 int64_t bufferGroupPtsDistance; 82 std::string name_; 83 84 std::shared_ptr<AVCodec> avCodec_; 85 std::shared_ptr<AVCodecContext> avCodecContext_; 86 std::shared_ptr<AVFrame> cachedFrame_; 87 std::shared_ptr<AVPacket> avPacket_; 88 std::mutex avMutext_; 89 std::mutex parameterMutex_; 90 Format format_; 91 std::vector<uint8_t> frameBuffer_; 92 93 std::shared_ptr<AudioResample> resample_; 94 bool needResample_; 95 AVSampleFormat destFmt_; 96 std::shared_ptr<AVFrame> convertedFrame_; 97 98 private: 99 int32_t SendBuffer(const std::shared_ptr<AudioBufferInfo> &inputBuffer); 100 int32_t ReceiveBuffer(std::shared_ptr<AudioBufferInfo> &outBuffer); 101 int32_t ReceiveFrameSucc(std::shared_ptr<AudioBufferInfo> &outBuffer); 102 int32_t InitResample(); 103 int32_t ConvertPlanarFrame(std::shared_ptr<AudioBufferInfo> &outBuffer); 104 }; 105 } // namespace MediaAVCodec 106 } // namespace OHOS 107 108 #endif