1 /*
2  * Copyright (C) 2024 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_SAMPLE_H
17 #define VIDEODEC_SAMPLE_H
18 
19 #include <iostream>
20 #include <cstdio>
21 #include <unistd.h>
22 #include <atomic>
23 #include <fstream>
24 #include <thread>
25 #include <mutex>
26 #include <queue>
27 #include <string>
28 #include <unordered_map>
29 #include "securec.h"
30 #include "native_avcodec_videodecoder.h"
31 #include "nocopyable.h"
32 #include "native_avmemory.h"
33 #include "native_avformat.h"
34 #include "native_averrors.h"
35 #include "surface/window.h"
36 #include "iconsumer_surface.h"
37 
38 namespace OHOS {
39 namespace Media {
40 class VDecSignal {
41 public:
42     std::mutex inMutex_;
43     std::mutex outMutex_;
44     std::condition_variable inCond_;
45     std::condition_variable outCond_;
46     std::queue<uint32_t> inIdxQueue_;
47     std::queue<uint32_t> outIdxQueue_;
48     std::queue<OH_AVCodecBufferAttr> attrQueue_;
49     std::queue<OH_AVMemory *> inBufferQueue_;
50     std::queue<OH_AVMemory *> outBufferQueue_;
51 };
52 
53 class VDecNdkSample : public NoCopyable {
54 public:
55     VDecNdkSample() = default;
56     ~VDecNdkSample();
57     int32_t RunVideoDec_Surface(std::string codeName = "");
58     int32_t RunVideoDec(std::string codeName = "");
59     std::string INP_DIR = "/data/test/media/1920_1080_30.h265";
60     const char *OUT_DIR = "/data/test/media/VDecTest.yuv";
61     const char *OUT_DIR2 = "/data/test/media/VDecTest2.yuv";
62     bool SF_OUTPUT = false;
63     uint32_t DEFAULT_WIDTH = 1920;
64     uint32_t DEFAULT_HEIGHT = 1080;
65     uint32_t defualtPixelFormat = AV_PIXEL_FORMAT_NV12;
66     uint32_t REPEAT_CALL_TIME = 10;
67     uint32_t MAX_SURF_NUM = 2;
68     double DEFAULT_FRAME_RATE = 30.0;
69     uint32_t DEFAULT_RANGE_FLAG = 0;
70     bool BEFORE_EOS_INPUT = false;              // 0800 测试用例
71     bool BEFORE_EOS_INPUT_INPUT = false;        // 0900 测试用例
72     bool AFTER_EOS_DESTORY_CODEC = true;        // 1000 测试用例 结束不销毁codec
73     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
74     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
75     uint32_t frameCount_ = 0;
76     uint32_t repeat_time = 0;
77     // 解码输出数据预期
78     bool checkOutPut = true;
79     bool isResChangeStream = false;
80     int32_t stride_ = 0;
81     int32_t sliceHeight_ = 0;
82     int32_t picWidth_ = 0;
83     int32_t picHeight_ = 0;
84 
85     int32_t Start();
86     int32_t Stop();
87     int32_t Flush();
88     int32_t Reset();
89     int32_t state_EOS();
90     void SetEOS(uint32_t index);
91     void WaitForEOS();
92     int32_t ConfigureVideoDecoder();
93     int32_t StartVideoDecoder();
94     int64_t GetSystemTimeUs();
95     int32_t CreateVideoDecoder(std::string codeName);
96     int32_t SetVideoDecoderCallback();
97     void testAPI();
98     int32_t SwitchSurface();
99     int32_t RepeatCallSetSurface();
100     int32_t Release();
101     int32_t SetParameter(OH_AVFormat *format);
102     void CheckOutputDescription();
103     void AutoSwitchSurface();
104     void InputFunc();
105     int32_t PushData(uint32_t index, OH_AVMemory *buffer);
106     int32_t CheckAndReturnBufferSize(OH_AVMemory *buffer);
107     uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVMemory *buffer);
108     void ProcessOutputData(OH_AVMemory *buffer, uint32_t index);
109     void InputFuncTest();
110     void OutputFuncTest();
111     void ReleaseSignal();
112     void CreateSurface();
113     void ReleaseInFile();
114     void StopInloop();
115     void Flush_buffer();
116     void StopOutloop();
117     bool IsRender();
118     bool MdCompare(uint8_t source[]);
119     std::vector<uint8_t> LoadHashFile();
120     VDecSignal *signal_;
121     uint32_t errCount = 0;
122     uint32_t outCount = 0;
123     int64_t outTimeArray[2000] = {};
124     bool sleepOnFPS = false;
125     bool repeatRun = false;
126     int64_t decode_count = 0;
127     int64_t start_time = 0;
128     int32_t maxInputSize = 0;
129     int64_t end_time = 0;
130     bool autoSwitchSurface = false;
131     std::atomic<bool> isFlushing_ {false};
132     int32_t switchSurfaceFlag = 0;
133     std::atomic<bool> isRunning_ { false };
134     bool inputCallbackFlush = false;
135     bool inputCallbackStop = false;
136     bool outputCallbackFlush = false;
137     bool outputCallbackStop = false;
138     int32_t DecodeSetSurface();
139 private:
140     std::unique_ptr<std::ifstream> inFile_;
141     std::unique_ptr<std::thread> inputLoop_;
142     std::unique_ptr<std::thread> outputLoop_;
143     std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_;
144     std::unordered_map<uint32_t, OH_AVMemory *> outBufferMap_;
145     OH_AVCodec *vdec_;
146     OH_AVCodecAsyncCallback cb_;
147     int64_t timeStamp_ { 0};
148     int64_t lastRenderedTimeUs_ { 0 };
149     bool isFirstFrame_ = true;
150     OHNativeWindow *nativeWindow[2] = {};
151     sptr<Surface> cs[2] = {};
152     sptr<Surface> ps[2] = {};
153 };
154 } // namespace Media
155 } // namespace OHOS
156 
157 std::vector<std::string> find_files(const std::string& dir_path, const std::string& extension);
158 void VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData);
159 void VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData);
160 void VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData);
161 void VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
162                          void *userData);
163 #endif // VIDEODEC_SAMPLE_H
164