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 "nocopyable.h" 32 #include "native_avmemory.h" 33 #include "native_avformat.h" 34 #include "native_averrors.h" 35 #include "surface/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/1920_1080_10_30Mb.h264"; 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 bool TRANSFER_FLAG = false; 64 bool NV21_FLAG = false; 65 bool PREPARE_FLAG = true; 66 bool IS_FIRST_FRAME = true; 67 uint32_t DEFAULT_WIDTH = 1920; 68 uint32_t DEFAULT_HEIGHT = 1080; 69 uint32_t originalWidth = 0; 70 uint32_t originalHeight = 0; 71 uint32_t defualtPixelFormat = AV_PIXEL_FORMAT_NV12; 72 uint32_t REPEAT_CALL_TIME = 10; 73 uint32_t MAX_SURF_NUM = 2; 74 double DEFAULT_FRAME_RATE = 30.0; 75 uint32_t DEFAULT_RANGE_FLAG = 0; 76 bool BEFORE_EOS_INPUT = false; // 0800 测试用例 77 bool BEFORE_EOS_INPUT_INPUT = false; // 0900 测试用例 78 bool AFTER_EOS_DESTORY_CODEC = true; // 1000 测试用例 结束不销毁codec 79 uint32_t REPEAT_START_STOP_BEFORE_EOS = 0; // 1200 测试用例 80 uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例 81 uint32_t frameCount_ = 0; 82 uint32_t repeat_time = 0; 83 uint32_t outFrameCount = 0; 84 bool outputYuvFlag = false; 85 // 解码输出数据预期 86 bool needCheckOutputDesc = false; 87 bool isResChangeStream = false; 88 uint32_t expectCropTop = 0; 89 uint32_t expectCropBottom = 0; 90 uint32_t expectCropLeft = 0; 91 uint32_t expectCropRight = 0; 92 int32_t stride_ = 0; 93 int32_t sliceHeight_ = 0; 94 int32_t picWidth_ = 0; 95 int32_t picHeight_ = 0; 96 97 int32_t Start(); 98 int32_t Stop(); 99 int32_t Flush(); 100 int32_t Reset(); 101 int32_t Prepare(); 102 int32_t state_EOS(); 103 void SetEOS(uint32_t index, OH_AVBuffer *buffer); 104 void WaitForEOS(); 105 int32_t ConfigureVideoDecoder(); 106 int32_t StartDecoder(); 107 int32_t StartVideoDecoder(); 108 int64_t GetSystemTimeUs(); 109 int32_t CreateVideoDecoder(std::string codeName); 110 int32_t SetVideoDecoderCallback(); 111 void testAPI(); 112 int32_t SwitchSurface(); 113 int32_t RepeatCallSetSurface(); 114 int32_t Release(); 115 int32_t SetParameter(OH_AVFormat *format); 116 void CheckOutputDescription(); 117 void AutoSwitchSurface(); 118 void InputFunc(); 119 int32_t PushData(uint32_t index, OH_AVBuffer *buffer); 120 int32_t CheckAndReturnBufferSize(OH_AVBuffer *buffer); 121 uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVBuffer *buffer); 122 void ProcessOutputData(OH_AVBuffer *buffer, uint32_t index); 123 int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr); 124 void GetStride(); 125 void InputFuncTest(); 126 void InFuncTest(); 127 void OutputFuncTest(); 128 void ReleaseSignal(); 129 void CreateSurface(); 130 void ReleaseInFile(); 131 void StopInloop(); 132 void Flush_buffer(); 133 void StopOutloop(); 134 bool IsRender(); 135 void RenderOutAtTime(uint32_t index); 136 bool MdCompare(uint8_t source[]); 137 std::vector<uint8_t> LoadHashFile(); 138 VDecAPI11Signal *signal_; 139 uint32_t errCount = 0; 140 uint32_t outCount = 0; 141 int64_t renderTimestampNs = 0; 142 bool rsAtTime = false; 143 int64_t outTimeArray[2000] = {}; 144 bool sleepOnFPS = false; 145 bool repeatRun = false; 146 int64_t decode_count = 0; 147 int64_t start_time = 0; 148 int32_t maxInputSize = 0; 149 int64_t end_time = 0; 150 bool autoSwitchSurface = false; 151 std::atomic<bool> isFlushing_ { false }; 152 int32_t switchSurfaceFlag = 0; 153 std::atomic<bool> isRunning_ { false }; 154 bool inputCallbackFlush = false; 155 bool inputCallbackStop = false; 156 bool outputCallbackFlush = false; 157 bool outputCallbackStop = false; 158 bool useHDRSource = false; 159 bool isAPI = false; 160 int32_t DEFAULT_PROFILE = HEVC_PROFILE_MAIN_10; 161 int32_t DecodeSetSurface(); 162 private: 163 std::unique_ptr<std::ifstream> inFile_; 164 std::unique_ptr<std::thread> inputLoop_; 165 std::unique_ptr<std::thread> outputLoop_; 166 std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_; 167 std::unordered_map<uint32_t, OH_AVBuffer *> outBufferMap_; 168 OH_AVCodec *vdec_; 169 OH_AVCodecCallback cb_; 170 int64_t timeStamp_ { 0}; 171 int64_t lastRenderedTimeUs_ { 0 }; 172 bool isFirstFrame_ = true; 173 OHNativeWindow *nativeWindow[2] = {}; 174 sptr<Surface> cs[2] = {}; 175 sptr<Surface> ps[2] = {}; 176 }; 177 } // namespace Media 178 } // namespace OHOS 179 180 void VdecAPI11Error(OH_AVCodec *codec, int32_t errorCode, void *userData); 181 void VdecAPI11FormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData); 182 void VdecAPI11InputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData); 183 void VdecAPI11OutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData); 184 #endif // VIDEODEC_SAMPLE_H 185