• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 private:
73     bool hasExtra_;
74     int32_t maxInputSize_;
75     int32_t bufferNum_;
76     int32_t bufferIndex_;
77     int64_t preBufferGroupPts_;
78     int64_t curBufferGroupPts_;
79     int64_t bufferGroupPtsDistance;
80     std::string name_;
81 
82     std::shared_ptr<AVCodec> avCodec_;
83     std::shared_ptr<AVCodecContext> avCodecContext_;
84     std::shared_ptr<AVFrame> cachedFrame_;
85     std::shared_ptr<AVPacket> avPacket_;
86     std::mutex avMutext_;
87     std::mutex parameterMutex_;
88     Format format_;
89     std::vector<uint8_t> frameBuffer_;
90 
91     std::shared_ptr<AudioResample> resample_;
92     bool needResample_;
93     AVSampleFormat destFmt_;
94     std::shared_ptr<AVFrame> convertedFrame_;
95 
96 private:
97     int32_t SendBuffer(const std::shared_ptr<AudioBufferInfo> &inputBuffer);
98     int32_t ReceiveBuffer(std::shared_ptr<AudioBufferInfo> &outBuffer);
99     int32_t ReceiveFrameSucc(std::shared_ptr<AudioBufferInfo> &outBuffer);
100     int32_t InitResample();
101     int32_t ConvertPlanarFrame(std::shared_ptr<AudioBufferInfo> &outBuffer);
102 };
103 } // namespace MediaAVCodec
104 } // namespace OHOS
105 
106 #endif