• 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_API11_SAMPLE_H
17 #define VIDEOENC_API11_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_videoencoder.h"
31 #include "nocopyable.h"
32 #include "native_avbuffer.h"
33 #include "native_avformat.h"
34 #include "native_averrors.h"
35 #include "window.h"
36 #include "media_description.h"
37 #include "av_common.h"
38 #include "external_window.h"
39 #include "native_buffer.h"
40 namespace OHOS {
41 namespace Media {
42 class VEncAPI11Signal {
43 public:
44     std::mutex inMutex_;
45     std::mutex outMutex_;
46     std::condition_variable inCond_;
47     std::condition_variable outCond_;
48     std::queue<uint32_t> inIdxQueue_;
49     std::queue<uint32_t> outIdxQueue_;
50     std::queue<OH_AVBuffer *> inBufferQueue_;
51     std::queue<OH_AVBuffer *> outBufferQueue_;
52 };
53 struct fileInfo {
54     std::string fileDir;
55     OH_NativeBuffer_Format format;
56     uint32_t width;
57     uint32_t height;
58 };
59 
60 typedef struct LtrTestParameter {
61     uint32_t ltrInterval = 0;
62     bool enableUseLtr = false;
63     bool useBadLtr = false;
64     bool useLtrOnce = false;
65     uint32_t useLtrIndex = 0;
66     bool markAndUseSelf = false;
67     int32_t ltrCount = 0;
68 }LtrTestParameter;
69 
70 class VEncAPI11Sample : public NoCopyable {
71 public:
72     VEncAPI11Sample() = default;
73     ~VEncAPI11Sample();
74     const char *INP_DIR = "/data/test/media/1280_720_nv.yuv";
75     const char *OUT_DIR = "/data/test/media/VEncTest.h264";
76     uint32_t DEFAULT_WIDTH = 1280;
77     uint32_t DEFAULT_HEIGHT = 720;
78     uint32_t DEFAULT_BITRATE = 5000000;
79     uint32_t DEFAULT_QUALITY = 30;
80     double DEFAULT_FRAME_RATE = 30.0;
81     bool isAVCEncoder = true;
82     OH_HEVCProfile hevcProfile = HEVC_PROFILE_MAIN;
83     OH_AVCProfile avcProfile = AVC_PROFILE_BASELINE;
84     int32_t DEFAULT_QP = 20;
85     uint32_t DEFAULT_BITRATE_MODE = CBR;
86     int32_t DEFAULT_PROFILE = HEVC_PROFILE_MAIN;
87     OH_AVPixelFormat DEFAULT_PIX_FMT = AV_PIXEL_FORMAT_NV12;
88     uint32_t DEFAULT_KEY_FRAME_INTERVAL = 1000;
89     uint32_t DEFAULT_RANGE_FLAG = 0;
90     uint32_t DEFAULT_COLOR_PRIMARIES = COLOR_PRIMARY_BT709;
91     uint32_t DEFAULT_TRANSFER_CHARACTERISTICS = TRANSFER_CHARACTERISTIC_BT709;
92     uint32_t DEFAULT_MATRIX_COEFFICIENTS = MATRIX_COEFFICIENT_BT709;
93     uint32_t repeat_time = 0;
94     int32_t CreateVideoEncoder(const char *codecName);
95     int32_t ConfigureVideoEncoder();
96     int32_t ConfigureVideoEncoder_Temporal(int32_t temporal_gop_size);
97     int32_t ConfigureVideoEncoder_fuzz(int32_t data);
98     int32_t SetVideoEncoderCallback();
99     int32_t CreateSurface();
100     int32_t StartVideoEncoder();
101     int32_t VideoEncoder();
102     int32_t SetParameter(OH_AVFormat *format);
103     void SetBufferParameter(OH_AVBuffer *buffer);
104     void SetLTRParameter(OH_AVBuffer *buffer);
105     void SetForceIDR();
106     void GetStride();
107     void testApi();
108     void WaitForEOS();
109     int32_t SyncOutputFuncEos(uint32_t &last_index, uint32_t &outFrames, uint32_t &index,
110         OH_AVBuffer *buffer, OH_AVCodecBufferAttr &attr);
111     int32_t OpenFile();
112     uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
113     int64_t GetSystemTimeUs();
114     int32_t Start();
115     int32_t Flush();
116     int32_t Reset();
117     int32_t Stop();
118     int32_t Release();
119     int32_t ReadFile(uint32_t index, OH_AVBuffer *buffer);
120     void Flush_buffer();
121     void AutoSwitchParam();
122     void RepeatStartBeforeEOS();
123     bool RandomEOS(uint32_t index);
124     void SetEOS(uint32_t index, OH_AVBuffer *buffer);
125     int32_t PushData(OH_AVBuffer *buffer, uint32_t index, int32_t &result);
126     int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
127     void InputFunc();
128     void SyncInputFunc();
129     int32_t state_EOS();
130     void InputFuncSurface();
131     uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
132     uint32_t ReadOneFrameRGBA8888(uint8_t *dst);
133     uint32_t ReadOneFrameYUVP010(uint8_t *dst);
134     uint32_t ReadOneFrameFromList(uint8_t *dst, int32_t &fileIndex);
135     uint32_t ReadOneFrameByType(uint8_t *dst, OH_NativeBuffer_Format format);
136     int32_t OpenFileFail();
137     int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr);
138     void OutputFuncFail();
139     void OutputFunc();
140     void SyncOutputFunc();
141     uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer);
142     void ReleaseSignal();
143     void ReleaseInFile();
144     void StopInloop();
145     void StopOutloop();
146     void DumpLtrInfo(OH_AVBuffer *buffer);
147     void DumpQPInfo(OH_AVBuffer *buffer);
148     void DumpInfo(OH_AVCodecBufferAttr attr, OH_AVBuffer *buffer);
149     void readMultiFilesFunc();
150     int32_t InitBuffer(OHNativeWindowBuffer *&ohNativeWindowBuffer, OH_NativeBuffer *&nativeBuffer, uint8_t *&dst);
151     void InputEnableRepeatSleep();
152     int32_t QueryInputBuffer(uint32_t index, int64_t timeoutUs);
153     int32_t QueryOutputBuffer(uint32_t index, int64_t timeoutUs);
154     OH_AVBuffer *GetInputBuffer(uint32_t index);
155     OH_AVBuffer *GetOutputBuffer(uint32_t index);
156     void SetConfigureEnc(OH_AVFormat *format);
157     VEncAPI11Signal *signal_;
158     uint32_t errCount = 0;
159     bool enableForceIDR = false;
160     uint32_t outCount = 0;
161     uint32_t frameCount = 0;
162     uint32_t switchParamsTimeSec = 3;
163     bool sleepOnFPS = false;
164     bool SURF_INPUT = false;
165     bool enableAutoSwitchParam = false;
166     bool enableColorSpaceParams = false;
167     bool enableAutoSwitchBufferParam = false;
168     bool needResetBitrate = false;
169     bool needResetFrameRate = false;
170     bool needResetQP = false;
171     bool enableQP = false;
172     bool repeatRun = false;
173     bool showLog = false;
174     bool enableLTR = false;
175     bool getInputBufferIndexRepeat = false;
176     bool abnormalIndexValue = false;
177     bool getOutputBufferIndexNoExisted = false;
178     int64_t encode_count = 0;
179     bool enable_random_eos = false;
180     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
181     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
182     int64_t start_time = 0;
183     int64_t end_time = 0;
184     LtrTestParameter ltrParam;
185     bool TEMPORAL_CONFIG = false;
186     bool TEMPORAL_ENABLE = false;
187     bool TEMPORAL_JUMP_MODE = false;
188     bool TEMPORAL_DEFAULT = false;
189     bool TEMPORAL_UNIFORMLY = false;
190     bool getQpMse = false;
191     std::vector<fileInfo> fileInfos;
192     bool readMultiFiles = false;
193     bool setFormatRbgx = false;
194     bool configMain = false;
195     bool configMain10 = false;
196     bool setFormat8Bit = false;
197     bool setFormat10Bit = false;
198     bool enableRepeat = false;
199     bool enableSeekEos = false;
200     bool setMaxCount = false;
201     int32_t DEFAULT_FRAME_AFTER = 1;
202     int32_t DEFAULT_MAX_COUNT = 1;
203     uint32_t inCount = 0;
204     int32_t enbleSyncMode = 0;
205     int32_t enbleBFrameMode = 0;
206     int64_t syncInputWaitTime = -1;
207     int64_t syncOutputWaitTime = -1;
208     bool queryOutputBufferEOS = false;
209     bool queryInputBufferEOS = false;
210     bool getOutputBufferIndexRepeated = false;
211     bool noDestroy = false;
212     std::atomic<bool> isRunning_ { false };
213 private:
214     std::unique_ptr<std::ifstream> inFile_;
215     std::unique_ptr<std::thread> inputLoop_;
216     std::unique_ptr<std::thread> outputLoop_;
217     std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_;
218     std::unordered_map<uint32_t, OH_AVBuffer *> outBufferMap_;
219     OH_AVCodec *venc_;
220     OH_AVCodecCallback cb_;
221     int64_t timeStamp_ { 0 };
222     int64_t lastRenderedTimeUs_ { 0 };
223     bool isFirstFrame_ = true;
224     OHNativeWindow *nativeWindow;
225     int stride_;
226     static constexpr uint32_t SAMPLE_RATIO = 2;
227 };
228 } // namespace Media
229 } // namespace OHOS
230 
231 #endif // VIDEOENC_API11_SAMPLE_H
232