• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 AVC_ENCODER_H
17 #define AVC_ENCODER_H
18 
19 #include <atomic>
20 #include <list>
21 #include <map>
22 #include <shared_mutex>
23 #include <functional>
24 #include <fstream>
25 #include <tuple>
26 #include <vector>
27 #include <optional>
28 #include <algorithm>
29 #include "av_common.h"
30 #include "avcodec_common.h"
31 #include "avcodec_info.h"
32 #include "block_queue.h"
33 #include "codec_utils.h"
34 #include "codecbase.h"
35 #include "media_description.h"
36 #include "fsurface_memory.h"
37 #include "task_thread.h"
38 #include "AvcEnc_Typedef.h"
39 
40 namespace OHOS {
41 namespace MediaAVCodec {
42 namespace Codec {
43 
44 using CreateAvcEncoderFuncType = uint32_t (*)(AVC_ENC_HANDLE *phEncoder, AVC_ENC_INIT_PARAM *pstInitParam);
45 using EncodeFuncType = uint32_t (*)(AVC_ENC_HANDLE hEncoder, AVC_ENC_INARGS *pstInArgs, AVC_ENC_OUTARGS *pstOutArgs);
46 using DeleteFuncType = uint32_t (*)(AVC_ENC_HANDLE hEncoder);
47 
48 class AvcEncoder : public CodecBase, public RefBase {
49 public:
50     explicit AvcEncoder(const std::string &name);
51     ~AvcEncoder() override;
52     int32_t Configure(const Format &format) override;
53     int32_t Start() override;
54     int32_t Stop() override;
55     int32_t Flush() override;
56     int32_t Reset() override;
57     int32_t Release() override;
58     int32_t SetParameter(const Format &format) override;
59     int32_t GetInputFormat(Format &format) override;
60     int32_t GetOutputFormat(Format &format) override;
61     int32_t QueueInputBuffer(uint32_t index) override;
62     int32_t ReleaseOutputBuffer(uint32_t index) override;
63     int32_t NotifyEos() override;
64     int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) override;
65     sptr<Surface> CreateInputSurface() override;
66     int32_t SetInputSurface(sptr<Surface> surface) override;
67     int32_t SetOutputSurface(sptr<Surface> surface) override;
68     int32_t RenderOutputBuffer(uint32_t index) override;
69     int32_t SignalRequestIDRFrame() override;
70     void GetBufferFromSurface();
71     static int32_t GetCodecCapability(std::vector<CapabilityData> &capaArray);
72 
73     class FBuffer {
74     public:
75         FBuffer() = default;
76         ~FBuffer() = default;
77 
78         enum class Owner {
79             OWNED_BY_US,
80             OWNED_BY_CODEC,
81             OWNED_BY_USER,
82             OWNED_BY_SURFACE,
83         };
84 
85         std::shared_ptr<AVBuffer> avBuffer_ = nullptr;
86         sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
87         std::atomic<Owner> owner_ = Owner::OWNED_BY_US;
88         int32_t width_ = 0;
89         int32_t height_ = 0;
90     };
91 private:
92     static int32_t CheckAvcEncLibStatus();
93     static void GetCapabilityData(CapabilityData &capsData, uint32_t index);
94     int32_t Initialize();
95     int32_t ConfigureContext(const Format &format);
96     void ConfigureDefaultVal(const Format &format, const std::string_view &formatKey,
97         int32_t minVal = 0, int32_t maxVal = INT_MAX);
98     bool GetDiscardFlagFromAVBuffer(const std::shared_ptr<AVBuffer> &buffer);
99     bool GetPixelFmtFromUser(const Format &format);
100     void GetQpRangeFromUser(const Format &format);
101     void GetBitRateFromUser(const Format &format);
102     void GetFrameRateFromUser(const Format &format);
103     void GetBitRateModeFromUser(const Format &format);
104     void GetIFrameIntervalFromUser(const Format &format);
105     void GetRequestIDRFromUser(const Format &format);
106     void GetColorAspects(const Format &format);
107     void CheckIfEnableCb(const Format &format);
108     int32_t SetupPort(const Format &format);
109 
110     void CheckBitRateSupport(int32_t &bitrate);
111     void CheckFrameRateSupport(double &framerate);
112     void CheckIFrameIntervalTimeSupport(int32_t &interval);
113 
114     void AvcFuncMatch();
115     void ReleaseHandle();
116 
117     void ReleaseSurfaceBuffer();
118     void ClearDirtyList();
119     void WaitForInBuffer();
120     void FillEncodedBuffer(const std::shared_ptr<FBuffer> &frameBuffer);
121 
122     void NotifyUserToProcessBuffer(uint32_t index, std::shared_ptr<AVBuffer> &buffer);
123     void NotifyUserToFillBuffer(uint32_t index, std::shared_ptr<AVBuffer> &buffer);
124 
125     void InitAvcEncoderParams();
126     void FillAvcInitParams(AVC_ENC_INIT_PARAM &param);
127 
128     struct InputFrame {
129         uint8_t *buffer = nullptr;
130         int32_t width = 0;
131         int32_t height = 0;
132         int32_t stride = 0;
133         int32_t size = 0;
134         int32_t uvOffset = 0;
135         VideoPixelFormat format = VideoPixelFormat::UNKNOWN;
136         int64_t pts = 0;
137     };
138 
139     struct NVFrame {
140         uint8_t *srcY   = nullptr;
141         uint8_t *srcUV  = nullptr;
142         int32_t yStride = 0;
143         int32_t uvStide = 0;
144         int32_t width   = 0;
145         int32_t height  = 0;
146     };
147 
148     int32_t FillAvcEncoderInDefaultArgs(AVC_ENC_INARGS &inArgs);
149     int32_t FillAvcEncoderInArgs(std::shared_ptr<AVBuffer> &buffer, AVC_ENC_INARGS &inArgs);
150 
151     void FillNV21ToAvcEncInArgs(AVC_ENC_INARGS &inArgs, NVFrame &nvFrame, int64_t pts);
152     int32_t GetSurfaceBufferUvOffset(sptr<SurfaceBuffer> &surfaceBuffer, VideoPixelFormat format);
153     int32_t GetInputFrameFromAVBuffer(std::shared_ptr<AVBuffer> &buffer, InputFrame &inFrame);
154     int32_t Yuv420ToAvcEncoderInArgs(InputFrame &inFrame, AVC_ENC_INARGS &inArgs);
155     int32_t RgbaToAvcEncoderInArgs(InputFrame &inFrame, AVC_ENC_INARGS &inArgs);
156     int32_t Nv12ToAvcEncoderInArgs(InputFrame &inFrame, AVC_ENC_INARGS &inArgs);
157     int32_t Nv21ToAvcEncoderInArgs(InputFrame &inFrame, AVC_ENC_INARGS &inArgs);
158     int32_t CheckBufferSize(int32_t bufferSize, int32_t stride, int32_t height, VideoPixelFormat pixelFormat);
159 
160     void EncoderAvcHeader();
161     void EncoderAvcTailer();
162     int32_t EncoderAvcFrame(AVC_ENC_INARGS &inArgs, AVC_ENC_OUTARGS &outArgs);
163 
164     enum struct State : int32_t {
165         UNINITIALIZED,
166         INITIALIZED,
167         CONFIGURED,
168         STOPPING,
169         RUNNING,
170         FLUSHED,
171         FLUSHING,
172         EOS,
173         ERROR,
174     };
175     bool IsActive() const;
176     void CalculateBufferSize();
177     int32_t AllocateBuffers();
178     void InitBuffers();
179     void ResetBuffers();
180     void ResetData();
181     void ReleaseBuffers();
182     void StopThread();
183     void ReleaseResource();
184     void SendFrame();
185     int32_t AllocateInputBuffer(int32_t bufferCnt, int32_t inBufferSize);
186     int32_t AllocateOutputBuffer(int32_t bufferCnt, int32_t outBufferSize);
187 
188 private:
189     class EncoderBuffersConsumerListener : public IBufferConsumerListener {
190     public:
EncoderBuffersConsumerListener(AvcEncoder * codec)191         explicit EncoderBuffersConsumerListener(AvcEncoder *codec) : codec_(codec) {}
192         void OnBufferAvailable() override;
193     private:
194         AvcEncoder *codec_ = nullptr;
195     };
196 
197     AVC_ENC_HANDLE avcEncoder_ = nullptr;
198     CreateAvcEncoderFuncType avcEncoderCreateFunc_ = nullptr;
199     EncodeFuncType avcEncoderFrameFunc_ = nullptr;
200     DeleteFuncType avcEncoderDeleteFunc_ = nullptr;
201     AVC_ENC_INIT_PARAM initParams_;
202     AVC_ENC_INARGS avcEncInputArgs_;
203     AVC_ENC_OUTARGS avcEncOutputArgs_;
204 
205 private:
206     std::string codecName_;
207     std::atomic<State> state_ = State::UNINITIALIZED;
208     void* handle_ = nullptr;
209     uint32_t encInstanceID_ = 0;
210     static std::mutex encoderCountMutex_;
211     static std::vector<uint32_t> encInstanceIDSet_;
212     static std::vector<uint32_t> freeIDSet_;
213     static constexpr uint32_t SURFACE_MODE_CONSUMER_USAGE = BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_CPU_READ;
214     Format format_;
215     int32_t encWidth_;
216     int32_t encHeight_;
217     int32_t encBitrate_;
218     int32_t encQp_;
219     int32_t encQpMax_;
220     int32_t encQpMin_;
221     int32_t encIperiod_;
222     double encFrameRate_;
223     bool encIdrRequest_ = false;
224     bool enableSurfaceModeInputCb_ = false;
225     uint8_t srcRange_ = 0;
226     ColorPrimary srcPrimary_ = ColorPrimary::COLOR_PRIMARY_UNSPECIFIED;
227     TransferCharacteristic srcTransfer_ = TransferCharacteristic::TRANSFER_CHARACTERISTIC_UNSPECIFIED;
228     MatrixCoefficient srcMatrix_ = MatrixCoefficient::MATRIX_COEFFICIENT_UNSPECIFIED;
229     AVCProfile avcProfile_ = AVCProfile::AVC_PROFILE_BASELINE;
230     AVCLevel avcLevel_ = AVCLevel::AVC_LEVEL_3;
231     VideoEncodeBitrateMode bitrateMode_ = VideoEncodeBitrateMode::CQ;
232     int32_t avcQuality_ = 0;
233     int32_t inputBufferSize_ = 0;
234     int32_t outputBufferSize_ = 0;
235 
236     uint8_t *scaleData_[AV_NUM_DATA_POINTERS] = {nullptr};
237     int32_t scaleLineSize_[AV_NUM_DATA_POINTERS] = {0};
238     std::shared_ptr<Scale> scale_ = nullptr;
239     VideoPixelFormat srcPixelFmt_ = VideoPixelFormat::UNKNOWN;
240     VideoPixelFormat encodePixelFmt_ = VideoPixelFormat::NV21;
241     std::vector<std::shared_ptr<FBuffer>> buffers_[2];
242     std::shared_ptr<AVBuffer> convertBuffer_ = nullptr;
243     std::shared_ptr<BlockQueue<uint32_t>> inputAvailQue_;
244     std::shared_ptr<BlockQueue<uint32_t>> codecAvailQue_;
245     std::shared_ptr<TaskThread> sendTask_ = nullptr;
246     std::mutex inputMutex_;
247     std::mutex outputMutex_;
248     std::mutex sendMutex_;
249     std::mutex encRunMutex_;
250 
251     // 存放可用的 buffers 索引
252     std::mutex freeListMutex_;
253     std::list<uint32_t> freeList_;
254     std::condition_variable surfaceRecvCv_;
255 
256     std::condition_variable sendCv_;
257     sptr<Surface> inputSurface_ = nullptr;
258     std::shared_ptr<MediaCodecCallback> callback_;
259     std::atomic<bool> isSendWait_ = false;
260     std::atomic<bool> isSendEos_ = false;
261     std::atomic<bool> isBufferAllocated_ = false;
262     bool isFirstFrame_ = true;
263 };
264 } // namespace Codec
265 } // namespace MediaAVCodec
266 } // namespace OHOS
267 #endif // AVC_ENCODER_H