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 "surface/window.h"
36 #include "media_description.h"
37 #include "av_common.h"
38 #include "external_window.h"
39 #include "native_buffer_inner.h"
40 namespace OHOS {
41 namespace Media {
42 class VEncSignal {
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 
54 typedef struct LtrTestParameter {
55     uint32_t ltrInterval = 0;
56     bool enableUseLtr = false;
57     bool useBadLtr = false;
58     bool useLtrOnce = false;
59     uint32_t useLtrIndex = 0;
60     bool markAndUseSelf = false;
61     int32_t ltrCount = 0;
62 }LtrTestParameter;
63 
64 class VEncAPI11Sample : public NoCopyable {
65 public:
66     VEncAPI11Sample() = default;
67     ~VEncAPI11Sample();
68     const char *INP_DIR = "/data/test/media/1280_720_nv.yuv";
69     const char *OUT_DIR = "/data/test/media/VEncTest.h264";
70     uint32_t DEFAULT_WIDTH = 1280;
71     uint32_t DEFAULT_HEIGHT = 720;
72     uint32_t DEFAULT_BITRATE = 5000000;
73     uint32_t DEFAULT_QUALITY = 30;
74     double DEFAULT_FRAME_RATE = 30.0;
75     int32_t DEFAULT_QP = 20;
76     uint32_t DEFAULT_BITRATE_MODE = CBR;
77     OH_AVPixelFormat DEFAULT_PIX_FMT = AV_PIXEL_FORMAT_NV12;
78     uint32_t DEFAULT_KEY_FRAME_INTERVAL = 1000;
79     uint32_t repeat_time = 0;
80     int32_t CreateVideoEncoder(const char *codecName);
81     int32_t ConfigureVideoEncoder();
82     int32_t ConfigureVideoEncoder_Temporal(int32_t temporal_gop_size);
83     int32_t ConfigureVideoEncoder_fuzz(int32_t data);
84     int32_t SetVideoEncoderCallback();
85     int32_t CreateSurface();
86     int32_t StartVideoEncoder();
87     int32_t SetParameter(OH_AVFormat *format);
88     void SetBufferParameter(OH_AVBuffer *buffer);
89     void SetLTRParameter(OH_AVBuffer *buffer);
90     void SetForceIDR();
91     void GetStride();
92     void testApi();
93     void WaitForEOS();
94     int32_t OpenFile();
95     uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
96     int64_t GetSystemTimeUs();
97     int32_t Start();
98     int32_t Flush();
99     int32_t Reset();
100     int32_t Stop();
101     int32_t Release();
102     void Flush_buffer();
103     void AutoSwitchParam();
104     void RepeatStartBeforeEOS();
105     bool RandomEOS(uint32_t index);
106     void SetEOS(uint32_t index, OH_AVBuffer *buffer);
107     int32_t PushData(OH_AVBuffer *buffer, uint32_t index, int32_t &result);
108     int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
109     void InputFunc();
110     int32_t state_EOS();
111     void InputFuncSurface();
112     uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
113     void ReadOneFrameRGBA8888(uint8_t *dst);
114     int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr);
115     void OutputFuncFail();
116     void OutputFunc();
117     uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer);
118     void ReleaseSignal();
119     void ReleaseInFile();
120     void StopInloop();
121     void StopOutloop();
122     void DumpLtrInfo(OH_AVBuffer *buffer);
123     void DumpQPInfo(OH_AVBuffer *buffer);
124     void DumpInfo(OH_AVCodecBufferAttr attr, OH_AVBuffer *buffer);
125     VEncSignal *signal_;
126     uint32_t errCount = 0;
127     bool enableForceIDR = false;
128     uint32_t outCount = 0;
129     uint32_t frameCount = 0;
130     uint32_t switchParamsTimeSec = 3;
131     bool sleepOnFPS = false;
132     bool SURF_INPUT = false;
133     bool enableAutoSwitchParam = false;
134     bool enableAutoSwitchBufferParam = false;
135     bool needResetBitrate = false;
136     bool needResetFrameRate = false;
137     bool needResetQP = false;
138     bool repeatRun = false;
139     bool showLog = false;
140     bool enableLTR = false;
141     int64_t encode_count = 0;
142     bool enable_random_eos = false;
143     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
144     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
145     int64_t start_time = 0;
146     int64_t end_time = 0;
147     LtrTestParameter ltrParam;
148     bool TEMPORAL_CONFIG = false;
149     bool TEMPORAL_ENABLE = false;
150     bool TEMPORAL_JUMP_MODE = false;
151     bool TEMPORAL_DEFAULT = false;
152     bool getQpMse = false;
153 private:
154     std::atomic<bool> isRunning_ { false };
155     std::unique_ptr<std::ifstream> inFile_;
156     std::unique_ptr<std::thread> inputLoop_;
157     std::unique_ptr<std::thread> outputLoop_;
158     std::unordered_map<uint32_t, OH_AVBuffer *> inBufferMap_;
159     std::unordered_map<uint32_t, OH_AVBuffer *> outBufferMap_;
160     OH_AVCodec *venc_;
161     OH_AVCodecCallback cb_;
162     int64_t timeStamp_ { 0 };
163     int64_t lastRenderedTimeUs_ { 0 };
164     bool isFirstFrame_ = true;
165     OHNativeWindow *nativeWindow;
166     int stride_;
167     static constexpr uint32_t SAMPLE_RATIO = 2;
168 };
169 } // namespace Media
170 } // namespace OHOS
171 
172 #endif // VIDEOENC_API11_SAMPLE_H
173