1 /* 2 * Copyright (C) 2024 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 "surface/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.yuv"; 66 int32_t CreateVideoEncoder(); 67 int32_t ConfigureVideoEncoderFuzz(int32_t data); 68 int32_t ConfigureVideoEncoder(); 69 int32_t SetVideoEncoderCallback(); 70 int32_t CreateSurface(); 71 int32_t StartVideoEncoder(); 72 int32_t SetParameter(int32_t data); 73 void GetStride(); 74 void WaitForEOS(); 75 int32_t OpenFile(); 76 uint32_t ReturnZeroIfEOS(uint32_t expectedSize); 77 int64_t GetSystemTimeUs(); 78 int32_t Start(); 79 int32_t Flush(); 80 int32_t Reset(); 81 int32_t Stop(); 82 int32_t Release(); 83 void SetEOS(uint32_t index, OH_AVBuffer *buffer); 84 void InputFunc(); 85 void InputFuncSurface(); 86 uint32_t ReadOneFrameYUV420SP(uint8_t *dst); 87 void ReleaseInFile(); 88 int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr); 89 uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer); 90 void ReleaseSignal(); 91 int32_t PushData(OH_AVBuffer *buffer, uint32_t index, int32_t &result); 92 void StopInloop(); 93 VEncSignal *signal_; 94 uint32_t errCount = 0; 95 uint32_t frameCount = 0; 96 bool sleepOnFPS = false; 97 bool surfInput = false; 98 private: 99 std::atomic<bool> isRunning_ { false }; 100 std::unique_ptr<std::thread> inputLoop_; 101 std::unique_ptr<std::ifstream> inFile_; 102 std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_; 103 OH_AVCodec *venc_; 104 OH_AVCodecCallback cb_; 105 int64_t timeStamp_ { 0 }; 106 int64_t lastRenderedTimeUs_ { 0 }; 107 bool isFirstFrame_ = true; 108 OHNativeWindow *nativeWindow; 109 int stride_; 110 }; 111 } // namespace Media 112 } // namespace OHOS 113 114 #endif // VIDEOENC_API11_SAMPLE_H 115