• 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_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.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 
54 class VEncAPI11Sample : public NoCopyable {
55 public:
56     VEncAPI11Sample() = default;
57     ~VEncAPI11Sample();
58     const char *INP_DIR = "/data/test/media/1280_720_nv.yuv";
59     const char *OUT_DIR = "/data/test/media/VEncTest.h265";
60     const char *roiInfo = "0,0-128,64=-4;100,200-300,400=3";
61     uint32_t DEFAULT_WIDTH = 1280;
62     uint32_t DEFAULT_HEIGHT = 720;
63     uint64_t DEFAULT_BITRATE = 5000000;
64     uint64_t DEFAULT_MAX_BITRATE = 8000000;
65     uint32_t DEFAULT_QUALITY = 30;
66     uint32_t DEFAULT_SQR_FACTOR = 28;
67     double DEFAULT_FRAME_RATE = 30.0;
68     uint32_t DEFAULT_QUALITY_RUN = 28;
69     uint64_t DEFAULT_BITRATE_RUN = 6000000;
70     uint64_t DEFAULT_SQR_FACTOR_RUN = 30;
71     uint32_t DEFAULT_FRAME_RATE_RUN = 30;
72     uint64_t DEFAULT_MAX_BITRATE_RUN =  50000000;
73     uint32_t DEFAULT_INTERVAL_RUN = 1000;
74     uint32_t DEFAULT_BITRATE_MODE_RUN = CBR;
75     uint32_t DEFAULT_BFRAME = 1;
76     uint32_t DEFAULT_MAX_B_FRAMES = 3;
77     bool SETBIRATE = false;
78     bool MODE_ENABLE = false;
79     bool QUALITY_ENABLE = false;
80     bool FACTOR_ENABLE = false;
81     bool MAXBITE_ENABLE = false;
82     bool B_ENABLE = false;
83     bool QUALITY_ENABLE_RUN = false;
84     bool SETBIRATE_RUN = false;
85     bool FACTOR_ENABLE_RUN = false;
86     bool MAXBITE_ENABLE_RUN = false;
87     bool IFRAME_ENABLE_RUN = false;
88     bool FRAMERATE_ENABLE_RUN = false;
89     bool MODE_ENABLE_RUN = false;
90     bool enablePTSBasedRateControl = false;
91     bool MAXBFRAMES_ENABLE = false;
92     uint32_t DEFAULT_BITRATE_MODE = CBR;
93     OH_AVPixelFormat DEFAULT_PIX_FMT = AV_PIXEL_FORMAT_NV12;
94     uint32_t DEFAULT_KEY_FRAME_INTERVAL = 1000;
95     std::vector<time_t> timeList;
96     int32_t CreateVideoEncoder(const char *codecName);
97     int32_t ConfigureVideoEncoder();
98     int32_t ConfigureVideoEncoderSqr();
99     int32_t SetVideoEncoderCallback();
100     int32_t CreateSurface();
101     int32_t StartVideoEncoder();
102     int32_t SetParameter();
103     void GetStride();
104     void WaitForEOS();
105     int32_t OpenFile();
106     uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
107     int64_t GetSystemTimeUs();
108     int32_t Start();
109     int32_t Flush();
110     int32_t Reset();
111     int32_t Stop();
112     int32_t Release();
113     void Flush_buffer();
114     void RepeatStartBeforeEOS();
115     bool RandomEOS(uint32_t index);
116     void SetEOS(uint32_t index, OH_AVBuffer *buffer);
117     int32_t PushData(OH_AVBuffer *buffer, uint32_t index, int32_t &result);
118     int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
119     void InputFunc();
120     void InputFuncSurface();
121     uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
122     uint32_t ReadOneFrameRGBA8888(uint8_t *dst);
123     int32_t OpenFileFail();
124     int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr);
125     void OutputFuncFail();
126     void OutputFunc();
127     uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer);
128     void ReleaseSignal();
129     void ReleaseInFile();
130     void StopInloop();
131     void StopOutloop();
132     void readMultiFilesFunc();
133     int32_t GetBframes(OH_AVFormat *format);
134     int32_t InitBuffer(OHNativeWindowBuffer *&ohNativeWindowBuffer, OH_NativeBuffer *&nativeBuffer, uint8_t *&dst);
135     int32_t LoadTimeStampData(std::string filePath, std::string &inputDir,
136                             std::string &outputDir, uint32_t &w, uint32_t &h,
137                             uint32_t &bitrateMode, uint32_t &bitRate, bool &surfaceMode);
138     VEncAPI11Signal *signal_;
139     uint32_t errCount = 0;
140     uint32_t outCount = 0;
141     uint32_t frameCount = 0;
142     bool sleepOnFPS = false;
143     bool SURF_INPUT = false;
144     bool repeatRun = false;
145     bool enableParameter = false;
146     int64_t encode_count = 0;
147     bool enable_random_eos = false;
148     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;
149     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0;
150     int64_t frameIndex = 0;
151     bool readMultiFiles = false;
152     bool configMain10 = false;
153     bool isParamSet = false;
154     uint32_t frameCount_1 = 0;
155 private:
156     std::atomic<bool> isRunning_ { false };
157     std::unique_ptr<std::ifstream> inFile_;
158     std::unique_ptr<std::thread> inputLoop_;
159     std::unique_ptr<std::thread> outputLoop_;
160     OH_AVCodecCallback cb_;
161     OH_AVCodec *venc_;
162     OHNativeWindow *nativeWindow;
163     int stride_;
164     static constexpr uint32_t SAMPLE_RATIO = 2;
165 };
166 } // namespace Media
167 } // namespace OHOS
168 
169 #endif // VIDEOENC_API11_SAMPLE_H
170