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 AVCODEC_VIDEO_DECODER_INNER_DEMO_H 17 #define AVCODEC_VIDEO_DECODER_INNER_DEMO_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <thread> 22 #include <queue> 23 #include <string> 24 #include "surface/window.h" 25 #include "avcodec_common.h" 26 #include "avcodec_video_decoder.h" 27 #include "nocopyable.h" 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 #include "libavcodec/avcodec.h" 33 #ifdef __cplusplus 34 } 35 #endif 36 37 namespace OHOS { 38 namespace MediaAVCodec { 39 namespace InnerVideoDemo { 40 class TestConsumerListener : public IBufferConsumerListener { 41 public: 42 TestConsumerListener(sptr<Surface> cs, std::string_view name); 43 ~TestConsumerListener(); 44 void OnBufferAvailable() override; 45 46 private: 47 int64_t timestamp_ = 0; 48 OHOS::Rect damage_ = {}; 49 sptr<Surface> cs_ = nullptr; 50 std::unique_ptr<std::ofstream> outFile_; 51 }; 52 53 class VDecSignal { 54 public: 55 std::mutex inMutex_; 56 std::mutex outMutex_; 57 std::condition_variable inCond_; 58 std::condition_variable outCond_; 59 std::queue<uint32_t> inQueue_; 60 std::queue<uint32_t> outQueue_; 61 std::queue<std::shared_ptr<AVSharedMemory>> inBufferQueue_; 62 std::queue<std::shared_ptr<AVSharedMemory>> outBufferQueue_; 63 std::queue<AVCodecBufferInfo> infoQueue_; 64 std::queue<AVCodecBufferFlag> flagQueue_; 65 }; 66 67 class VDecDemoCallback : public AVCodecCallback, public NoCopyable { 68 public: 69 explicit VDecDemoCallback(std::shared_ptr<VDecSignal> signal); 70 virtual ~VDecDemoCallback() = default; 71 72 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 73 void OnOutputFormatChanged(const Format &format) override; 74 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override; 75 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 76 std::shared_ptr<AVSharedMemory> buffer) override; 77 78 private: 79 std::shared_ptr<VDecSignal> signal_; 80 }; 81 82 class VDecInnerDemo : public NoCopyable { 83 public: 84 VDecInnerDemo(); 85 virtual ~VDecInnerDemo(); 86 void RunCase(std::string &mode); 87 88 private: 89 int32_t CreateDec(); 90 int32_t Configure(const Format &format); 91 int32_t SetOutputSurface(sptr<Surface> surface); 92 sptr<Surface> GetSurface(std::string &mode); 93 int32_t Start(); 94 int32_t Stop(); 95 int32_t Flush(); 96 int32_t Reset(); 97 int32_t Release(); 98 void InputFunc(); 99 void OutputFunc(); 100 void HandleInputEOS(const uint32_t &index); 101 int32_t HandleNormalInput(const uint32_t &index, const int64_t &pts, const size_t &size); 102 int32_t ExtractPacket(); 103 104 std::atomic<bool> isRunning_ = false; 105 std::unique_ptr<std::ifstream> inputFile_ = nullptr; 106 std::unique_ptr<std::ofstream> outFile_ = nullptr; 107 std::unique_ptr<std::thread> inputLoop_ = nullptr; 108 std::unique_ptr<std::thread> outputLoop_ = nullptr; 109 std::shared_ptr<AVCodecVideoDecoder> videoDec_ = nullptr; 110 std::shared_ptr<VDecSignal> signal_ = nullptr; 111 std::shared_ptr<VDecDemoCallback> cb_ = nullptr; 112 113 // Extract packet 114 static constexpr int32_t VIDEO_INBUF_SIZE = 10240; 115 static constexpr int32_t VIDEO_REFILL_THRESH = 4096; 116 const AVCodec *codec_ = nullptr; 117 AVCodecParserContext *parser_ = nullptr; 118 AVCodecContext *codec_ctx_ = nullptr; 119 AVPacket *pkt_ = nullptr; 120 size_t data_size_ = 0; 121 uint8_t *data_ = nullptr; 122 uint8_t inbuf_[VIDEO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = {0}; 123 bool file_end_ = false; 124 125 std::string mode_ = "0"; 126 }; 127 } // namespace InnerVideoDemo 128 } // namespace MediaAVCodec 129 } // namespace OHOS 130 #endif // AVCODEC_VIDEO_DECODER_INNER_DEMO_H 131