1 /* 2 * Copyright (C) 2022 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 VIDEODECENC_NDK_SAMPLE_H 17 #define VIDEODECENC_NDK_SAMPLE_H 18 19 #include <iostream> 20 #include <stdio.h> 21 #include <unistd.h> 22 #include <atomic> 23 #include <fstream> 24 #include <thread> 25 #include <mutex> 26 #include <queue> 27 #include <string> 28 #include "securec.h" 29 #include "nocopyable.h" 30 #include "ndktest_log.h" 31 #include "native_avmagic.h" 32 #include "surface.h" 33 #include "native_avcodec_base.h" 34 #include "native_avcodec_videodecoder.h" 35 #include "native_avcodec_videoencoder.h" 36 37 namespace OHOS { 38 namespace Media { 39 class VDecEncSignal { 40 public: 41 std::mutex inMutexDec_; 42 std::mutex outMutexDec_; 43 std::condition_variable inCondDec_; 44 std::condition_variable outCondDec_; 45 std::queue<uint32_t> inQueueDec_; 46 std::queue<uint32_t> outQueueDec_; 47 std::queue<uint32_t> flagQueueDec_; 48 std::queue<OH_AVMemory *> inBufferQueueDec_; 49 std::queue<OH_AVMemory *> outBufferQueueDec_; 50 51 std::mutex outMutexEnc_; 52 std::condition_variable outCondEnc_; 53 std::queue<uint32_t> inQueueEnc_; 54 std::queue<uint32_t> outQueueEnc_; 55 std::queue<uint32_t> sizeQueueEnc_; 56 std::queue<uint32_t> flagQueueEnc_; 57 std::queue<OH_AVMemory *> inBufferQueueEnc_; 58 std::queue<OH_AVMemory *> outBufferQueueEnc_; 59 int32_t errorNum_ = 0; 60 std::atomic<bool> isVdecFlushing_ = false; 61 std::atomic<bool> isVencFlushing_ = false; 62 }; 63 64 class VDecEncNdkSample : public NoCopyable { 65 public: 66 VDecEncNdkSample() = default; 67 ~VDecEncNdkSample(); 68 69 void SetEosState(bool needSetEos); 70 struct OH_AVCodec* CreateVideoDecoderByMime(std::string mimetype); 71 struct OH_AVCodec* CreateVideoDecoderByName(std::string name); 72 int32_t ConfigureDec(struct OH_AVFormat *format); 73 int32_t SetParameterDec(struct OH_AVFormat *format); 74 int32_t SetOutputSurface(); 75 int32_t PrepareDec(); 76 int32_t StartDec(); 77 int32_t StopDec(); 78 int32_t FlushDec(); 79 int32_t ResetDec(); 80 int32_t ReleaseDec(); 81 82 struct OH_AVCodec* CreateVideoEncoderByMime(std::string mimetype); 83 struct OH_AVCodec* CreateVideoEncoderByName(std::string name); 84 int32_t ConfigureEnc(struct OH_AVFormat *format); 85 int32_t SetParameterEnc(struct OH_AVFormat *format); 86 int32_t GetSurface(); 87 int32_t PrepareEnc(); 88 int32_t StartEnc(); 89 int32_t StopEnc(); 90 int32_t FlushEnc(); 91 int32_t ResetEnc(); 92 int32_t ReleaseEnc(); 93 94 int32_t CalcuError(); 95 void SetReadPath(std::string filepath); 96 void SetSavePath(std::string filepath); 97 void ReRead(); 98 void ResetDecParam(); 99 void ResetEncParam(); 100 int32_t GetFrameCount(); 101 bool GetEncEosState(); 102 bool GetDecEosState(); 103 void PopInqueueDec(); 104 void PopOutqueueDec(); 105 void PopOutqueueEnc(); 106 void SendEncEos(); 107 int32_t PushInbufferDec(uint32_t index, uint32_t bufferSize); 108 VDecEncSignal* vcodecSignal_ = nullptr; 109 bool isDecInputEOS = false; 110 bool isEncInputEOS = false; 111 bool isDecOutputEOS = false; 112 bool isEncOutputEOS = false; 113 bool setEos = true; 114 bool needRender = true; 115 116 private: 117 OHNativeWindow *nativeWindow_; 118 struct OH_AVCodec* vdec_; 119 void InputFuncDec(); 120 void OutputFuncDec(); 121 int32_t WriteToFile(); 122 std::atomic<bool> isDecRunning_ = false; 123 std::unique_ptr<std::ifstream> testFile_; 124 std::unique_ptr<std::thread> inputLoopDec_; 125 std::unique_ptr<std::thread> outputLoopDec_; 126 struct OH_AVCodecAsyncCallback cbDec_; 127 int64_t timeStampDec_ = 0; 128 uint32_t decInCnt_ = 0; 129 uint32_t decOutCnt_ = 0; 130 131 struct OH_AVCodec* venc_; 132 void OutputFuncEnc(); 133 std::atomic<bool> isEncRunning_ = false; 134 std::unique_ptr<std::thread> outputLoopEnc_; 135 struct OH_AVCodecAsyncCallback cbEnc_; 136 bool isFirstDecFrame_ = true; 137 uint32_t encOutCnt_ = 0; 138 std::string inFile_ = "/data/media/out_320_240_10s.h264"; 139 std::string outFile_ = "/data/media/video_out.es"; 140 }; 141 } 142 } 143 #endif // VIDEODECENC_NDK_SAMPLE_H 144