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 #define LOG_TAG "detailEnh"
17
18 #include "detailEnh_sample.h"
19
20 #include <fstream>
21 #include <string>
22 #include <vector>
23 #include <memory>
24 #include <cstdlib>
25
26 #include "securec.h"
27
28 namespace OHOS {
29 namespace Media {
30 namespace VideoProcessingEngine {
ReadYuvFile(sptr<SurfaceBuffer> & buffer,std::unique_ptr<std::ifstream> & yuvFile,int32_t frameSize)31 void ReadYuvFile(sptr<SurfaceBuffer> &buffer, std::unique_ptr<std::ifstream> &yuvFile, int32_t frameSize)
32 {
33 if (buffer == nullptr) {
34 TEST_LOG("null ptr");
35 return;
36 }
37 if (frameSize < 0) {
38 TEST_LOG("Invalid size");
39 return;
40 }
41 if (!yuvFile->is_open()) {
42 TEST_LOG("Yuv file is not open");
43 return;
44 }
45 yuvFile->read(reinterpret_cast<char *>(buffer->GetVirAddr()), frameSize);
46 }
47
CreateSurfaceBuffer(uint32_t pixelFormat,int32_t width,int32_t height)48 sptr<SurfaceBuffer> CreateSurfaceBuffer(uint32_t pixelFormat, int32_t width, int32_t height)
49 {
50 auto buffer = SurfaceBuffer::Create();
51 if (buffer == nullptr) {
52 TEST_LOG("Create surface buffer failed");
53 return nullptr;
54 }
55 if (width <= 0 || height <= 0) {
56 TEST_LOG("Invalid resolution");
57 return nullptr;
58 }
59 BufferRequestConfig inputCfg;
60 inputCfg.width = width;
61 inputCfg.height = height;
62 inputCfg.strideAlignment = width;
63 inputCfg.usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE
64 | BUFFER_USAGE_HW_RENDER | BUFFER_USAGE_HW_TEXTURE | BUFFER_USAGE_MEM_MMZ_CACHE;
65 inputCfg.format = pixelFormat;
66 inputCfg.timeout = 0;
67 GSError err = buffer->Alloc(inputCfg);
68 if (GSERROR_OK != err) {
69 TEST_LOG("Alloc surface buffer{ %d(%d)x%d format:%d } failed:%d",
70 inputCfg.width, inputCfg.strideAlignment, inputCfg.height, inputCfg.format, err);
71 return nullptr;
72 }
73 return buffer;
74 }
75 } // namespace VideoProcessingEngine
76 } // namespace Media
77 } // namespace OHOS
78