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