• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 SetParameter();
115     int32_t SetCallback();
116     int32_t SetCallback(std::shared_ptr<MediaCodecParameterWithAttrCallback> cb);
117     int32_t GetInputFormat(Format &format);
118     int32_t SyncOutputFuncEos(AVCodecBufferFlag flag, uint32_t index);
119     int32_t StartVideoEncoder();
120     int32_t VideoEncoder();
121     int32_t testApi();
122     int32_t PushData(std::shared_ptr<AVSharedMemory> buffer, uint32_t index, int32_t &result);
123     int32_t SyncPushData(std::shared_ptr<AVBuffer> buffer, uint32_t index, int32_t &result);
124     int32_t OpenFileFail();
125     int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
126     int32_t CheckFlag(AVCodecBufferFlag flag);
127     int32_t InputProcess(OH_NativeBuffer *nativeBuffer, OHNativeWindowBuffer *ohNativeWindowBuffer);
128     int32_t StateEOS();
129     uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
130     uint32_t ReadOneFrameRGBA8888(uint8_t *dst);
131     uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
132     uint32_t ReadOneFrameYUVP010(uint8_t *dst);
133     uint32_t ReadOneFrameFromList(uint8_t *dst, int32_t &fileIndex);
134     uint32_t ReadOneFrameByType(uint8_t *dst, std::string &fileType);
135     uint32_t ReadOneFrameByType(uint8_t *dst, GraphicPixelFormat format);
136     int32_t PushInputParameter(uint32_t index);
137     bool RandomEOS(uint32_t index);
138     void RepeatStartBeforeEOS();
139     void SetEOS(uint32_t index);
140     void WaitForEOS();
141     void InputParamLoopFunc();
142     void InputFuncSurface();
143     void InputFunc();
144     void SyncInputFunc();
145     void OutputFunc();
146     void SyncOutputFunc();
147     void OutputFuncFail();
148     void FlushBuffer();
149     void StopInloop();
150     void StopOutloop();
151     void ReleaseInFile();
152     void InputEnableRepeatSleep();
153     void PushRandomDiscardIndex(uint32_t count, uint32_t max, uint32_t min);
154     bool IsFrameDiscard(uint32_t index);
155     bool CheckOutputFrameCount();
156     void readMultiFilesFunc();
157     int32_t ConfigureVideoEncoderSqr();
158     int32_t SetCustomBuffer(BufferRequestConfig bufferRequestConfig);
159     bool ReadCustomDataToAVBuffer(const std::string &fileName, std::shared_ptr<AVBuffer> buffer);
160     bool GetWaterMarkCapability(std::string codecMimeType);
161     int32_t InitBuffer(OHNativeWindowBuffer *&ohNativeWindowBuffer, OH_NativeBuffer *&nativeBuffer, uint8_t *&dst);
162 
163     const char *INP_DIR = "/data/test/media/1280_720_nv.yuv";
164     const char *OUT_DIR = "/data/test/media/VEncTest.h264";
165     const char *WATER_MARK_DIR = "/data/test/media/128_72_0.rgba";
166     uint32_t DEFAULT_WIDTH = 1280;
167     uint32_t DEFAULT_HEIGHT = 720;
168     uint32_t DEFAULT_BITRATE = 10000000;
169     double DEFAULT_FRAME_RATE = 30.0;
170     uint32_t DEFAULT_KEY_FRAME_INTERVAL = 1000;
171     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
172     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
173     uint32_t DEFAULT_KEY_I_FRAME_INTERVAL = 333; // 1300 测试用例,1000、333
174     int32_t DEFAULT_PIX_FMT = static_cast<int32_t>(VideoPixelFormat::NV12);
175     int32_t DEFAULT_SQR_FACTOR = 30;
176     int64_t DEFAULT_MAX_BITERATE = 4000000;
177     int32_t DEFAULT_SQR_FACTOR_RUN = 30;
178     int64_t DEFAULT_MAX_BITERATE_RUN = 4000000;
179 
180     uint32_t errCount = 0;
181     uint32_t outCount = 0;
182     uint32_t inCount = 0;
183     uint32_t frameCount = 0;
184     bool enableForceIDR = false;
185     bool sleepOnFPS = false;
186     bool surfaceInput = false;
187     bool repeatRun = false;
188     bool enableRandomEos = false;
189     int64_t encodeCount = 0;
190     uint32_t DEFAULT_BITRATE_MODE = CBR;
191     bool enableRepeat = false;
192     bool enableSeekEos = false;
193     int32_t DEFAULT_FRAME_AFTER = 1;
194     int32_t DEFAULT_MAX_COUNT = 1;
195     uint32_t DEFAULT_KEY_I_INTERVAL = 10;
196     int32_t DEFAULT_BFRAME = 1;
197     uint32_t DEFAULT_GOP_MODE = 1;
198     int32_t discardInterval = -1;
199     bool isDiscardFrame = false;
200     std::vector<int32_t> discardFrameIndex;
201     int32_t discardMaxIndex = -1;
202     int32_t discardMinIndex = -1;
203     int32_t discardFrameCount = 0;
204     int32_t inputFrameCount = 0;
205     bool setMaxCount = false;
206     bool enableWaterMark = false;
207     bool enbleBFrameMode = false;
208     int32_t videoCoordinateX = 100;
209     int32_t videoCoordinateY = 100;
210     int32_t videoCoordinateWidth = 400;
211     int32_t videoCoordinateHeight = 400;
212     int32_t DEFAULT_QUALITY = 32;
213     int32_t DEFAULT_BITRATE_RUN = 5000000;
214     int32_t DEFAULT_MAX_B_FRAMES = 3;
215     std::vector<std::string> fileDirs;
216     std::vector<fileInfo> fileInfos;
217     bool readMultiFiles = false;
218     bool setFormatRbgx = false;
219     bool configMain = false;
220     bool configMain10 = false;
221     bool setFormat8Bit = false;
222     bool setFormat10Bit = false;
223     bool enableParameter = false;
224     bool isParamSet = false;
225     bool MODE_ENABLE = false;
226     bool SETBIRATE = false;
227     bool QUALITY_ENABLE = false;
228     bool B_ENABLE = false;
229     bool FACTOR_ENABLE = false;
230     bool MAXBITE_ENABLE = false;
231     bool FACTOR_ENABLE_RUN = false;
232     bool MAXBITE_ENABLE_RUN = false;
233     bool SETBIRATE_RUN = false;
234     bool GOPMODE_ENABLE = false;
235     bool MAXBFRAMES_ENABLE = false;
236     int32_t enbleSyncMode = 0;
237     int64_t syncInputWaitTime = -1;
238     int64_t syncOutputWaitTime = -1;
239     bool queryOutputBufferEOS = false;
240     bool queryInputBufferEOS = false;
241     std::atomic<bool> isRunning_ { false };
242     std::shared_ptr<VEncInnerSignal> signal_;
243 private:
244     std::unique_ptr<std::ifstream> inFile_;
245     std::unique_ptr<std::thread> inputLoop_;
246     std::unique_ptr<std::thread> inputParamLoop_;
247     std::unique_ptr<std::thread> outputLoop_;
248     std::shared_ptr<AVCodecVideoEncoder> venc_;
249     std::shared_ptr<VEncInnerCallback> cb_;
250     int stride_;
251     OHNativeWindow *nativeWindow;
252     static constexpr uint32_t SAMPLE_RATIO = 2;
253     bool isSetParamCallback_ = false;
254 };
255 } // namespace MediaAVCodec
256 } // namespace OHOS
257 
258 #endif // VIDEODEC_INNER_SAMPLE_H
259