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 "native_avmemory.h" 33 #include "nocopyable.h" 34 #include "securec.h" 35 #include "window.h" 36 37 namespace OHOS { 38 namespace Media { 39 class VDecSignal { 40 public: 41 std::mutex inMutex_; 42 std::mutex outMutex_; 43 std::condition_variable inCond_; 44 std::condition_variable outCond_; 45 std::queue<uint32_t> inIdxQueue_; 46 std::queue<uint32_t> outIdxQueue_; 47 std::queue<OH_AVCodecBufferAttr> attrQueue_; 48 std::queue<OH_AVMemory *> inBufferQueue_; 49 std::queue<OH_AVMemory *> outBufferQueue_; 50 }; 51 52 class VDecNdkSample : public NoCopyable { 53 public: 54 VDecNdkSample() = default; 55 ~VDecNdkSample(); 56 int32_t RunVideoDec_Surface(std::string codeName = ""); 57 int32_t RunVideoDec(std::string codeName = ""); 58 const char *INP_DIR = "/data/test/media/main@high_level_1920_1080_60.m2v"; 59 const char *OUT_DIR = "/data/test/media/VDecTest.yuv"; 60 bool SURFACE_OUTPUT = false; 61 uint32_t DEFAULT_WIDTH = 1920; 62 uint32_t DEFAULT_HEIGHT = 1080; 63 uint32_t originalWidth = 0; 64 uint32_t originalHeight = 0; 65 int32_t DEFAULT_FRAME_RATE = 60; 66 uint32_t DEFAULT_ROTATION = 0; 67 int32_t maxInputSize = 0; 68 uint32_t DEFAULT_PIXEL_FORMAT = AV_PIXEL_FORMAT_NV12; 69 bool BEFORE_EOS_INPUT = false; // 0800 测试用例 70 bool BEFORE_EOS_INPUT_INPUT = false; // 0900 测试用例 71 bool AFTER_EOS_DESTORY_CODEC = true; // 1000 测试用例 结束不销毁codec 72 uint32_t REPEAT_START_STOP_BEFORE_EOS = 0; // 1200 测试用例 73 uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例 74 uint32_t frameCount_ = 0; 75 bool finishLastPush = false; 76 const char *fileSourcesha256[64] = {"27", "6D", "A2", "D4", "18", "21", "A5", "CD", "50", "F6", "DD", "CA", "46", 77 "32", "C3", "FE", "58", "FC", "BC", "51", "FD", "70", "C7", "D4", "E7", "4D", 78 "5C", "76", "E7", "71", "8A", "B3", "C0", "51", "84", "0A", "FA", "AF", "FA", 79 "DC", "7B", "C5", "26", "D1", "9A", "CA", "00", "DE", "FC", "C8", "4E", "34", 80 "C5", "9A", "43", "59", "85", "DC", "AC", "97", "A3", "FB", "23", "51"}; 81 void GetBufferSize(); 82 void PtrStep(uint32_t &bufferSize, unsigned char **pBuffer, uint32_t size); 83 void PtrStepExtraRead(uint32_t &bufferSize, unsigned char **pBuffer); 84 int32_t Start(); 85 int32_t Stop(); 86 int32_t Flush(); 87 void Flush_buffer(); 88 int32_t Reset(); 89 void SetEOS(uint32_t index); 90 uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVMemory *buffer); 91 int32_t ReadData(uint32_t index, OH_AVMemory *buffer); 92 int32_t state_EOS(); 93 void WaitForEOS(); 94 int32_t ConfigureVideoDecoder(); 95 int32_t StartVideoDecoder(); 96 int64_t GetSystemTimeUs(); 97 int32_t CreateVideoDecoder(std::string codeName); 98 int32_t SetVideoDecoderCallback(); 99 int32_t SetSurface(OHNativeWindow *window); 100 int32_t Release(); 101 int32_t SetParameter(OH_AVFormat *format); 102 void CheckOutputDescription(); 103 void OutputFunc(); 104 void WriteOutputFrame(uint32_t index, OH_AVMemory *buffer, OH_AVCodecBufferAttr attr, FILE *outFile); 105 void InputFunc_AVCC(); 106 OH_AVErrCode InputFunc_FUZZ(const uint8_t *data, size_t size); 107 void ReleaseSignal(); 108 void ReleaseInFile(); 109 void StopInloop(); 110 void StopOutloop(); 111 bool IsRender(); 112 bool MdCompare(unsigned char *buffer, int len, const char *source[]); 113 VDecSignal *signal_; 114 uint32_t errCount = 0; 115 uint32_t outCount = 0; 116 int64_t outTimeArray[2000] = {}; 117 bool sleepOnFPS = false; 118 bool repeatRun = false; 119 int64_t start_time = 0; 120 int64_t end_time = 0; 121 bool setParameters = false; 122 bool checkOutPut = true; 123 OH_AVCodec *vdec_; 124 125 private: 126 std::atomic<bool> isRunning_ { false }; 127 std::unique_ptr<std::ifstream> inFile_; 128 std::unique_ptr<uint8_t[]> prereadBuffer_; 129 std::unique_ptr<std::vector<uint8_t>> mpegUnit_; 130 std::unique_ptr<std::thread> inputLoop_; 131 std::unique_ptr<std::thread> outputLoop_; 132 std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_; 133 std::unordered_map<uint32_t, OH_AVMemory *> outBufferMap_; 134 OH_AVCodecAsyncCallback cb_; 135 int64_t timeStamp_ { 0 }; 136 int64_t lastRenderedTimeUs_ { 0 }; 137 bool isFirstFrame_ = true; 138 uint32_t pPrereadBuffer_; 139 uint32_t prereadBufferSize_; 140 }; 141 } // namespace Media 142 } // namespace OHOS 143 144 void VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData); 145 void VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData); 146 void VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData); 147 void VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr, 148 void *userData); 149 #endif // VIDEODEC_SAMPLE_H 150