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 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 "native_avcodec_base.h" 32 #include "nocopyable.h" 33 #include "native_avmemory.h" 34 #include "native_avformat.h" 35 #include "native_averrors.h" 36 #include "surface/window.h" 37 #include "iconsumer_surface.h" 38 39 namespace OHOS { 40 namespace Media { 41 class VDecAPI11Signal { 42 public: 43 std::mutex inMutex_; 44 std::mutex outMutex_; 45 std::condition_variable inCond_; 46 std::condition_variable outCond_; 47 std::queue<uint32_t> inIdxQueue_; 48 std::queue<uint32_t> outIdxQueue_; 49 std::queue<OH_AVCodecBufferAttr> attrQueue_; 50 std::queue<OH_AVBuffer *> inBufferQueue_; 51 std::queue<OH_AVBuffer *> outBufferQueue_; 52 }; 53 54 class VDecAPI11Sample : public NoCopyable { 55 public: 56 VDecAPI11Sample() = default; 57 ~VDecAPI11Sample(); 58 int32_t RunVideoDec_Surface(std::string codeName = ""); 59 int32_t RunVideoDec(std::string codeName = ""); 60 const char *INP_DIR = "/data/test/media/1920_1080_10_30Mb.h264"; 61 const char *OUT_DIR = "/data/test/media/VDecTest.yuv"; 62 const char *OUT_DIR2 = "/data/test/media/VDecTest2.yuv"; 63 bool SF_OUTPUT = false; 64 bool TRANSFER_FLAG = false; 65 bool NV21_FLAG = false; 66 bool PREPARE_FLAG = true; 67 bool IS_FIRST_FRAME = true; 68 uint32_t DEFAULT_WIDTH = 1920; 69 uint32_t DEFAULT_HEIGHT = 1080; 70 uint32_t originalWidth = 0; 71 uint32_t originalHeight = 0; 72 uint32_t defualtPixelFormat = AV_PIXEL_FORMAT_NV12; 73 uint32_t REPEAT_CALL_TIME = 10; 74 uint32_t MAX_SURF_NUM = 2; 75 double DEFAULT_FRAME_RATE = 30.0; 76 uint32_t DEFAULT_RANGE_FLAG = 0; 77 bool BEFORE_EOS_INPUT = false; // 0800 测试用例 78 bool BEFORE_EOS_INPUT_INPUT = false; // 0900 测试用例 79 bool AFTER_EOS_DESTORY_CODEC = true; // 1000 测试用例 结束不销毁codec 80 uint32_t REPEAT_START_STOP_BEFORE_EOS = 0; // 1200 测试用例 81 uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例 82 uint32_t frameCount_ = 0; 83 uint32_t repeat_time = 0; 84 uint32_t outFrameCount = 0; 85 bool outputYuvFlag = false; 86 bool NocaleHash = false; 87 // 解码输出数据预期 88 bool needCheckOutputDesc = false; 89 bool isResChangeStream = false; 90 uint32_t expectCropTop = 0; 91 uint32_t expectCropBottom = 0; 92 uint32_t expectCropLeft = 0; 93 uint32_t expectCropRight = 0; 94 int32_t stride_ = 0; 95 int32_t sliceHeight_ = 0; 96 int32_t picWidth_ = 0; 97 int32_t picHeight_ = 0; 98 bool enableVRR = false; 99 bool enableLowLatency = false; 100 bool getInputBufferIndexRepeat = false; 101 int32_t enbleSyncMode = 0; 102 int64_t syncInputWaitTime = -1; 103 int64_t syncOutputWaitTime = -1; 104 bool queryOutputBufferEOS = false; 105 bool queryInputBufferEOS = false; 106 bool getOutputBufferIndexRepeated = false; 107 bool getOutputBufferIndexNoExisted = false; 108 bool abnormalIndexValue = false; 109 110 int32_t Start(); 111 int32_t Stop(); 112 int32_t Flush(); 113 int32_t Reset(); 114 int32_t Prepare(); 115 int32_t state_EOS(); 116 void SetEOS(uint32_t index, OH_AVBuffer *buffer); 117 void WaitForEOS(); 118 int32_t ConfigureVideoDecoder(); 119 int32_t StartDecoder(); 120 int32_t StartSyncDecoder(); 121 int32_t StartVideoDecoder(); 122 int64_t GetSystemTimeUs(); 123 int32_t CreateVideoDecoder(std::string codeName); 124 int32_t SetVideoDecoderCallback(); 125 void testAPI(); 126 int32_t SwitchSurface(); 127 int32_t RepeatCallSetSurface(); 128 int32_t Release(); 129 int32_t SetParameter(OH_AVFormat *format); 130 void CheckOutputDescription(); 131 void AutoSwitchSurface(); 132 void InputFunc(); 133 int32_t PushData(uint32_t index, OH_AVBuffer *buffer); 134 int32_t CheckAndReturnBufferSize(OH_AVBuffer *buffer); 135 uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer); 136 void ProcessOutputData(OH_AVBuffer *buffer, uint32_t index); 137 int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr); 138 void GetStride(); 139 void InputFuncTest(); 140 void SyncInputFunc(); 141 void InFuncTest(); 142 void OutputFuncTest(); 143 void SyncOutputFunc(); 144 int32_t SyncOutputFuncEos(uint32_t &last_index, uint32_t &outFrames, uint32_t &index, 145 OH_AVBuffer *buffer, OH_AVCodecBufferAttr &attr); 146 int32_t QueryInputBuffer(uint32_t index, int64_t timeoutUs); 147 int32_t QueryOutputBuffer(uint32_t index, int64_t timeoutUs); 148 OH_AVBuffer *GetInputBuffer(uint32_t index); 149 OH_AVBuffer *GetOutputBuffer(uint32_t index); 150 int32_t PushInputBuffer(uint32_t index); 151 void ReleaseSignal(); 152 void CreateSurface(); 153 void ReleaseInFile(); 154 void StopInloop(); 155 void Flush_buffer(); 156 void StopOutloop(); 157 bool IsRender(); 158 void RenderOutAtTime(uint32_t index); 159 bool MdCompare(uint8_t source[]); 160 std::vector<uint8_t> LoadHashFile(); 161 VDecAPI11Signal *signal_; 162 uint32_t errCount = 0; 163 uint32_t outCount = 0; 164 int64_t renderTimestampNs = 0; 165 bool rsAtTime = false; 166 int64_t outTimeArray[2000] = {}; 167 bool sleepOnFPS = false; 168 bool repeatRun = false; 169 int64_t decode_count = 0; 170 int64_t start_time = 0; 171 int32_t maxInputSize = 0; 172 int64_t end_time = 0; 173 bool autoSwitchSurface = false; 174 std::atomic<bool> isFlushing_ { false }; 175 int32_t switchSurfaceFlag = 0; 176 std::atomic<bool> isRunning_ { false }; 177 bool inputCallbackFlush = false; 178 bool inputCallbackStop = false; 179 bool outputCallbackFlush = false; 180 bool outputCallbackStop = false; 181 bool useHDRSource = false; 182 bool isAPI = false; 183 bool outputYuvSurface = false; 184 int32_t DEFAULT_PROFILE = HEVC_PROFILE_MAIN_10; 185 int32_t DecodeSetSurface(); 186 int32_t OpenFile(); 187 int enbleBlankFrame = 0; 188 private: 189 std::unique_ptr<std::ifstream> inFile_; 190 std::unique_ptr<std::thread> inputLoop_; 191 std::unique_ptr<std::thread> outputLoop_; 192 std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_; 193 std::unordered_map<uint32_t, OH_AVBuffer *> outBufferMap_; 194 OH_AVCodec *vdec_; 195 OH_AVCodecCallback cb_; 196 int64_t timeStamp_ { 0}; 197 int64_t lastRenderedTimeUs_ { 0 }; 198 bool isFirstFrame_ = true; 199 OHNativeWindow *nativeWindow[2] = {}; 200 sptr<Surface> cs[2] = {}; 201 sptr<Surface> ps[2] = {}; 202 }; 203 } // namespace Media 204 } // namespace OHOS 205 206 void VdecAPI11Error(OH_AVCodec *codec, int32_t errorCode, void *userData); 207 void VdecAPI11FormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData); 208 void VdecAPI11InputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData); 209 void VdecAPI11OutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData); 210 #endif // VIDEODEC_SAMPLE_H 211