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_API11_SAMPLE_H 17 #define VIDEODEC_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_videodecoder.h" 31 #include "nocopyable.h" 32 #include "native_avmemory.h" 33 #include "native_avformat.h" 34 #include "native_averrors.h" 35 #include "window.h" 36 #include "iconsumer_surface.h" 37 38 namespace OHOS { 39 namespace Media { 40 class VDecAPI11Signal { 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_AVBuffer *> inBufferQueue_; 50 std::queue<OH_AVBuffer *> outBufferQueue_; 51 }; 52 53 class VDecAPI11Sample : public NoCopyable { 54 public: 55 VDecAPI11Sample() = default; 56 ~VDecAPI11Sample(); 57 int32_t RunVideoDec_Surface(std::string codeName = ""); 58 int32_t RunVideoDec(std::string codeName = ""); 59 const char *INP_DIR = "/data/test/media/main@high_level_1920_1080_60.m2v"; 60 const char *OUT_DIR = "/data/test/media/VDecTest.yuv"; 61 const char *OUT_DIR2 = "/data/test/media/VDecTest2.yuv"; 62 bool SF_OUTPUT = false; 63 uint32_t DEFAULT_WIDTH = 1920; 64 uint32_t DEFAULT_HEIGHT = 1080; 65 uint32_t originalWidth = 0; 66 uint32_t originalHeight = 0; 67 bool SURFACE_OUTPUT = false; 68 uint32_t defualtPixelFormat = AV_PIXEL_FORMAT_NV12; 69 uint32_t REPEAT_CALL_TIME = 10; 70 uint32_t MAX_SURF_NUM = 2; 71 double DEFAULT_FRAME_RATE = 60.0; 72 uint32_t DEFAULT_RANGE_FLAG = 0; 73 bool BEFORE_EOS_INPUT = false; // 0800 测试用例 74 bool BEFORE_EOS_INPUT_INPUT = false; // 0900 测试用例 75 bool AFTER_EOS_DESTORY_CODEC = true; // 1000 测试用例 结束不销毁codec 76 uint32_t REPEAT_START_STOP_BEFORE_EOS = 0; // 1200 测试用例 77 uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例 78 uint32_t frameCount_ = 0; 79 bool outputYuvFlag = false; 80 uint32_t repeat_time = 0; 81 uint32_t outFrameCount = 0; 82 // 解码输出数据预期 83 bool finishLastPush = false; 84 bool needCheckOutputDesc = false; 85 bool isResChangeStream = false; 86 uint32_t expectCropTop = 0; 87 uint32_t expectCropBottom = 0; 88 uint32_t expectCropLeft = 0; 89 uint32_t expectCropRight = 0; 90 int32_t enbleSyncMode = 0; 91 int64_t syncInputWaitTime = -1; 92 int64_t syncOutputWaitTime = -1; 93 bool queryOutputBufferEOS = false; 94 bool queryInputBufferEOS = false; 95 const char *fileSourcesha256[64] = {"27", "6D", "A2", "D4", "18", "21", "A5", "CD", "50", "F6", "DD", "CA", "46", 96 "32", "C3", "FE", "58", "FC", "BC", "51", "FD", "70", "C7", "D4", "E7", "4D", 97 "5C", "76", "E7", "71", "8A", "B3", "C0", "51", "84", "0A", "FA", "AF", "FA", 98 "DC", "7B", "C5", "26", "D1", "9A", "CA", "00", "DE", "FC", "C8", "4E", "34", 99 "C5", "9A", "43", "59", "85", "DC", "AC", "97", "A3", "FB", "23", "51"}; 100 void GetBufferSize(); 101 void SetAttr(OH_AVCodecBufferAttr &attr, int64_t startPts, uint32_t bufferSize); 102 void SetRepeat(); 103 void PtrStep(uint32_t &bufferSize, unsigned char **pBuffer, uint32_t size); 104 void PtrStepExtraRead(uint32_t &bufferSize, unsigned char **pBuffer); 105 int32_t Start(); 106 int32_t Stop(); 107 int32_t Flush(); 108 int32_t Reset(); 109 int32_t state_EOS(); 110 int32_t DecodeSetSurface(); 111 void SetEOS(uint32_t index, OH_AVBuffer *buffer); 112 void WaitForEOS(); 113 int32_t ConfigureVideoDecoder(); 114 int32_t StartVideoDecoder(); 115 int32_t StartSyncVideoDecoder(); 116 int64_t GetSystemTimeUs(); 117 int32_t CreateVideoDecoder(std::string codeName); 118 int32_t SetVideoDecoderCallback(); 119 void testAPI(); 120 int32_t SwitchSurface(); 121 int32_t RepeatCallSetSurface(); 122 int32_t Release(); 123 int32_t SetParameter(OH_AVFormat *format); 124 void CheckOutputDescription(); 125 void AutoSwitchSurface(); 126 void InputFunc(); 127 int32_t PushData(uint32_t index, OH_AVBuffer *buffer); 128 int32_t CheckAndReturnBufferSize(OH_AVBuffer *buffer); 129 uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer); 130 void ProcessOutputData(OH_AVBuffer *buffer, uint32_t index, int32_t size); 131 int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr); 132 void OutputFunc(); 133 void InputFuncTest(); 134 void SyncInputFunc(); 135 void OutputFuncTest(); 136 void SyncOutputFunc(); 137 int32_t SyncOutputFuncEos(OH_AVCodecBufferAttr attr, uint32_t index); 138 void ReleaseSignal(); 139 void CreateSurface(); 140 void ReleaseInFile(); 141 void StopInloop(); 142 void Flush_buffer(); 143 void StopOutloop(); 144 bool IsRender(); 145 bool MdCompare(unsigned char *buffer, int len, const char *source[]); 146 VDecAPI11Signal *signal_; 147 uint32_t errCount = 0; 148 uint32_t outCount = 0; 149 int64_t renderTimestampNs = 0; 150 bool rsAtTime = false; 151 int64_t outTimeArray[2000] = {}; 152 bool sleepOnFPS = false; 153 bool repeatRun = false; 154 int64_t decode_count = 0; 155 int64_t start_time = 0; 156 int32_t maxInputSize = 0; 157 int64_t end_time = 0; 158 bool autoSwitchSurface = false; 159 int32_t switchSurfaceFlag = 0; 160 std::atomic<bool> isRunning_ { false }; 161 bool inputCallbackFlush = false; 162 bool inputCallbackStop = false; 163 bool outputCallbackFlush = false; 164 bool outputCallbackStop = false; 165 bool useHDRSource = false; 166 int32_t DEFAULT_PROFILE = HEVC_PROFILE_MAIN_10; 167 int enbleBlankFrame = 0; 168 private: 169 std::unique_ptr<uint8_t[]> prereadBuffer_; 170 std::unique_ptr<std::vector<uint8_t>> mpegUnit_; 171 std::unique_ptr<std::ifstream> inFile_; 172 std::unique_ptr<std::thread> inputLoop_; 173 std::unique_ptr<std::thread> outputLoop_; 174 std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_; 175 std::unordered_map<uint32_t, OH_AVBuffer *> outBufferMap_; 176 OH_AVCodec *vdec_; 177 OH_AVCodecCallback cb_; 178 int64_t timeStamp_ { 0}; 179 int64_t lastRenderedTimeUs_ { 0 }; 180 bool isFirstFrame_ = true; 181 uint32_t pPrereadBuffer_; 182 uint32_t prereadBufferSize_; 183 OHNativeWindow *nativeWindow[2] = {}; 184 sptr<Surface> cs[2] = {}; 185 sptr<Surface> ps[2] = {}; 186 }; 187 } // namespace Media 188 } // namespace OHOS 189 190 void VdecAPI11Error(OH_AVCodec *codec, int32_t errorCode, void *userData); 191 void VdecAPI11FormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData); 192 void VdecAPI11InputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData); 193 void VdecAPI11OutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData); 194 #endif // VIDEODEC_SAMPLE_H 195