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 VIDEODEC_SAMPLE_H 17 #define VIDEODEC_SAMPLE_H 18 19 #include <atomic> 20 #include <cstdio> 21 #include <fstream> 22 #include <iostream> 23 #include <mutex> 24 #include <queue> 25 #include <string> 26 #include <thread> 27 #include <unistd.h> 28 #include <unordered_map> 29 #include "native_avcodec_videodecoder.h" 30 #include "native_averrors.h" 31 #include "native_avformat.h" 32 #include "nocopyable.h" 33 #include "securec.h" 34 #include "window.h" 35 #include "iconsumer_surface.h" 36 namespace OHOS { 37 namespace Media { 38 class VDecSignal { 39 public: 40 std::mutex inMutex_; 41 std::mutex outMutex_; 42 std::condition_variable inCond_; 43 std::condition_variable outCond_; 44 std::queue<uint32_t> inIdxQueue_; 45 std::queue<uint32_t> outIdxQueue_; 46 std::queue<OH_AVCodecBufferAttr> attrQueue_; 47 std::queue<OH_AVMemory *> inBufferQueue_; 48 std::queue<OH_AVMemory *> outBufferQueue_; 49 }; 50 51 class VDecFuzzSample : public NoCopyable { 52 public: 53 VDecFuzzSample() = default; 54 ~VDecFuzzSample(); 55 int32_t RunVideoDec(); 56 const char *inpDir = "/data/test/media/1920_1080_10_30Mb.h264"; 57 const char *outDir = "/data/test/media/VDecTest.yuv"; 58 uint32_t defaultWidth = 1920; 59 uint32_t defaultHeight = 1080; 60 uint32_t defaultFrameRate = 30; 61 uint32_t defaultRotation = 0; 62 uint32_t defaultPixelFormat = AV_PIXEL_FORMAT_NV12; 63 uint32_t frameCount_ = 0; 64 int32_t Start(); 65 int32_t Stop(); 66 int32_t Flush(); 67 int32_t Reset(); 68 void SetEOS(uint32_t index); 69 uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVMemory *buffer); 70 void CopyStartCode(uint8_t *frameBuffer, uint32_t bufferSize, OH_AVCodecBufferAttr &attr); 71 int32_t ReadData(uint32_t index, OH_AVMemory *buffer); 72 void WaitForEOS(); 73 int32_t ConfigureVideoDecoder(); 74 int32_t StartVideoDecoder(); 75 int64_t GetSystemTimeUs(); 76 int32_t CreateVideoDecoder(); 77 int32_t SetVideoDecoderCallback(); 78 int32_t Release(); 79 void SetParameter(int32_t data); 80 void InputFuncAVCC(); 81 OH_AVErrCode InputFuncFUZZ(const uint8_t *data, size_t size); 82 void ReleaseSignal(); 83 void ReleaseInFile(); 84 void StopInLoop(); 85 VDecSignal *signal_; 86 uint32_t errCount = 0; 87 bool isSurfMode = false; 88 OH_AVCodec *vdec_; 89 OHNativeWindow *nativeWindow = nullptr; 90 sptr<Surface> cs = nullptr; 91 sptr<Surface> ps = nullptr; 92 std::atomic<bool> isRunning_ { false }; 93 private: 94 std::unique_ptr<std::ifstream> inFile_; 95 std::unique_ptr<std::thread> inputLoop_; 96 std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_; 97 OH_AVCodecAsyncCallback cb_; 98 int64_t timeStamp_ { 0 }; 99 int64_t lastRenderedTimeUs_ { 0 }; 100 bool isFirstFrame_ = true; 101 }; 102 } // namespace Media 103 } // namespace OHOS 104 105 void VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData); 106 void VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData); 107 void VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData); 108 void VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr, 109 void *userData); 110 #endif // VIDEODEC_SAMPLE_H 111