• 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 #include <arpa/inet.h>
16 #include <sys/time.h>
17 #include <utility>
18 #include "iconsumer_surface.h"
19 #include "openssl/crypto.h"
20 #include "openssl/sha.h"
21 #include "native_buffer_inner.h"
22 #include "display_type.h"
23 #include "videoenc_api11_sample.h"
24 #include "native_avcapability.h"
25 using namespace OHOS;
26 using namespace OHOS::Media;
27 using namespace std;
28 namespace {
29 constexpr int64_t NANOS_IN_SECOND = 1000000000L;
30 constexpr int64_t NANOS_IN_MICRO = 1000L;
31 constexpr uint32_t FRAME_INTERVAL = 16666;
32 constexpr uint32_t MAX_PIXEL_FMT = 5;
33 constexpr uint8_t RGBA_SIZE = 4;
34 constexpr uint32_t IDR_FRAME_INTERVAL = 10;
35 constexpr uint32_t DOUBLE = 2;
36 constexpr uint32_t BADPOC = 1000;
37 constexpr uint32_t LTR_INTERVAL = 5;
38 sptr<Surface> cs = nullptr;
39 sptr<Surface> ps = nullptr;
40 OH_AVCapability *cap = nullptr;
41 VEncAPI11Sample *enc_sample = nullptr;
42 constexpr uint8_t FILE_END = -1;
43 constexpr uint8_t LOOP_END = 0;
44 int32_t g_picWidth;
45 int32_t g_picHeight;
46 int32_t g_keyWidth;
47 int32_t g_keyHeight;
48 
clearIntqueue(std::queue<uint32_t> & q)49 void clearIntqueue(std::queue<uint32_t> &q)
50 {
51     std::queue<uint32_t> empty;
52     swap(empty, q);
53 }
54 } // namespace
55 
~VEncAPI11Sample()56 VEncAPI11Sample::~VEncAPI11Sample()
57 {
58     if (SURF_INPUT && nativeWindow) {
59         OH_NativeWindow_DestroyNativeWindow(nativeWindow);
60         nativeWindow = nullptr;
61     }
62     Release();
63 }
64 
VencError(OH_AVCodec * codec,int32_t errorCode,void * userData)65 static void VencError(OH_AVCodec *codec, int32_t errorCode, void *userData)
66 {
67     cout << "Error errorCode=" << errorCode << endl;
68 }
69 
VencFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)70 static void VencFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
71 {
72     cout << "Format Changed" << endl;
73     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_WIDTH, &g_picWidth);
74     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PIC_HEIGHT, &g_picHeight);
75     OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, &g_keyWidth);
76     OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, &g_keyHeight);
77     cout << "format info: " << OH_AVFormat_DumpInfo(format) << ", OH_MD_KEY_VIDEO_PIC_WIDTH: " << g_picWidth
78     << ", OH_MD_KEY_VIDEO_PIC_HEIGHT: "<< g_picHeight << ", OH_MD_KEY_WIDTH: " << g_keyWidth
79     << ", OH_MD_KEY_HEIGHT: " << g_keyHeight << endl;
80 }
81 
onEncInputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)82 static void onEncInputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
83 {
84     VEncAPI11Signal *signal = static_cast<VEncAPI11Signal *>(userData);
85     unique_lock<mutex> lock(signal->inMutex_);
86     signal->inIdxQueue_.push(index);
87     signal->inBufferQueue_.push(buffer);
88     signal->inCond_.notify_all();
89 }
90 
onEncOutputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)91 static void onEncOutputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
92 {
93     VEncAPI11Signal *signal = static_cast<VEncAPI11Signal *>(userData);
94     unique_lock<mutex> lock(signal->outMutex_);
95     signal->outIdxQueue_.push(index);
96     signal->outBufferQueue_.push(buffer);
97     signal->outCond_.notify_all();
98 }
99 
onEncInputParam(OH_AVCodec * codec,uint32_t index,OH_AVFormat * parameter,void * userData)100 static void onEncInputParam(OH_AVCodec *codec, uint32_t index, OH_AVFormat *parameter, void *userData)
101 {
102     static bool useLtrOnce = false;
103     if (!parameter || !userData) {
104         return;
105     }
106     if (enc_sample->frameCount % enc_sample->ltrParam.ltrInterval == 0) {
107         OH_AVFormat_SetIntValue(parameter, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_MARK_LTR, 1);
108     }
109     if (!enc_sample->ltrParam.enableUseLtr) {
110         enc_sample->frameCount++;
111         OH_VideoEncoder_PushInputParameter(codec, index);
112         return;
113     }
114     static int32_t useLtrIndex = 0;
115     if (enc_sample->ltrParam.useLtrIndex == 0) {
116         useLtrIndex = LTR_INTERVAL;
117     } else if (enc_sample->ltrParam.useBadLtr) {
118         useLtrIndex = BADPOC;
119     } else {
120         uint32_t interval = enc_sample->ltrParam.ltrInterval;
121         if (interval > 0 && enc_sample->frameCount > 0 && (enc_sample->frameCount % interval == 0)) {
122             useLtrIndex = enc_sample->frameCount / interval * interval;
123         }
124     }
125     if (enc_sample->frameCount > useLtrIndex) {
126         if (!enc_sample->ltrParam.useLtrOnce) {
127             OH_AVFormat_SetIntValue(parameter, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_USE_LTR, useLtrIndex);
128         } else {
129             if (!useLtrOnce) {
130                 OH_AVFormat_SetIntValue(parameter, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_USE_LTR, useLtrIndex);
131                 useLtrOnce = true;
132             }
133         }
134     } else if (enc_sample->frameCount == useLtrIndex && enc_sample->frameCount > 0) {
135         int32_t sampleInterval = enc_sample->ltrParam.ltrInterval;
136         OH_AVFormat_SetIntValue(parameter, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_USE_LTR, useLtrIndex - sampleInterval);
137     }
138     enc_sample->frameCount++;
139     OH_VideoEncoder_PushInputParameter(codec, index);
140 }
141 
DumpLtrInfo(OH_AVBuffer * buffer)142 void VEncAPI11Sample::DumpLtrInfo(OH_AVBuffer *buffer)
143 {
144     OH_AVFormat *format = OH_AVBuffer_GetParameter(buffer);
145     int32_t isLtr = 0;
146     int32_t framePoc = 0;
147     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PER_FRAME_IS_LTR, &isLtr);
148     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_PER_FRAME_POC, &framePoc);
149 }
150 
DumpQPInfo(OH_AVBuffer * buffer)151 void VEncAPI11Sample::DumpQPInfo(OH_AVBuffer *buffer)
152 {
153     OH_AVFormat *format = OH_AVBuffer_GetParameter(buffer);
154     int32_t qp_average = 0;
155     double mse = 0;
156     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_AVERAGE, &qp_average);
157     OH_AVFormat_GetDoubleValue(format, OH_MD_KEY_VIDEO_ENCODER_MSE, &mse);
158 }
159 
DumpInfo(OH_AVCodecBufferAttr attr,OH_AVBuffer * buffer)160 void VEncAPI11Sample::DumpInfo(OH_AVCodecBufferAttr attr, OH_AVBuffer *buffer)
161 {
162     if (enableLTR && attr.flags == AVCODEC_BUFFER_FLAGS_NONE) {
163         DumpLtrInfo(buffer);
164     }
165     if (getQpMse && attr.flags == AVCODEC_BUFFER_FLAGS_NONE) {
166         DumpQPInfo(buffer);
167     }
168 }
169 
GetSystemTimeUs()170 int64_t VEncAPI11Sample::GetSystemTimeUs()
171 {
172     struct timespec now;
173     (void)clock_gettime(CLOCK_BOOTTIME, &now);
174     int64_t nanoTime = (int64_t)now.tv_sec * NANOS_IN_SECOND + now.tv_nsec;
175 
176     return nanoTime / NANOS_IN_MICRO;
177 }
178 
ConfigureVideoEncoder()179 int32_t VEncAPI11Sample::ConfigureVideoEncoder()
180 {
181     OH_AVFormat *format = OH_AVFormat_Create();
182     if (format == nullptr) {
183         cout << "Fatal: Failed to create format" << endl;
184         return AV_ERR_UNKNOWN;
185     }
186     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
187     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
188     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIX_FMT);
189     (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
190     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, DEFAULT_KEY_FRAME_INTERVAL);
191     if (isAVCEncoder) {
192         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, avcProfile);
193     } else {
194         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, hevcProfile);
195     }
196     if (configMain10) {
197         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, HEVC_PROFILE_MAIN_10);
198     } else if (configMain) {
199         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, HEVC_PROFILE_MAIN);
200     }
201     if (DEFAULT_BITRATE_MODE == CQ) {
202         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, DEFAULT_QUALITY);
203     } else {
204         (void)OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
205     }
206     if (enableQP) {
207         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MAX, DEFAULT_QP);
208         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MIN, DEFAULT_QP);
209     }
210     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, DEFAULT_BITRATE_MODE);
211     if (enableLTR && (ltrParam.ltrCount >= 0)) {
212         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_LTR_FRAME_COUNT, ltrParam.ltrCount);
213     }
214     if (enableColorSpaceParams) {
215         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_RANGE_FLAG, DEFAULT_RANGE_FLAG);
216         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_COLOR_PRIMARIES, DEFAULT_COLOR_PRIMARIES);
217         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_TRANSFER_CHARACTERISTICS, DEFAULT_TRANSFER_CHARACTERISTICS);
218         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_MATRIX_COEFFICIENTS, DEFAULT_MATRIX_COEFFICIENTS);
219     }
220     if (enableRepeat) {
221         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_REPEAT_PREVIOUS_FRAME_AFTER, DEFAULT_FRAME_AFTER);
222         if (setMaxCount) {
223             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_REPEAT_PREVIOUS_MAX_COUNT, DEFAULT_MAX_COUNT);
224         }
225     }
226     int ret = OH_VideoEncoder_Configure(venc_, format);
227     OH_AVFormat_Destroy(format);
228     return ret;
229 }
230 
ConfigureVideoEncoder_Temporal(int32_t temporal_gop_size)231 int32_t VEncAPI11Sample::ConfigureVideoEncoder_Temporal(int32_t temporal_gop_size)
232 {
233     OH_AVFormat *format = OH_AVFormat_Create();
234     if (format == nullptr) {
235         cout << "Fatal: Failed to create format" << endl;
236         return AV_ERR_UNKNOWN;
237     }
238     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, DEFAULT_WIDTH);
239     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, DEFAULT_HEIGHT);
240     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, DEFAULT_PIX_FMT);
241     (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, DEFAULT_FRAME_RATE);
242     (void)OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
243 
244     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, DEFAULT_KEY_FRAME_INTERVAL);
245 
246     if (TEMPORAL_CONFIG) {
247         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_TEMPORAL_GOP_SIZE, temporal_gop_size);
248         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_TEMPORAL_GOP_REFERENCE_MODE,
249             ADJACENT_REFERENCE);
250     }
251     if (TEMPORAL_ENABLE) {
252         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_ENABLE_TEMPORAL_SCALABILITY, 1);
253     } else {
254         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_ENABLE_TEMPORAL_SCALABILITY, 0);
255     }
256     if (TEMPORAL_JUMP_MODE) {
257         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_TEMPORAL_GOP_REFERENCE_MODE, JUMP_REFERENCE);
258     }
259     if (enableLTR && (ltrParam.ltrCount > 0)) {
260         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_LTR_FRAME_COUNT, ltrParam.ltrCount);
261     }
262     if (TEMPORAL_UNIFORMLY) {
263         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_TEMPORAL_GOP_SIZE, temporal_gop_size);
264         (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_TEMPORAL_GOP_REFERENCE_MODE,
265             UNIFORMLY_SCALED_REFERENCE);
266     }
267     int ret = OH_VideoEncoder_Configure(venc_, format);
268     OH_AVFormat_Destroy(format);
269     return ret;
270 }
271 
ConfigureVideoEncoder_fuzz(int32_t data)272 int32_t VEncAPI11Sample::ConfigureVideoEncoder_fuzz(int32_t data)
273 {
274     OH_AVFormat *format = OH_AVFormat_Create();
275     if (format == nullptr) {
276         cout << "Fatal: Failed to create format" << endl;
277         return AV_ERR_UNKNOWN;
278     }
279     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, data);
280     DEFAULT_WIDTH = data;
281     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, data);
282     DEFAULT_HEIGHT = data;
283     (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, data % MAX_PIXEL_FMT);
284     double frameRate = data;
285     (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, frameRate);
286 
287     OH_AVFormat_SetIntValue(format, OH_MD_KEY_RANGE_FLAG, data);
288     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COLOR_PRIMARIES, data);
289     OH_AVFormat_SetIntValue(format, OH_MD_KEY_TRANSFER_CHARACTERISTICS, data);
290     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MATRIX_COEFFICIENTS, data);
291     OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, data);
292     OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, data);
293     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, data);
294     OH_AVFormat_SetIntValue(format, OH_MD_KEY_QUALITY, data);
295 
296     int ret = OH_VideoEncoder_Configure(venc_, format);
297     OH_AVFormat_Destroy(format);
298     return ret;
299 }
300 
SetVideoEncoderCallback()301 int32_t VEncAPI11Sample::SetVideoEncoderCallback()
302 {
303     signal_ = new VEncAPI11Signal();
304     if (signal_ == nullptr) {
305         cout << "Failed to new VEncAPI11Signal" << endl;
306         return AV_ERR_UNKNOWN;
307     }
308     if (SURF_INPUT) {
309         int32_t ret = OH_VideoEncoder_RegisterParameterCallback(venc_, onEncInputParam, static_cast<void *>(this));
310         if (ret != AV_ERR_OK) {
311             return ret;
312         }
313     }
314     cb_.onError = VencError;
315     cb_.onStreamChanged = VencFormatChanged;
316     cb_.onNeedInputBuffer = onEncInputBufferAvailable;
317     cb_.onNewOutputBuffer = onEncOutputBufferAvailable;
318 
319     return OH_VideoEncoder_RegisterCallback(venc_, cb_, static_cast<void *>(signal_));
320 }
321 
state_EOS()322 int32_t VEncAPI11Sample::state_EOS()
323 {
324     unique_lock<mutex> lock(signal_->inMutex_);
325     signal_->inCond_.wait(lock, [this]() { return signal_->inIdxQueue_.size() > 0; });
326     uint32_t index = signal_->inIdxQueue_.front();
327     OH_AVBuffer *buffer = signal_->inBufferQueue_.front();
328     signal_->inIdxQueue_.pop();
329     signal_->inBufferQueue_.pop();
330     lock.unlock();
331     OH_AVCodecBufferAttr attr;
332     attr.pts = 0;
333     attr.size = 0;
334     attr.offset = 0;
335     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
336     OH_AVBuffer_SetBufferAttr(buffer, &attr);
337     return OH_VideoEncoder_PushInputBuffer(venc_, index);
338 }
ReleaseInFile()339 void VEncAPI11Sample::ReleaseInFile()
340 {
341     if (inFile_ != nullptr) {
342         if (inFile_->is_open()) {
343             inFile_->close();
344         }
345         inFile_.reset();
346         inFile_ = nullptr;
347     }
348 }
349 
StopInloop()350 void VEncAPI11Sample::StopInloop()
351 {
352     if (inputLoop_ != nullptr && inputLoop_->joinable()) {
353         unique_lock<mutex> lock(signal_->inMutex_);
354         clearIntqueue(signal_->inIdxQueue_);
355         isRunning_.store(false);
356         signal_->inCond_.notify_all();
357         lock.unlock();
358 
359         inputLoop_->join();
360         inputLoop_ = nullptr;
361     }
362 }
363 
testApi()364 void VEncAPI11Sample::testApi()
365 {
366     OH_VideoEncoder_GetSurface(venc_, &nativeWindow);
367     OH_VideoEncoder_Prepare(venc_);
368     OH_VideoEncoder_GetInputDescription(venc_);
369     OH_VideoEncoder_Start(venc_);
370     OH_AVFormat *format = OH_AVFormat_Create();
371     OH_AVFormat_SetIntValue(format, OH_MD_KEY_REQUEST_I_FRAME, 1);
372     OH_VideoEncoder_SetParameter(venc_, format);
373     OH_VideoEncoder_NotifyEndOfStream(venc_);
374     OH_VideoEncoder_GetOutputDescription(venc_);
375     OH_AVFormat_Destroy(format);
376     OH_VideoEncoder_Flush(venc_);
377     bool isValid = false;
378     OH_VideoEncoder_IsValid(venc_, &isValid);
379     OH_VideoEncoder_Stop(venc_);
380     OH_VideoEncoder_Reset(venc_);
381 }
382 
CreateSurface()383 int32_t VEncAPI11Sample::CreateSurface()
384 {
385     int32_t ret = 0;
386     ret = OH_VideoEncoder_GetSurface(venc_, &nativeWindow);
387     if (ret != AV_ERR_OK) {
388         cout << "OH_VideoEncoder_GetSurface fail" << endl;
389         return ret;
390     }
391     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_YCBCR_420_SP);
392     if (ret != AV_ERR_OK) {
393         cout << "NativeWindowHandleOpt SET_FORMAT fail" << endl;
394         return ret;
395     }
396     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
397     if (ret != AV_ERR_OK) {
398         cout << "NativeWindowHandleOpt SET_BUFFER_GEOMETRY fail" << endl;
399         return ret;
400     }
401     return AV_ERR_OK;
402 }
403 
GetStride()404 void VEncAPI11Sample::GetStride()
405 {
406     OH_AVFormat *format = OH_VideoEncoder_GetInputDescription(venc_);
407     int32_t inputStride = 0;
408     OH_AVFormat_GetIntValue(format, OH_MD_KEY_VIDEO_STRIDE, &inputStride);
409     stride_ = inputStride;
410     OH_AVFormat_Destroy(format);
411 }
412 
OpenFile()413 int32_t VEncAPI11Sample::OpenFile()
414 {
415     int32_t ret = AV_ERR_OK;
416     inFile_ = make_unique<ifstream>();
417     if (inFile_ == nullptr) {
418         isRunning_.store(false);
419         (void)OH_VideoEncoder_Stop(venc_);
420         return AV_ERR_UNKNOWN;
421     }
422     inFile_->open(INP_DIR, ios::in | ios::binary);
423     if (!inFile_->is_open()) {
424         ret = OpenFileFail();
425     }
426     return ret;
427 }
428 
StartVideoEncoder()429 int32_t VEncAPI11Sample::StartVideoEncoder()
430 {
431     isRunning_.store(true);
432     int32_t ret = 0;
433     if (SURF_INPUT) {
434         ret = CreateSurface();
435         if (ret != AV_ERR_OK) {
436             return ret;
437         }
438     }
439     ret = OH_VideoEncoder_Start(venc_);
440     GetStride();
441     if (ret != AV_ERR_OK) {
442         cout << "Failed to start codec" << endl;
443         isRunning_.store(false);
444         signal_->inCond_.notify_all();
445         signal_->outCond_.notify_all();
446         return ret;
447     }
448     inFile_ = make_unique<ifstream>();
449     if (inFile_ == nullptr) {
450         isRunning_.store(false);
451         (void)OH_VideoEncoder_Stop(venc_);
452         return AV_ERR_UNKNOWN;
453     }
454     readMultiFilesFunc();
455     if (SURF_INPUT) {
456         inputLoop_ = make_unique<thread>(&VEncAPI11Sample::InputFuncSurface, this);
457     } else {
458         inputLoop_ = make_unique<thread>(&VEncAPI11Sample::InputFunc, this);
459     }
460     if (inputLoop_ == nullptr) {
461         isRunning_.store(false);
462         (void)OH_VideoEncoder_Stop(venc_);
463         ReleaseInFile();
464         return AV_ERR_UNKNOWN;
465     }
466     outputLoop_ = make_unique<thread>(&VEncAPI11Sample::OutputFunc, this);
467     if (outputLoop_ == nullptr) {
468         isRunning_.store(false);
469         (void)OH_VideoEncoder_Stop(venc_);
470         ReleaseInFile();
471         StopInloop();
472         Release();
473         return AV_ERR_UNKNOWN;
474     }
475     return AV_ERR_OK;
476 }
477 
readMultiFilesFunc()478 void VEncAPI11Sample::readMultiFilesFunc()
479 {
480     if (!readMultiFiles) {
481         inFile_->open(INP_DIR, ios::in | ios::binary);
482         if (!inFile_->is_open()) {
483             OpenFileFail();
484         }
485     }
486 }
487 
CreateVideoEncoder(const char * codecName)488 int32_t VEncAPI11Sample::CreateVideoEncoder(const char *codecName)
489 {
490     cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_AVC, true, HARDWARE);
491     const char *tmpCodecName = OH_AVCapability_GetName(cap);
492     if (!strcmp(codecName, tmpCodecName)) {
493         isAVCEncoder = true;
494     } else {
495         isAVCEncoder = false;
496     }
497     venc_ = OH_VideoEncoder_CreateByName(codecName);
498     enc_sample = this;
499     return venc_ == nullptr ? AV_ERR_UNKNOWN : AV_ERR_OK;
500 }
501 
WaitForEOS()502 void VEncAPI11Sample::WaitForEOS()
503 {
504     if (inputLoop_)
505         inputLoop_->join();
506     if (outputLoop_)
507         outputLoop_->join();
508     inputLoop_ = nullptr;
509     outputLoop_ = nullptr;
510 }
511 
ReturnZeroIfEOS(uint32_t expectedSize)512 uint32_t VEncAPI11Sample::ReturnZeroIfEOS(uint32_t expectedSize)
513 {
514     if (inFile_->gcount() != (expectedSize)) {
515         cout << "no more data" << endl;
516         return 0;
517     }
518     return 1;
519 }
520 
ReadOneFrameYUV420SP(uint8_t * dst)521 uint32_t VEncAPI11Sample::ReadOneFrameYUV420SP(uint8_t *dst)
522 {
523     uint8_t *start = dst;
524     // copy Y
525     for (uint32_t i = 0; i < DEFAULT_HEIGHT; i++) {
526         inFile_->read(reinterpret_cast<char *>(dst), DEFAULT_WIDTH);
527         if (!ReturnZeroIfEOS(DEFAULT_WIDTH))
528             return 0;
529         dst += stride_;
530     }
531     // copy UV
532     for (uint32_t i = 0; i < DEFAULT_HEIGHT / SAMPLE_RATIO; i++) {
533         inFile_->read(reinterpret_cast<char *>(dst), DEFAULT_WIDTH);
534         if (!ReturnZeroIfEOS(DEFAULT_WIDTH))
535             return 0;
536         dst += stride_;
537     }
538     return dst - start;
539 }
540 
ReadOneFrameYUVP010(uint8_t * dst)541 uint32_t VEncAPI11Sample::ReadOneFrameYUVP010(uint8_t *dst)
542 {
543     uint8_t *start = dst;
544     int32_t num = 2;
545     // copy Y
546     for (uint32_t i = 0; i < DEFAULT_HEIGHT; i++) {
547         inFile_->read(reinterpret_cast<char *>(dst), DEFAULT_WIDTH*num);
548         if (!ReturnZeroIfEOS(DEFAULT_WIDTH*num))
549             return 0;
550         dst += stride_;
551     }
552     // copy UV
553     for (uint32_t i = 0; i < DEFAULT_HEIGHT / SAMPLE_RATIO; i++) {
554         inFile_->read(reinterpret_cast<char *>(dst), DEFAULT_WIDTH*num);
555         if (!ReturnZeroIfEOS(DEFAULT_WIDTH*num))
556             return 0;
557         dst += stride_;
558     }
559     return dst - start;
560 }
561 
ReadOneFrameRGBA8888(uint8_t * dst)562 uint32_t VEncAPI11Sample::ReadOneFrameRGBA8888(uint8_t *dst)
563 {
564     uint8_t *start = dst;
565     for (uint32_t i = 0; i < DEFAULT_HEIGHT; i++) {
566         inFile_->read(reinterpret_cast<char *>(dst), DEFAULT_WIDTH * RGBA_SIZE);
567         if (inFile_->eof())
568             return 0;
569         dst += stride_;
570     }
571     return dst - start;
572 }
573 
FlushSurf(OHNativeWindowBuffer * ohNativeWindowBuffer,OH_NativeBuffer * nativeBuffer)574 uint32_t VEncAPI11Sample::FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer, OH_NativeBuffer *nativeBuffer)
575 {
576     int32_t ret = 0;
577     struct Region region;
578     struct Region::Rect *rect = new Region::Rect();
579     rect->x = 0;
580     rect->y = 0;
581     rect->w = DEFAULT_WIDTH;
582     rect->h = DEFAULT_HEIGHT;
583     region.rects = rect;
584     NativeWindowHandleOpt(nativeWindow, SET_UI_TIMESTAMP, GetSystemTimeUs());
585     ret = OH_NativeBuffer_Unmap(nativeBuffer);
586     if (ret != 0) {
587         cout << "OH_NativeBuffer_Unmap failed" << endl;
588         delete rect;
589         return ret;
590     }
591     ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, ohNativeWindowBuffer, -1, region);
592     delete rect;
593     if (ret != 0) {
594         cout << "FlushBuffer failed" << endl;
595         return ret;
596     }
597     return ret;
598 }
599 
InputFuncSurface()600 void VEncAPI11Sample::InputFuncSurface()
601 {
602     int32_t readFileIndex = 0;
603     while (true) {
604         OHNativeWindowBuffer *ohNativeWindowBuffer;
605         OH_NativeBuffer *nativeBuffer = nullptr;
606         uint8_t *dst = nullptr;
607         int err = InitBuffer(ohNativeWindowBuffer, nativeBuffer, dst);
608         if (err == 0) {
609             break;
610         } else if (err == -1) {
611             continue;
612         }
613         if (readMultiFiles) {
614             err = ReadOneFrameFromList(dst, readFileIndex);
615             if (err == LOOP_END) {
616                 break;
617             } else if (err == FILE_END) {
618                 OH_NativeWindow_NativeWindowAbortBuffer(nativeWindow, ohNativeWindowBuffer);
619                 cout << "OH_NativeWindow_NativeWindowAbortBuffer" << endl;
620                 continue;
621             }
622         } else if (!ReadOneFrameYUV420SP(dst)) {
623             err = OH_VideoEncoder_NotifyEndOfStream(venc_);
624             if (err != 0) {
625                 cout << "OH_VideoEncoder_NotifyEndOfStream failed" << endl;
626             }
627             break;
628         }
629         err = FlushSurf(ohNativeWindowBuffer, nativeBuffer);
630         if (err != 0) {
631             break;
632         }
633         usleep(FRAME_INTERVAL);
634     }
635 }
636 
InitBuffer(OHNativeWindowBuffer * & ohNativeWindowBuffer,OH_NativeBuffer * & nativeBuffer,uint8_t * & dst)637 int32_t VEncAPI11Sample::InitBuffer(OHNativeWindowBuffer *&ohNativeWindowBuffer,
638     OH_NativeBuffer *&nativeBuffer, uint8_t *&dst)
639 {
640     int fenceFd = -1;
641     if (nativeWindow == nullptr) {
642         return 0;
643     }
644     int32_t err = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &ohNativeWindowBuffer, &fenceFd);
645     if (err != 0) {
646         cout << "RequestBuffer failed, GSError=" << err << endl;
647         return -1;
648     }
649     if (fenceFd > 0) {
650         close(fenceFd);
651     }
652     nativeBuffer = OH_NativeBufferFromNativeWindowBuffer(ohNativeWindowBuffer);
653     void *virAddr = nullptr;
654     err = OH_NativeBuffer_Map(nativeBuffer, &virAddr);
655     if (err != 0) {
656         cout << "OH_NativeBuffer_Map failed, GSError=" << err << endl;
657         isRunning_.store(false);
658         return 0;
659     }
660     dst = (uint8_t *)virAddr;
661     const SurfaceBuffer *sbuffer = SurfaceBuffer::NativeBufferToSurfaceBuffer(nativeBuffer);
662     int32_t stride = sbuffer->GetStride();
663     if (dst == nullptr || stride < (int32_t)DEFAULT_WIDTH) {
664         cout << "invalid va or stride=" << stride << endl;
665         err = NativeWindowCancelBuffer(nativeWindow, ohNativeWindowBuffer);
666         isRunning_.store(false);
667         return 0;
668     }
669     stride_ = stride;
670     return 1;
671 }
672 
ReadOneFrameFromList(uint8_t * dst,int32_t & index)673 uint32_t VEncAPI11Sample::ReadOneFrameFromList(uint8_t *dst, int32_t &index)
674 {
675     int32_t ret = 0;
676     if (index >= fileInfos.size()) {
677         ret = OH_VideoEncoder_NotifyEndOfStream(venc_);
678         if (ret != 0) {
679             cout << "OH_VideoEncoder_NotifyEndOfStream failed" << endl;
680         }
681         return LOOP_END;
682     }
683     if (!inFile_->is_open()) {
684         inFile_->open(fileInfos[index].fileDir);
685         if (!inFile_->is_open()) {
686             return OpenFileFail();
687         }
688         DEFAULT_WIDTH = fileInfos[index].width;
689         DEFAULT_HEIGHT = fileInfos[index].height;
690         if (setFormatRbgx) {
691             ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_RGBX_8888);
692         } else if (setFormat8Bit) {
693             ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_YCBCR_420_SP);
694         } else if (setFormat10Bit) {
695             ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_YCBCR_P010);
696         } else {
697             ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, fileInfos[index].format);
698         }
699         if (ret != AV_ERR_OK) {
700             return ret;
701         }
702         ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
703         if (ret != AV_ERR_OK) {
704             return ret;
705         }
706         cout << fileInfos[index].fileDir << endl;
707         cout << "set width:" << fileInfos[index].width << "height: " << fileInfos[index].height << endl;
708         return FILE_END;
709     }
710     ret = ReadOneFrameByType(dst, fileInfos[index].format);
711     if (!ret) {
712         if (inFile_->is_open()) {
713             inFile_->close();
714         }
715         index++;
716         if (index >= fileInfos.size()) {
717             OH_VideoEncoder_NotifyEndOfStream(venc_);
718             return LOOP_END;
719         }
720         return FILE_END;
721     }
722     return ret;
723 }
724 
ReadOneFrameByType(uint8_t * dst,OH_NativeBuffer_Format format)725 uint32_t VEncAPI11Sample::ReadOneFrameByType(uint8_t *dst, OH_NativeBuffer_Format format)
726 {
727     if (format == NATIVEBUFFER_PIXEL_FMT_RGBA_8888) {
728         return ReadOneFrameRGBA8888(dst);
729     } else if (format == NATIVEBUFFER_PIXEL_FMT_YCBCR_420_SP || format == NATIVEBUFFER_PIXEL_FMT_YCRCB_420_SP) {
730         return ReadOneFrameYUV420SP(dst);
731     } else if (format == NATIVEBUFFER_PIXEL_FMT_YCBCR_P010) {
732         return ReadOneFrameYUVP010(dst);
733     } else {
734         cout << "error fileType" << endl;
735         return 0;
736     }
737 }
738 
OpenFileFail()739 int32_t VEncAPI11Sample::OpenFileFail()
740 {
741     cout << "file open fail" << endl;
742     isRunning_.store(false);
743     (void)OH_VideoEncoder_Stop(venc_);
744     inFile_->close();
745     inFile_.reset();
746     inFile_ = nullptr;
747     return AV_ERR_UNKNOWN;
748 }
749 
Flush_buffer()750 void VEncAPI11Sample::Flush_buffer()
751 {
752     unique_lock<mutex> inLock(signal_->inMutex_);
753     clearIntqueue(signal_->inIdxQueue_);
754     std::queue<OH_AVBuffer *> empty;
755     swap(empty, signal_->inBufferQueue_);
756     signal_->inCond_.notify_all();
757     inLock.unlock();
758     unique_lock<mutex> outLock(signal_->outMutex_);
759     clearIntqueue(signal_->outIdxQueue_);
760     signal_->outCond_.notify_all();
761     outLock.unlock();
762 }
763 
RepeatStartBeforeEOS()764 void VEncAPI11Sample::RepeatStartBeforeEOS()
765 {
766     if (REPEAT_START_FLUSH_BEFORE_EOS > 0) {
767         REPEAT_START_FLUSH_BEFORE_EOS--;
768         OH_VideoEncoder_Flush(venc_);
769         Flush_buffer();
770         OH_VideoEncoder_Start(venc_);
771     }
772 
773     if (REPEAT_START_STOP_BEFORE_EOS > 0) {
774         REPEAT_START_STOP_BEFORE_EOS--;
775         OH_VideoEncoder_Stop(venc_);
776         Flush_buffer();
777         OH_VideoEncoder_Start(venc_);
778     }
779 }
780 
RandomEOS(uint32_t index)781 bool VEncAPI11Sample::RandomEOS(uint32_t index)
782 {
783     uint32_t random_eos = rand() % 25;
784     if (enable_random_eos && random_eos == frameCount) {
785         OH_VideoEncoder_NotifyEndOfStream(venc_);
786         cout << "random eos" << endl;
787         frameCount++;
788         unique_lock<mutex> lock(signal_->inMutex_);
789         signal_->inIdxQueue_.pop();
790         signal_->inBufferQueue_.pop();
791         return true;
792     }
793     return false;
794 }
795 
AutoSwitchParam()796 void VEncAPI11Sample::AutoSwitchParam()
797 {
798     int64_t currentBitrate = DEFAULT_BITRATE;
799     double currentFrameRate = DEFAULT_FRAME_RATE;
800     int32_t currentQP = DEFAULT_QP;
801     if (frameCount == switchParamsTimeSec * (int32_t)DEFAULT_FRAME_RATE) {
802         OH_AVFormat *format = OH_AVFormat_Create();
803         if (needResetBitrate) {
804             currentBitrate = DEFAULT_BITRATE >> 1;
805             (void)OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, currentBitrate);
806         }
807         if (needResetFrameRate) {
808             currentFrameRate = DEFAULT_FRAME_RATE * DOUBLE;
809             (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, currentFrameRate);
810         }
811         if (needResetQP) {
812             currentQP = DEFAULT_QP;
813             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MAX, currentQP);
814             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MIN, currentQP);
815         }
816         SetParameter(format) == AV_ERR_OK ? (0) : (errCount++);
817         OH_AVFormat_Destroy(format);
818     }
819     if (frameCount == switchParamsTimeSec * (int32_t)DEFAULT_FRAME_RATE * DOUBLE) {
820         OH_AVFormat *format = OH_AVFormat_Create();
821         if (needResetBitrate) {
822             currentBitrate = DEFAULT_BITRATE << 1;
823             cout<<"switch bitrate "<< currentBitrate;
824             (void)OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, currentBitrate);
825         }
826         if (needResetFrameRate) {
827             currentFrameRate = DEFAULT_FRAME_RATE / DOUBLE;
828             cout<< "switch framerate" << currentFrameRate << endl;
829             (void)OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, currentFrameRate);
830         }
831         if (needResetQP) {
832             currentQP = DEFAULT_QP * DOUBLE;
833             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MAX, currentQP);
834             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MIN, currentQP);
835         }
836         SetParameter(format) == AV_ERR_OK ? (0) : (errCount++);
837         OH_AVFormat_Destroy(format);
838     }
839 }
840 
SetEOS(uint32_t index,OH_AVBuffer * buffer)841 void VEncAPI11Sample::SetEOS(uint32_t index, OH_AVBuffer *buffer)
842 {
843     OH_AVCodecBufferAttr attr;
844     attr.pts = 0;
845     attr.size = 0;
846     attr.offset = 0;
847     attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
848     OH_AVBuffer_SetBufferAttr(buffer, &attr);
849     int32_t res = OH_VideoEncoder_PushInputBuffer(venc_, index);
850     cout << "OH_VideoEncoder_PushInputBuffer    EOS   res: " << res << endl;
851     unique_lock<mutex> lock(signal_->inMutex_);
852     signal_->inIdxQueue_.pop();
853     signal_->inBufferQueue_.pop();
854 }
855 
SetForceIDR()856 void VEncAPI11Sample::SetForceIDR()
857 {
858     OH_AVFormat *format = OH_AVFormat_Create();
859     OH_AVFormat_SetIntValue(format, OH_MD_KEY_REQUEST_I_FRAME, 1);
860     OH_VideoEncoder_SetParameter(venc_, format);
861     OH_AVFormat_Destroy(format);
862 }
863 
SetLTRParameter(OH_AVBuffer * buffer)864 void VEncAPI11Sample::SetLTRParameter(OH_AVBuffer *buffer)
865 {
866     if (!ltrParam.enableUseLtr) {
867         return;
868     }
869     OH_AVFormat *format = OH_AVFormat_Create();
870     if (frameCount % ltrParam.ltrInterval == 0) {
871         OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_MARK_LTR, 1);
872     }
873     if (!ltrParam.enableUseLtr) {
874         OH_AVBuffer_SetParameter(buffer, format) == AV_ERR_OK ? (0) : (errCount++);
875         OH_AVFormat_Destroy(format);
876         return;
877     }
878     static int32_t useLtrIndex = 0;
879     if (ltrParam.useLtrIndex == 0) {
880         useLtrIndex = LTR_INTERVAL;
881     } else if (ltrParam.useBadLtr) {
882         useLtrIndex = BADPOC;
883     } else {
884         uint32_t interval = ltrParam.ltrInterval;
885         if (interval > 0 && frameCount > 0 && (frameCount % interval == 0)) {
886             useLtrIndex = frameCount / interval * interval;
887         }
888     }
889     if (frameCount > useLtrIndex) {
890         if (!ltrParam.useLtrOnce) {
891             OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_USE_LTR, useLtrIndex);
892         } else {
893             OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_USE_LTR, useLtrIndex);
894             ltrParam.useLtrOnce = true;
895         }
896     } else if (frameCount == useLtrIndex && frameCount > 0) {
897         OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_PER_FRAME_USE_LTR, useLtrIndex - ltrParam.ltrInterval);
898     }
899     OH_AVBuffer_SetParameter(buffer, format) == AV_ERR_OK ? (0) : (errCount++);
900     OH_AVFormat_Destroy(format);
901 }
902 
SetBufferParameter(OH_AVBuffer * buffer)903 void VEncAPI11Sample::SetBufferParameter(OH_AVBuffer *buffer)
904 {
905     int32_t currentQP = DEFAULT_QP;
906     if (!enableAutoSwitchBufferParam) {
907         return;
908     }
909     if (frameCount == switchParamsTimeSec * (int32_t)DEFAULT_FRAME_RATE) {
910         OH_AVFormat *format = OH_AVFormat_Create();
911         if (needResetQP) {
912             currentQP = DEFAULT_QP;
913             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MAX, currentQP);
914             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MIN, currentQP);
915         }
916         OH_AVBuffer_SetParameter(buffer, format) == AV_ERR_OK ? (0) : (errCount++);
917         OH_AVFormat_Destroy(format);
918     }
919     if (frameCount == switchParamsTimeSec * (int32_t)DEFAULT_FRAME_RATE * DOUBLE) {
920         OH_AVFormat *format = OH_AVFormat_Create();
921         if (needResetQP) {
922             currentQP = DEFAULT_QP * DOUBLE;
923             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MAX, currentQP);
924             (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODER_QP_MIN, currentQP);
925         }
926         OH_AVBuffer_SetParameter(buffer, format) == AV_ERR_OK ? (0) : (errCount++);
927         OH_AVFormat_Destroy(format);
928     }
929 }
930 
PushData(OH_AVBuffer * buffer,uint32_t index,int32_t & result)931 int32_t VEncAPI11Sample::PushData(OH_AVBuffer *buffer, uint32_t index, int32_t &result)
932 {
933     int32_t res = -2;
934     OH_AVCodecBufferAttr attr;
935     uint8_t *fileBuffer = OH_AVBuffer_GetAddr(buffer);
936     if (fileBuffer == nullptr) {
937         cout << "Fatal: no memory" << endl;
938         return -1;
939     }
940     int32_t size = OH_AVBuffer_GetCapacity(buffer);
941     if (DEFAULT_PIX_FMT == AV_PIXEL_FORMAT_RGBA) {
942         if (size < DEFAULT_HEIGHT * stride_) {
943             return -1;
944         }
945         attr.size = ReadOneFrameRGBA8888(fileBuffer);
946     } else {
947         if (size < (DEFAULT_HEIGHT * stride_ + (DEFAULT_HEIGHT * stride_ / DOUBLE))) {
948             return -1;
949         }
950         attr.size = ReadOneFrameYUV420SP(fileBuffer);
951     }
952     if (repeatRun && inFile_->eof()) {
953         inFile_->clear();
954         inFile_->seekg(0, ios::beg);
955         encode_count++;
956         cout << "repeat"<< "   encode_count:" << encode_count << endl;
957         return -1;
958     }
959     if (inFile_->eof()) {
960         SetEOS(index, buffer);
961         return 0;
962     }
963     attr.pts = GetSystemTimeUs();
964     attr.offset = 0;
965     attr.flags = AVCODEC_BUFFER_FLAGS_NONE;
966     if (enableForceIDR && (frameCount % IDR_FRAME_INTERVAL == 0)) {
967         SetForceIDR();
968     }
969     OH_AVBuffer_SetBufferAttr(buffer, &attr);
970     SetBufferParameter(buffer);
971     SetLTRParameter(buffer);
972     result = OH_VideoEncoder_PushInputBuffer(venc_, index);
973     frameCount++;
974     unique_lock<mutex> lock(signal_->inMutex_);
975     signal_->inIdxQueue_.pop();
976     signal_->inBufferQueue_.pop();
977     return res;
978 }
979 
CheckResult(bool isRandomEosSuccess,int32_t pushResult)980 int32_t VEncAPI11Sample::CheckResult(bool isRandomEosSuccess, int32_t pushResult)
981 {
982     if (isRandomEosSuccess) {
983         if (pushResult == 0) {
984             errCount = errCount + 1;
985             cout << "push input after eos should be failed!  pushResult:" << pushResult << endl;
986         }
987         return -1;
988     } else if (pushResult != 0) {
989         errCount = errCount + 1;
990         cout << "push input data failed, error:" << pushResult << endl;
991         return -1;
992     }
993     return 0;
994 }
995 
InputFunc()996 void VEncAPI11Sample::InputFunc()
997 {
998     errCount = 0;
999     while (true) {
1000         if (!isRunning_.load()) {
1001             break;
1002         }
1003         RepeatStartBeforeEOS();
1004         unique_lock<mutex> lock(signal_->inMutex_);
1005         signal_->inCond_.wait(lock, [this]() {
1006             if (!isRunning_.load()) {
1007                 return true;
1008             }
1009             return signal_->inIdxQueue_.size() > 0;
1010         });
1011         if (!isRunning_.load()) {
1012             break;
1013         }
1014         uint32_t index = signal_->inIdxQueue_.front();
1015         auto buffer = signal_->inBufferQueue_.front();
1016         lock.unlock();
1017         if (!inFile_->eof()) {
1018             bool isRandomEosSuccess = RandomEOS(index);
1019             if (isRandomEosSuccess) {
1020                 continue;
1021             }
1022             int32_t pushResult = 0;
1023             int32_t ret = PushData(buffer, index, pushResult);
1024             if (ret == 0) {
1025                 break;
1026             } else if (ret == -1) {
1027                 continue;
1028             }
1029             if (CheckResult(isRandomEosSuccess, pushResult) == -1) {
1030                 break;
1031             }
1032             if (enableAutoSwitchParam) {
1033                 AutoSwitchParam();
1034             }
1035         }
1036         if (sleepOnFPS) {
1037             usleep(FRAME_INTERVAL);
1038         }
1039     }
1040 }
1041 
CheckAttrFlag(OH_AVCodecBufferAttr attr)1042 int32_t VEncAPI11Sample::CheckAttrFlag(OH_AVCodecBufferAttr attr)
1043 {
1044     if (attr.flags & AVCODEC_BUFFER_FLAGS_EOS) {
1045         cout << "attr.flags == AVCODEC_BUFFER_FLAGS_EOS" << endl;
1046         unique_lock<mutex> inLock(signal_->inMutex_);
1047         isRunning_.store(false);
1048         signal_->inCond_.notify_all();
1049         signal_->outCond_.notify_all();
1050         inLock.unlock();
1051         return -1;
1052     }
1053     if (attr.flags == AVCODEC_BUFFER_FLAGS_CODEC_DATA) {
1054         cout << "enc AVCODEC_BUFFER_FLAGS_CODEC_DATA" << attr.pts << endl;
1055         return 0;
1056     }
1057     outCount = outCount + 1;
1058     return 0;
1059 }
1060 
OutputFuncFail()1061 void VEncAPI11Sample::OutputFuncFail()
1062 {
1063     cout << "errCount > 0" << endl;
1064     unique_lock<mutex> inLock(signal_->inMutex_);
1065     isRunning_.store(false);
1066     signal_->inCond_.notify_all();
1067     signal_->outCond_.notify_all();
1068     inLock.unlock();
1069     (void)Stop();
1070     Release();
1071 }
1072 
OutputFunc()1073 void VEncAPI11Sample::OutputFunc()
1074 {
1075     FILE *outFile = fopen(OUT_DIR, "wb");
1076     while (true) {
1077         if (!isRunning_.load()) {
1078             break;
1079         }
1080         OH_AVCodecBufferAttr attr;
1081         unique_lock<mutex> lock(signal_->outMutex_);
1082         signal_->outCond_.wait(lock, [this]() {
1083             if (!isRunning_.load()) {
1084                 return true;
1085             }
1086             return signal_->outIdxQueue_.size() > 0;
1087         });
1088         if (!isRunning_.load()) {
1089             break;
1090         }
1091         uint32_t index = signal_->outIdxQueue_.front();
1092         OH_AVBuffer *buffer = signal_->outBufferQueue_.front();
1093         signal_->outBufferQueue_.pop();
1094         signal_->outIdxQueue_.pop();
1095         lock.unlock();
1096         DumpInfo(attr, buffer);
1097         if (OH_AVBuffer_GetBufferAttr(buffer, &attr) != AV_ERR_OK) {
1098             errCount = errCount + 1;
1099         }
1100         if (CheckAttrFlag(attr) == -1) {
1101             break;
1102         }
1103         int size = attr.size;
1104         if (outFile == nullptr) {
1105             cout << "dump data fail" << endl;
1106         } else {
1107             fwrite(OH_AVBuffer_GetAddr(buffer), 1, size, outFile);
1108         }
1109         if (OH_VideoEncoder_FreeOutputBuffer(venc_, index) != AV_ERR_OK) {
1110             cout << "Fatal: ReleaseOutputBuffer fail" << endl;
1111             errCount = errCount + 1;
1112         }
1113         if (errCount > 0) {
1114             OutputFuncFail();
1115             break;
1116         }
1117     }
1118     if (outFile) {
1119         (void)fclose(outFile);
1120     }
1121 }
1122 
Flush()1123 int32_t VEncAPI11Sample::Flush()
1124 {
1125     unique_lock<mutex> inLock(signal_->inMutex_);
1126     clearIntqueue(signal_->inIdxQueue_);
1127     signal_->inCond_.notify_all();
1128     inLock.unlock();
1129     unique_lock<mutex> outLock(signal_->outMutex_);
1130     clearIntqueue(signal_->outIdxQueue_);
1131     signal_->outCond_.notify_all();
1132     outLock.unlock();
1133     return OH_VideoEncoder_Flush(venc_);
1134 }
1135 
Reset()1136 int32_t VEncAPI11Sample::Reset()
1137 {
1138     isRunning_.store(false);
1139     StopInloop();
1140     StopOutloop();
1141     ReleaseInFile();
1142     return OH_VideoEncoder_Reset(venc_);
1143 }
1144 
Release()1145 int32_t VEncAPI11Sample::Release()
1146 {
1147     int ret = OH_VideoEncoder_Destroy(venc_);
1148     venc_ = nullptr;
1149     if (signal_ != nullptr) {
1150         delete signal_;
1151         signal_ = nullptr;
1152     }
1153     return ret;
1154 }
1155 
Stop()1156 int32_t VEncAPI11Sample::Stop()
1157 {
1158     StopInloop();
1159     clearIntqueue(signal_->outIdxQueue_);
1160     ReleaseInFile();
1161     return OH_VideoEncoder_Stop(venc_);
1162 }
1163 
Start()1164 int32_t VEncAPI11Sample::Start()
1165 {
1166     return OH_VideoEncoder_Start(venc_);
1167 }
1168 
StopOutloop()1169 void VEncAPI11Sample::StopOutloop()
1170 {
1171     if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1172         unique_lock<mutex> lock(signal_->outMutex_);
1173         clearIntqueue(signal_->outIdxQueue_);
1174         signal_->outCond_.notify_all();
1175         lock.unlock();
1176     }
1177 }
1178 
SetParameter(OH_AVFormat * format)1179 int32_t VEncAPI11Sample::SetParameter(OH_AVFormat *format)
1180 {
1181     if (venc_) {
1182         return OH_VideoEncoder_SetParameter(venc_, format);
1183     }
1184     return AV_ERR_UNKNOWN;
1185 }