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 VIDEOENC_SAMPLE_H 17 #define VIDEOENC_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_avmemory.h" 33 #include "native_avformat.h" 34 #include "native_averrors.h" 35 #include "media_description.h" 36 #include "av_common.h" 37 38 namespace OHOS { 39 namespace Media { 40 class VEncSignal { 41 public: 42 std::mutex inMutex_; 43 std::mutex outMutex_; 44 std::condition_variable inCond_; 45 std::condition_variable outCond_; 46 std::queue<uint32_t> inIdxQueue_; 47 std::queue<uint32_t> outIdxQueue_; 48 std::queue<OH_AVCodecBufferAttr> attrQueue_; 49 std::queue<OH_AVMemory *> inBufferQueue_; 50 std::queue<OH_AVMemory *> outBufferQueue_; 51 }; 52 53 class VEncFuzzSample : public NoCopyable { 54 public: 55 VEncFuzzSample() = default; 56 ~VEncFuzzSample(); 57 const char *inpDir = "/data/test/media/1280_720_nv.yuv"; 58 const char *outDir = "/data/test/media/VEncTest.h264"; 59 uint32_t defaultWidth = 1280; 60 uint32_t defaultHeight = 720; 61 uint32_t defaultBitrate = 5000000; 62 uint32_t defaultQuality = 30; 63 double defaultFrameRate = 30.0; 64 uint32_t defaultFuzzTime = 30; 65 uint32_t defaultBitrateMode = CBR; 66 OH_AVPixelFormat defaultPixFmt = AV_PIXEL_FORMAT_NV12; 67 uint32_t defaultKeyFrameInterval = 1000; 68 int32_t CreateVideoEncoder(const char *codecName); 69 int32_t ConfigureVideoEncoder(); 70 int32_t ConfigureVideoEncoderFuzz(int32_t data); 71 int32_t SetVideoEncoderCallback(); 72 int32_t StartVideoEncoder(); 73 int32_t SetParameter(OH_AVFormat *format); 74 void SetForceIDR(); 75 void GetStride(); 76 void WaitForEOS(); 77 int32_t OpenFile(); 78 uint32_t ReturnZeroIfEOS(uint32_t expectedSize); 79 int64_t GetSystemTimeUs(); 80 int32_t Start(); 81 int32_t Flush(); 82 int32_t Reset(); 83 int32_t Stop(); 84 int32_t Release(); 85 bool RandomEOS(uint32_t index); 86 void SetEOS(uint32_t index); 87 int32_t PushData(OH_AVMemory *buffer, uint32_t index, int32_t &result); 88 void InputDataFuzz(bool &runningFlag, uint32_t index); 89 int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult); 90 void InputFunc(); 91 uint32_t ReadOneFrameYUV420SP(uint8_t *dst); 92 void ReadOneFrameRGBA8888(uint8_t *dst); 93 int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr); 94 void OutputFuncFail(); 95 void OutputFunc(); 96 void ReleaseSignal(); 97 void ReleaseInFile(); 98 void StopInloop(); 99 void StopOutloop(); 100 101 VEncSignal *signal_; 102 uint32_t errCount = 0; 103 bool enableForceIDR = false; 104 uint32_t outCount = 0; 105 uint32_t frameCount = 0; 106 uint32_t switchParamsTimeSec = 3; 107 bool sleepOnFPS = false; 108 bool enableAutoSwitchParam = false; 109 bool needResetBitrate = false; 110 bool needResetFrameRate = false; 111 bool needResetQP = false; 112 bool repeatRun = false; 113 bool showLog = false; 114 bool fuzzMode = false; 115 116 private: 117 std::atomic<bool> isRunning_ { false }; 118 std::unique_ptr<std::ifstream> inFile_; 119 std::unique_ptr<std::thread> inputLoop_; 120 std::unique_ptr<std::thread> outputLoop_; 121 std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_; 122 std::unordered_map<uint32_t, OH_AVMemory *> outBufferMap_; 123 OH_AVCodec *venc_; 124 OH_AVCodecAsyncCallback cb_; 125 int64_t timeStamp_ { 0 }; 126 int64_t lastRenderedTimeUs_ { 0 }; 127 bool isFirstFrame_ = true; 128 OHNativeWindow *nativeWindow; 129 int stride_; 130 static constexpr uint32_t sampleRatio = 2; 131 }; 132 } // namespace Media 133 } // namespace OHOS 134 135 #endif // VIDEODEC_SAMPLE_H 136