• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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_SAMPLE_H
17 #define VIDEOENC_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_avmemory.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_inner.h"
40 namespace OHOS {
41 namespace Media {
42 class VEncSignal {
43 public:
44     std::mutex inMutex_;
45     std::mutex outMutex_;
46     std::mutex flushMutex_;
47     std::condition_variable inCond_;
48     std::condition_variable outCond_;
49     std::queue<uint32_t> inIdxQueue_;
50     std::queue<uint32_t> outIdxQueue_;
51     std::queue<OH_AVCodecBufferAttr> attrQueue_;
52     std::queue<OH_AVMemory *> inBufferQueue_;
53     std::queue<OH_AVMemory *> outBufferQueue_;
54 };
55 
56 class VEncNdkFuzzSample : public NoCopyable {
57 public:
58     VEncNdkFuzzSample() = default;
59     ~VEncNdkFuzzSample();
60     const char *inpDir = "/data/test/media/1280_720_nv.yuv";
61     const uint8_t *fuzzData;
62     size_t fuzzSize;
63     uint32_t defaultWidth = 1280;
64     uint32_t defaultHeight = 720;
65     uint32_t defaultBitrate = 5000000;
66     uint32_t defaultQuality = 30;
67     double defaultFrameRate = 30.0;
68     uint32_t defaultFuzzTime = 30;
69     uint32_t defaultBitrateMode = CBR;
70     OH_AVPixelFormat DEFAULT_PIX_FMT = AV_PIXEL_FORMAT_NV12;
71     uint32_t defaultKeyFrameInterval = 1000;
72     int32_t CreateVideoEncoder();
73     int32_t ConfigureVideoEncoderFuzz(int32_t data);
74     int32_t ConfigureVideoEncoder();
75     int32_t ConfigureVideoEncoderTemporal(int32_t temporalGopSize);
76     int32_t SetVideoEncoderCallback();
77     int32_t CreateSurface();
78     int32_t StartVideoEncoder();
79     int32_t SetParameterFuzz(int32_t data);
80     int32_t SetParameter(OH_AVFormat *format);
81     void SetForceIDR();
82     void GetStride();
83     void TestApi();
84     void WaitForEOS();
85     int32_t OpenFile();
86     uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
87     int64_t GetSystemTimeUs();
88     int32_t Start();
89     int32_t Flush();
90     int32_t Reset();
91     int32_t Stop();
92     int32_t Release();
93     void FlushBuffer();
94     void AutoSwitchParam();
95     void RepeatStartBeforeEOS();
96     bool RandomEOS(uint32_t index);
97     void SetEOS(uint32_t index);
98     int32_t PushData(OH_AVMemory *buffer, uint32_t index, int32_t &result);
99     void InputDataNormal(bool &runningFlag, uint32_t index, OH_AVMemory *buffer);
100     void InputDataFuzz(bool &runningFlag, uint32_t index);
101     int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
102     void InputFunc();
103     int32_t StateEos();
104     bool ProcessNativeWindowBuffer(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer);
105     void InputFuncSurface();
106     uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
107     void ReadOneFrameRGBA8888(uint8_t *dst);
108     int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr);
109     void OutputFuncFail();
110     void OutputFunc();
111     uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer);
112     void ReleaseSignal();
113     void ReleaseInFile();
114     void StopInloop();
115     void StopOutloop();
116 
117     VEncSignal *signal_;
118     uint32_t errCount = 0;
119     bool enableForceIDR = false;
120     uint32_t outCount = 0;
121     uint32_t frameCount = 0;
122     uint32_t switchParamsTimeSec = 3;
123     bool sleepOnFPS = false;
124     bool surfInput = false;
125     bool enableAutoSwitchParam = false;
126     bool needResetBitrate = false;
127     bool needResetFrameRate = false;
128     bool needResetQP = false;
129     bool repeatRun = false;
130     bool showLog = false;
131     bool fuzzMode = false;
132     bool inputCallbackFlush = false;
133     bool inputCallbackStop = false;
134     bool outputCallbackFlush = false;
135     bool outputCallbackStop = false;
136     int64_t encodeCount = 0;
137     bool enableRandomEos = false;
138     uint32_t repeatStartStopBeforeEos = 0;  // 1200 ��������
139     uint32_t repeatStartFlushBeforeEos = 0; // 1300 ��������
140     uint32_t randomEos = 0;
141     bool temporalConfig = false;
142     bool temporalEnable = false;
143     bool temporalJumpMode = false;
144     std::atomic<bool> isRunning_ { false };
145     std::atomic<bool> isFlushing_ { false };
146 private:
147     std::unique_ptr<std::ifstream> inFile_;
148     std::unique_ptr<std::thread> inputLoop_;
149     std::unique_ptr<std::thread> outputLoop_;
150     std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_;
151     std::unordered_map<uint32_t, OH_AVMemory *> outBufferMap_;
152     OH_AVCodec *venc_;
153     OH_AVCodecAsyncCallback cb_;
154     int64_t timeStamp_ { 0 };
155     int64_t lastRenderedTimeUs_ { 0 };
156     bool isFirstFrame_ = true;
157     OHNativeWindow *nativeWindow;
158     int stride_;
159     static constexpr uint32_t sampleRatio = 2;
160 };
161 } // namespace Media
162 } // namespace OHOS
163 
164 #endif // VIDEODEC_SAMPLE_H
165