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