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_DEMO_H 17 #define AVCODEC_VIDEO_DECODER_DEMO_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <queue> 22 #include <string> 23 #include <thread> 24 #include <string> 25 26 #include "surface/window.h" 27 #include "nocopyable.h" 28 #include "native_avcodec_videodecoder.h" 29 #include "avcodec_video_decoder_inner_demo.h" 30 31 #ifdef __cplusplus 32 extern "C" { 33 #endif 34 #include "libavcodec/avcodec.h" 35 #ifdef __cplusplus 36 } 37 #endif 38 39 namespace OHOS { 40 namespace MediaAVCodec { 41 namespace VideoDemo { 42 class VDecSignal { 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> inQueue_; 49 std::queue<uint32_t> outQueue_; 50 std::queue<OH_AVMemory *> inBufferQueue_; 51 std::queue<OH_AVMemory *> outBufferQueue_; 52 std::queue<OH_AVCodecBufferAttr> attrQueue_; 53 }; 54 55 class VDecDemo : public NoCopyable { 56 public: 57 VDecDemo(); 58 virtual ~VDecDemo(); 59 void RunCase(std::string &mode); 60 void RunDrmCase(); 61 62 private: 63 int32_t CreateDec(); 64 int32_t Configure(OH_AVFormat *format); 65 int32_t SetSurface(OHNativeWindow *window); 66 sptr<Surface> GetSurface(std::string &mode); 67 int32_t Start(); 68 int32_t Stop(); 69 int32_t Flush(); 70 int32_t Reset(); 71 int32_t Release(); 72 int32_t ExtractPacket(); 73 void InputFunc(); 74 void OutputFunc(); 75 76 std::atomic<bool> isRunning_ = false; 77 std::unique_ptr<std::ifstream> inputFile_ = nullptr; 78 std::unique_ptr<std::ofstream> outFile_ = nullptr; 79 std::unique_ptr<std::thread> inputLoop_ = nullptr; 80 std::unique_ptr<std::thread> outputLoop_ = nullptr; 81 OH_AVCodec *videoDec_ = nullptr; 82 VDecSignal *signal_ = nullptr; 83 struct OH_AVCodecAsyncCallback cb_; 84 bool isFirstFrame_ = true; 85 int64_t timeStamp_ = 0; 86 87 // Extract packet 88 static constexpr int32_t VIDEO_INBUF_SIZE = 10240; 89 static constexpr int32_t VIDEO_REFILL_THRESH = 4096; 90 const AVCodec *codec_ = nullptr; 91 AVCodecParserContext *parser_ = nullptr; 92 AVCodecContext *codec_ctx_ = nullptr; 93 AVPacket *pkt_ = nullptr; 94 size_t data_size_ = 0; 95 uint8_t *data_ = nullptr; 96 uint8_t inbuf_[VIDEO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = {0}; 97 bool file_end_ = false; 98 std::string mode_ = "0"; 99 }; 100 } // namespace VideoDemo 101 } // namespace MediaAVCodec 102 } // namespace OHOS 103 #endif // AVCODEC_VIDEO_DECODER_DEMO_H 104