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_INNER_SAMPLE_H 17 #define VIDEODEC_INNER_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 "avcodec_video_decoder.h" 31 #include "nocopyable.h" 32 #include "buffer/avbuffer.h" 33 #include "meta/format.h" 34 #include "avcodec_errors.h" 35 #include "media_description.h" 36 #include "av_common.h" 37 #include "avcodec_common.h" 38 #include "surface/window.h" 39 40 namespace OHOS { 41 namespace MediaAVCodec { 42 class VDecInnerSignal { 43 public: 44 std::mutex inMutex_; 45 std::mutex outMutex_; 46 std::condition_variable inCond_; 47 std::condition_variable outCond_; 48 std::queue<uint32_t> inIdxQueue_; 49 std::queue<uint32_t> outIdxQueue_; 50 std::queue<std::shared_ptr<AVBuffer>> inBufferQueue_; 51 std::queue<std::shared_ptr<AVBuffer>> outBufferQueue_; 52 }; 53 54 class VDecInnerCallback : public MediaCodecCallback, public NoCopyable { 55 public: 56 explicit VDecInnerCallback(std::shared_ptr<VDecInnerSignal> signal); 57 ~VDecInnerCallback() = default; 58 59 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 60 void OnOutputFormatChanged(const Format& format) override; 61 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 62 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 63 64 private: 65 std::shared_ptr<VDecInnerSignal> innersignal_; 66 }; 67 68 class VDecNdkInnerSample : public NoCopyable { 69 public: 70 VDecNdkInnerSample() = default; 71 ~VDecNdkInnerSample(); 72 73 int64_t GetSystemTimeUs(); 74 int32_t CreateByMime(const std::string &mime); 75 int32_t CreateByName(const std::string &name); 76 int32_t Configure(); 77 int32_t Prepare(); 78 int32_t Start(); 79 int32_t Stop(); 80 int32_t Flush(); 81 int32_t Reset(); 82 int32_t Release(); 83 int32_t QueueInputBuffer(uint32_t index); 84 int32_t GetOutputFormat(Format &format); 85 int32_t ReleaseOutputBuffer(uint32_t index); 86 int32_t SetParameter(const Format &format); 87 int32_t SetCallback(); 88 89 int32_t StartVideoDecoder(); 90 int32_t RunVideoDecoder(const std::string &codeName); 91 int32_t PushData(std::shared_ptr<AVBuffer> buffer, uint32_t index); 92 int32_t SendData(uint32_t bufferSize, uint32_t index, std::shared_ptr<AVBuffer> buffer); 93 int32_t StateEOS(); 94 void RepeatStartBeforeEOS(); 95 void SetEOS(uint32_t index, std::shared_ptr<AVBuffer> buffer); 96 void WaitForEOS(); 97 void OpenFileFail(); 98 void InputFunc(); 99 void OutputFunc(); 100 void ProcessOutputData(std::shared_ptr<AVBuffer> buffer, uint32_t index); 101 void FlushBuffer(); 102 void StopInloop(); 103 void StopOutloop(); 104 void ReleaseInFile(); 105 bool MdCompare(unsigned char *buffer, int len, const char *source[]); 106 107 const char *INP_DIR = "/data/test/media/1920_1080_10_30Mb.h265"; 108 const char *OUT_DIR = "/data/test/media/VDecTest.yuv"; 109 uint32_t DEFAULT_WIDTH = 1920; 110 uint32_t DEFAULT_HEIGHT = 1080; 111 uint32_t DEFAULT_BITRATE = 10000000; 112 double DEFAULT_FRAME_RATE = 30.0; 113 uint32_t REPEAT_START_STOP_BEFORE_EOS = 0; // 1200 测试用例 114 uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例 115 bool SF_OUTPUT = false; 116 bool BEFORE_EOS_INPUT = false; // 0800 测试用例 117 bool BEFORE_EOS_INPUT_INPUT = false; // 0900 测试用例 118 bool AFTER_EOS_DESTORY_CODEC = true; // 1000 测试用例 结束不销毁codec 119 120 uint32_t errCount = 0; 121 uint32_t outCount = 0; 122 uint32_t frameCount = 0; 123 bool sleepOnFPS = false; 124 bool repeatRun = false; 125 bool enableRandomEos = false; 126 const char *fileSourcesha256[64] = {"27", "6D", "A2", "D4", "18", "21", "A5", "CD", "50", "F6", "DD", "CA", "46", 127 "32", "C3", "FE", "58", "FC", "BC", "51", "FD", "70", "C7", "D4", "E7", "4D", 128 "5C", "76", "E7", "71", "8A", "B3", "C0", "51", "84", "0A", "FA", "AF", "FA", 129 "DC", "7B", "C5", "26", "D1", "9A", "CA", "00", "DE", "FC", "C8", "4E", "34", 130 "C5", "9A", "43", "59", "85", "DC", "AC", "97", "A3", "FB", "23", "51"}; 131 132 private: 133 std::atomic<bool> isRunning_ { false }; 134 std::unique_ptr<std::ifstream> inFile_; 135 std::unique_ptr<std::thread> inputLoop_; 136 std::unique_ptr<std::thread> outputLoop_; 137 std::shared_ptr<AVCodecVideoDecoder> vdec_; 138 std::shared_ptr<VDecInnerSignal> signal_; 139 std::shared_ptr<VDecInnerCallback> cb_; 140 }; 141 } // namespace MediaAVCodec 142 } // namespace OHOS 143 144 #endif // VIDEODEC_INNER_SAMPLE_H 145