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 61 private: 62 int32_t CreateDec(); 63 int32_t Configure(OH_AVFormat *format); 64 int32_t SetSurface(OHNativeWindow *window); 65 sptr<Surface> GetSurface(std::string &mode); 66 int32_t Start(); 67 int32_t Stop(); 68 int32_t Flush(); 69 int32_t Reset(); 70 int32_t Release(); 71 int32_t ExtractPacket(); 72 void InputFunc(); 73 void OutputFunc(); 74 75 std::atomic<bool> isRunning_ = false; 76 std::unique_ptr<std::ifstream> inputFile_ = nullptr; 77 std::unique_ptr<std::ofstream> outFile_ = nullptr; 78 std::unique_ptr<std::thread> inputLoop_ = nullptr; 79 std::unique_ptr<std::thread> outputLoop_ = nullptr; 80 OH_AVCodec *videoDec_ = nullptr; 81 VDecSignal *signal_ = nullptr; 82 struct OH_AVCodecAsyncCallback cb_; 83 bool isFirstFrame_ = true; 84 int64_t timeStamp_ = 0; 85 86 // Extract packet 87 static constexpr int32_t VIDEO_INBUF_SIZE = 10240; 88 static constexpr int32_t VIDEO_REFILL_THRESH = 4096; 89 const AVCodec *codec_ = nullptr; 90 AVCodecParserContext *parser_ = nullptr; 91 AVCodecContext *codec_ctx_ = nullptr; 92 AVPacket *pkt_ = nullptr; 93 size_t data_size_ = 0; 94 uint8_t *data_ = nullptr; 95 uint8_t inbuf_[VIDEO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = {0}; 96 bool file_end_ = false; 97 std::string mode_ = "0"; 98 }; 99 } // namespace VideoDemo 100 } // namespace MediaAVCodec 101 } // namespace OHOS 102 #endif // AVCODEC_VIDEO_DECODER_DEMO_H 103