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 VIDEODEC_NDK_SAMPLE_H
17 #define VIDEODEC_NDK_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     const char *INP_DIR = "/data/test/media/1920_1080_10_30Mb.h264";
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 REPEAT_CALL_TIME = 10;
66     uint32_t MAX_SURF_NUM = 2;
67     double DEFAULT_FRAME_RATE = 30.0;
68     bool BEFORE_EOS_INPUT = false;              // 0800 测试用例
69     bool BEFORE_EOS_INPUT_INPUT = false;        // 0900 测试用例
70     bool AFTER_EOS_DESTORY_CODEC = true;        // 1000 测试用例 结束不销毁codec
71     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
72     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
73     uint32_t frameCount_ = 0;
74     uint32_t repeat_time = 0;
75     // 解码输出数据预期
76     bool needCheckOutputDesc = false;
77     bool isResChangeStream = false;
78     uint32_t expectCropTop = 0;
79     uint32_t expectCropBottom = 0;
80     uint32_t expectCropLeft = 0;
81     uint32_t expectCropRight = 0;
82     const char *fileSourcesha256[64] = {"27", "6D", "A2", "D4", "18", "21", "A5", "CD", "50", "F6", "DD", "CA", "46",
83                                         "32", "C3", "FE", "58", "FC", "BC", "51", "FD", "70", "C7", "D4", "E7", "4D",
84                                         "5C", "76", "E7", "71", "8A", "B3", "C0", "51", "84", "0A", "FA", "AF", "FA",
85                                         "DC", "7B", "C5", "26", "D1", "9A", "CA", "00", "DE", "FC", "C8", "4E", "34",
86                                         "C5", "9A", "43", "59", "85", "DC", "AC", "97", "A3", "FB", "23", "51"};
87 
88     int32_t Start();
89     int32_t Stop();
90     int32_t Flush();
91     int32_t Reset();
92     int32_t state_EOS();
93     void SetEOS(uint32_t index);
94     void WaitForEOS();
95     int32_t ConfigureVideoDecoder();
96     int32_t StartVideoDecoder();
97     int64_t GetSystemTimeUs();
98     int32_t CreateVideoDecoder(std::string codeName);
99     int32_t SetVideoDecoderCallback();
100     void testAPI();
101     int32_t SwitchSurface();
102     int32_t RepeatCallSetSurface();
103     int32_t Release();
104     int32_t SetParameter(OH_AVFormat *format);
105     void CheckOutputDescription();
106     void AutoSwitchSurface();
107     void InputFunc();
108     int32_t PushData(uint32_t index, OH_AVMemory *buffer);
109     uint32_t SendData(uint32_t bufferSize, uint32_t index, OH_AVMemory *buffer);
110     void ProcessOutputData(OH_AVMemory *buffer, uint32_t index);
111     void OutputFunc();
112     void InputFuncTest();
113     void OutputFuncTest();
114     void ReleaseSignal();
115     void CreateSurface();
116     void ReleaseInFile();
117     void StopInloop();
118     void Flush_buffer();
119     void StopOutloop();
120     bool IsRender();
121     bool MdCompare(unsigned char *buffer, int len, const char *source[]);
122     VDecSignal *signal_;
123     uint32_t errCount = 0;
124     uint32_t outCount = 0;
125     int64_t outTimeArray[2000] = {};
126     bool sleepOnFPS = false;
127     bool repeatRun = false;
128     int64_t decode_count = 0;
129     int64_t start_time = 0;
130     int64_t end_time = 0;
131     bool autoSwitchSurface = 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 private:
139     std::unique_ptr<std::ifstream> inFile_;
140     std::unique_ptr<std::thread> inputLoop_;
141     std::unique_ptr<std::thread> outputLoop_;
142     std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_;
143     std::unordered_map<uint32_t, OH_AVMemory *> outBufferMap_;
144     OH_AVCodec *vdec_;
145     OH_AVCodecAsyncCallback cb_;
146     int64_t timeStamp_ { 0};
147     int64_t lastRenderedTimeUs_ { 0 };
148     bool isFirstFrame_ = true;
149     OHNativeWindow *nativeWindow[2] = {};
150     sptr<Surface> cs[2] = {};
151     sptr<Surface> ps[2] = {};
152 };
153 } // namespace Media
154 } // namespace OHOS
155 
156 void VdecError(OH_AVCodec *codec, int32_t errorCode, void *userData);
157 void VdecFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData);
158 void VdecInputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData);
159 void VdecOutputDataReady(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
160                          void *userData);
161 #endif // VIDEODEC_NDK_SAMPLE_H
162