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 VIDEOENC_API11_SAMPLE_H 17 #define VIDEOENC_API11_SAMPLE_H 18 19 #include <iostream> 20 #include <cstdio> 21 #include <unistd.h> 22 #include <atomic> 23 #include <fstream> 24 #include <thread> 25 #include <mutex> 26 #include <queue> 27 #include <string> 28 #include <unordered_map> 29 #include "securec.h" 30 #include "native_avcodec_videoencoder.h" 31 #include "nocopyable.h" 32 #include "native_avbuffer.h" 33 #include "native_avformat.h" 34 #include "native_averrors.h" 35 #include "window.h" 36 #include "media_description.h" 37 #include "av_common.h" 38 #include "external_window.h" 39 #include "native_buffer_inner.h" 40 namespace OHOS { 41 namespace Media { 42 class VEncSignal { 43 public: 44 std::mutex inMutex_; 45 std::condition_variable inCond_; 46 std::queue<uint32_t> inIdxQueue_; 47 std::queue<OH_AVBuffer *> inBufferQueue_; 48 }; 49 50 class VEncAPI11FuzzSample : public NoCopyable { 51 public: 52 VEncAPI11FuzzSample() = default; 53 ~VEncAPI11FuzzSample(); 54 uint32_t defaultWidth = 1280; 55 uint32_t defaultHeight = 720; 56 uint32_t defaultBitRate = 5000000; 57 uint32_t defaultQuality = 30; 58 double defaultFrameRate = 30.0; 59 uint32_t maxFrameInput = 20; 60 int32_t defaultQP = 20; 61 bool fuzzMode = true; 62 uint32_t defaultBitrateMode = CBR; 63 OH_AVPixelFormat defaultPixFmt = AV_PIXEL_FORMAT_NV12; 64 uint32_t defaultKeyFrameInterval = 1000; 65 const char *inpDir = "/data/test/media/1280_720_nv_1.yuv"; 66 const char *roiInfo = "0,0-128,64=-4;100,200-300,400=3"; 67 const uint8_t *fuzzData; 68 size_t fuzzSize; 69 int32_t CreateVideoEncoder(const char *codecName); 70 int32_t ConfigureVideoEncoderFuzz(int32_t data); 71 int32_t ConfigureVideoEncoder(); 72 int32_t SetVideoEncoderCallback(); 73 int32_t CreateSurface(); 74 int32_t StartVideoEncoder(); 75 int32_t SetParameter(); 76 void GetStride(); 77 void WaitForEOS(); 78 int32_t OpenFile(); 79 uint32_t ReturnZeroIfEOS(uint32_t expectedSize); 80 int64_t GetSystemTimeUs(); 81 int32_t Start(); 82 int32_t Flush(); 83 int32_t Reset(); 84 int32_t Stop(); 85 int32_t Release(); 86 void SetEOS(uint32_t index, OH_AVBuffer *buffer); 87 void InputFunc(); 88 void InputFuncSurface(); 89 uint32_t ReadOneFrameYUV420SP(uint8_t *dst); 90 void ReleaseInFile(); 91 int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr); 92 uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer); 93 void ReleaseSignal(); 94 int32_t PushData(OH_AVBuffer *buffer, uint32_t index, int32_t &result); 95 void StopInloop(); 96 VEncSignal *signal_; 97 uint32_t errCount = 0; 98 uint32_t frameCount = 0; 99 bool sleepOnFPS = false; 100 bool surfInput = false; 101 uint32_t defaultRangeFlag = 0; 102 uint32_t defaultColorPrimaries = COLOR_PRIMARY_BT709; 103 uint32_t defaultTransferCharacteristics = TRANSFER_CHARACTERISTIC_BT709; 104 uint32_t defaultMatarixCoefficients = MATRIX_COEFFICIENT_BT709; 105 bool enableRepeat = false; 106 bool setMaxCount = false; 107 bool enablePTSBasedRateControl = false; 108 bool bEnable = false; 109 bool factorEnable = false; 110 bool maxbiteEnable = false; 111 int32_t defaultFrameAfter = 1; 112 int32_t defaultMaxCount = 1; 113 int32_t defaultBframe = 1; 114 int32_t defaultSqrFactor = 28; 115 int64_t defaultMaxBitrate = 5000000; 116 std::atomic<bool> isRunning_ { false }; 117 int32_t frameIndex_ = 0; 118 private: 119 std::unique_ptr<std::thread> inputLoop_; 120 std::unique_ptr<std::ifstream> inFile_; 121 std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_; 122 OH_AVCodec *venc_; 123 OH_AVCodecCallback cb_; 124 int64_t timeStamp_ { 0 }; 125 int64_t lastRenderedTimeUs_ { 0 }; 126 bool isFirstFrame_ = true; 127 OHNativeWindow *nativeWindow; 128 int stride_; 129 }; 130 } // namespace Media 131 } // namespace OHOS 132 133 #endif // VIDEOENC_API11_SAMPLE_H 134