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 VIDEOENC_INNER_SAMPLE_H
17 #define VIDEOENC_INNER_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 "avcodec_video_encoder.h"
31 #include "nocopyable.h"
32 #include "buffer/avsharedmemory.h"
33 #include "meta/format.h"
34 #include "avcodec_errors.h"
35 #include "surface/window.h"
36 #include "media_description.h"
37 #include "av_common.h"
38 #include "avcodec_common.h"
39 #include "external_window.h"
40 
41 namespace OHOS {
42 namespace MediaAVCodec {
43 struct fileInfo {
44     std::string fileDir;
45     GraphicPixelFormat format;
46     uint32_t width;
47     uint32_t height;
48 };
49 class VEncInnerSignal {
50 public:
51     std::mutex inMutex_;
52     std::mutex outMutex_;
53     std::condition_variable inCond_;
54     std::condition_variable outCond_;
55     std::queue<uint32_t> inIdxQueue_;
56     std::queue<uint32_t> outIdxQueue_;
57     std::queue<AVCodecBufferInfo> infoQueue_;
58     std::queue<AVCodecBufferFlag> flagQueue_;
59     std::queue<std::shared_ptr<AVSharedMemory>> inBufferQueue_;
60     std::queue<std::shared_ptr<AVSharedMemory>> outBufferQueue_;
61     std::queue<std::shared_ptr<Format>> inFormatQueue_;
62     std::queue<std::shared_ptr<Format>> inAttrQueue_;
63 };
64 
65 class VEncInnerCallback : public AVCodecCallback, public NoCopyable {
66 public:
67     explicit VEncInnerCallback(std::shared_ptr<VEncInnerSignal> signal);
68     ~VEncInnerCallback() = default;
69 
70     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
71     void OnOutputFormatChanged(const Format& format) override;
72     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
73     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
74         std::shared_ptr<AVSharedMemory> buffer) override;
75 
76 private:
77     std::shared_ptr<VEncInnerSignal> innersignal_;
78 };
79 
80 
81 class VEncParamWithAttrCallbackTest : public MediaCodecParameterWithAttrCallback {
82 public:
83     explicit VEncParamWithAttrCallbackTest(std::shared_ptr<VEncInnerSignal>);
84     virtual ~VEncParamWithAttrCallbackTest();
85     void OnInputParameterWithAttrAvailable(uint32_t index, std::shared_ptr<Format> attribute,
86                                            std::shared_ptr<Format> parameter) override;
87 private:
88     std::shared_ptr<VEncInnerSignal> signal_ = nullptr;
89 };
90 
91 class VEncNdkInnerSample : public NoCopyable {
92 public:
93     explicit VEncNdkInnerSample(std::shared_ptr<VEncInnerSignal> signal);
94     VEncNdkInnerSample() = default;
95     ~VEncNdkInnerSample();
96 
97     int64_t GetSystemTimeUs();
98     int32_t CreateByMime(const std::string &mime);
99     int32_t CreateByName(const std::string &name);
100     int32_t Configure();
101     int32_t ConfigureFuzz(int32_t data);
102     int32_t Prepare();
103     int32_t Start();
104     int32_t Stop();
105     int32_t Flush();
106     int32_t NotifyEos();
107     int32_t Reset();
108     int32_t Release();
109     int32_t CreateInputSurface();
110     int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag);
111     int32_t GetOutputFormat(Format &format);
112     int32_t ReleaseOutputBuffer(uint32_t index);
113     int32_t SetParameter(const Format &format);
114     int32_t SetCallback();
115     int32_t SetCallback(std::shared_ptr<MediaCodecParameterWithAttrCallback> cb);
116     int32_t GetInputFormat(Format &format);
117 
118     int32_t StartVideoEncoder();
119     int32_t testApi();
120     int32_t PushData(std::shared_ptr<AVSharedMemory> buffer, uint32_t index, int32_t &result);
121     int32_t OpenFileFail();
122     int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
123     int32_t CheckFlag(AVCodecBufferFlag flag);
124     int32_t InputProcess(OH_NativeBuffer *nativeBuffer, OHNativeWindowBuffer *ohNativeWindowBuffer);
125     int32_t StateEOS();
126     uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
127     uint32_t ReadOneFrameRGBA8888(uint8_t *dst);
128     uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
129     uint32_t ReadOneFrameYUVP010(uint8_t *dst);
130     uint32_t ReadOneFrameFromList(uint8_t *dst, int32_t &fileIndex);
131     uint32_t ReadOneFrameByType(uint8_t *dst, std::string &fileType);
132     uint32_t ReadOneFrameByType(uint8_t *dst, GraphicPixelFormat format);
133     int32_t PushInputParameter(uint32_t index);
134     bool RandomEOS(uint32_t index);
135     void RepeatStartBeforeEOS();
136     void SetEOS(uint32_t index);
137     void WaitForEOS();
138     void InputParamLoopFunc();
139     void InputFuncSurface();
140     void InputFunc();
141     void OutputFunc();
142     void OutputFuncFail();
143     void FlushBuffer();
144     void StopInloop();
145     void StopOutloop();
146     void ReleaseInFile();
147     void InputEnableRepeatSleep();
148     void PushRandomDiscardIndex(uint32_t count, uint32_t min, uint32_t max);
149     bool IsFrameDiscard(uint32_t index);
150     bool CheckOutputFrameCount();
151     void readMultiFilesFunc();
152     int32_t SetCustomBuffer(BufferRequestConfig bufferRequestConfig);
153     bool ReadCustomDataToAVBuffer(const std::string &fileName, std::shared_ptr<AVBuffer> buffer);
154     bool GetWaterMarkCapability(std::string codecMimeType);
155     int32_t InitBuffer(OHNativeWindowBuffer *&ohNativeWindowBuffer, OH_NativeBuffer *&nativeBuffer, uint8_t *&dst);
156 
157     const char *INP_DIR = "/data/test/media/1280_720_nv.yuv";
158     const char *OUT_DIR = "/data/test/media/VEncTest.h264";
159     const char *WATER_MARK_DIR = "/data/test/media/128_72_0.rgba";
160     uint32_t DEFAULT_WIDTH = 1280;
161     uint32_t DEFAULT_HEIGHT = 720;
162     uint32_t DEFAULT_BITRATE = 10000000;
163     double DEFAULT_FRAME_RATE = 30.0;
164     uint32_t DEFAULT_KEY_FRAME_INTERVAL = 1000;
165     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
166     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
167     uint32_t DEFAULT_KEY_I_FRAME_INTERVAL = 333; // 1300 测试用例,1000、333
168 
169     uint32_t errCount = 0;
170     uint32_t outCount = 0;
171     uint32_t inCount = 0;
172     uint32_t frameCount = 0;
173     bool enableForceIDR = false;
174     bool sleepOnFPS = false;
175     bool surfaceInput = false;
176     bool repeatRun = false;
177     bool enableRandomEos = false;
178     int64_t encodeCount = 0;
179     uint32_t DEFAULT_BITRATE_MODE = CBR;
180     bool enableRepeat = false;
181     bool enableSeekEos = false;
182     int32_t DEFAULT_FRAME_AFTER = 1;
183     int32_t DEFAULT_MAX_COUNT = 1;
184     uint32_t DEFAULT_KEY_I_INTERVAL = 10;
185     int32_t discardInterval = -1;
186     bool isDiscardFrame = false;
187     std::vector<int32_t> discardFrameIndex;
188     int32_t discardMaxIndex = -1;
189     int32_t discardMinIndex = -1;
190     int32_t discardFrameCount = 0;
191     int32_t inputFrameCount = 0;
192     bool setMaxCount = false;
193     bool enableWaterMark = false;
194     int32_t videoCoordinateX = 100;
195     int32_t videoCoordinateY = 100;
196     int32_t videoCoordinateWidth = 400;
197     int32_t videoCoordinateHeight = 400;
198     std::vector<std::string> fileDirs;
199     std::vector<fileInfo> fileInfos;
200     bool readMultiFiles = false;
201     bool setFormatRbgx = false;
202     bool configMain = false;
203     bool configMain10 = false;
204     bool setFormat8Bit = false;
205     bool setFormat10Bit = false;
206 
207 private:
208     std::atomic<bool> isRunning_ { false };
209     std::unique_ptr<std::ifstream> inFile_;
210     std::unique_ptr<std::thread> inputLoop_;
211     std::unique_ptr<std::thread> inputParamLoop_;
212     std::unique_ptr<std::thread> outputLoop_;
213     std::shared_ptr<AVCodecVideoEncoder> venc_;
214     std::shared_ptr<VEncInnerSignal> signal_;
215     std::shared_ptr<VEncInnerCallback> cb_;
216     int stride_;
217     OHNativeWindow *nativeWindow;
218     static constexpr uint32_t SAMPLE_RATIO = 2;
219     bool isSetParamCallback_ = false;
220 };
221 } // namespace MediaAVCodec
222 } // namespace OHOS
223 
224 #endif // VIDEODEC_INNER_SAMPLE_H
225