1 /*
2 * Copyright (c) 2020-2021.Huawei Technologies Co., Ltd. All rights reserved.
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 #include "minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h"
17
18 #include <thread>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include "minddata/dataset/include/dataset/constants.h"
22 #include "minddata/dataset/core/tensor_shape.h"
23 #include "minddata/dataset/kernels/image/image_utils.h"
24 #include "minddata/dataset/util/status.h"
25
26 namespace {
27 const int BUFFER_SIZE = 2048;
28 const mode_t DEFAULT_FILE_PERMISSION = 0077;
29 } // namespace
30
SetFileDefaultUmask()31 mode_t SetFileDefaultUmask() { return umask(DEFAULT_FILE_PERMISSION); }
32
33 /*
34 * @description: Constructor
35 * @param: resizeWidth specifies the resized width
36 * @param: resizeHeight specifies the resized hegiht
37 * @param: stream is used to maintain the execution order of operations
38 * @param: context is used to manage the life cycle of objects
39 * @param: dvppCommon is a class for decoding and resizing
40 */
MDAclProcess(uint32_t resizeWidth,uint32_t resizeHeight,uint32_t cropWidth,uint32_t cropHeight,aclrtContext context,bool is_crop,aclrtStream stream,const std::shared_ptr<DvppCommon> & dvppCommon)41 MDAclProcess::MDAclProcess(uint32_t resizeWidth, uint32_t resizeHeight, uint32_t cropWidth, uint32_t cropHeight,
42 aclrtContext context, bool is_crop, aclrtStream stream,
43 const std::shared_ptr<DvppCommon> &dvppCommon)
44 : resizeWidth_(resizeWidth),
45 resizeHeight_(resizeHeight),
46 cropWidth_(cropWidth),
47 cropHeight_(cropHeight),
48 context_(context),
49 stream_(stream),
50 contain_crop_(is_crop),
51 dvppCommon_(dvppCommon),
52 processedInfo_(nullptr) {}
53
MDAclProcess(uint32_t ParaWidth,uint32_t ParaHeight,aclrtContext context,bool is_crop,aclrtStream stream,const std::shared_ptr<DvppCommon> & dvppCommon)54 MDAclProcess::MDAclProcess(uint32_t ParaWidth, uint32_t ParaHeight, aclrtContext context, bool is_crop,
55 aclrtStream stream, const std::shared_ptr<DvppCommon> &dvppCommon)
56 : contain_crop_(is_crop), context_(context), stream_(stream), dvppCommon_(dvppCommon), processedInfo_(nullptr) {
57 if (is_crop) {
58 resizeWidth_ = 0;
59 resizeHeight_ = 0;
60 cropWidth_ = ParaWidth;
61 cropHeight_ = ParaHeight;
62 } else {
63 resizeWidth_ = ParaWidth;
64 resizeHeight_ = ParaHeight;
65 cropWidth_ = 0;
66 cropHeight_ = 0;
67 }
68 }
69
MDAclProcess(aclrtContext context,bool is_crop,aclrtStream stream,const std::shared_ptr<DvppCommon> & dvppCommon)70 MDAclProcess::MDAclProcess(aclrtContext context, bool is_crop, aclrtStream stream,
71 const std::shared_ptr<DvppCommon> &dvppCommon)
72 : resizeWidth_(0),
73 resizeHeight_(0),
74 cropWidth_(0),
75 cropHeight_(0),
76 contain_crop_(is_crop),
77 context_(context),
78 stream_(stream),
79 dvppCommon_(dvppCommon),
80 processedInfo_(nullptr) {}
81 /*
82 * @description: Release MDAclProcess resources
83 * @return: aclError which is error code of ACL API
84 */
Release()85 APP_ERROR MDAclProcess::Release() {
86 // Release objects resource
87 APP_ERROR ret = dvppCommon_->DeInit();
88 dvppCommon_->ReleaseDvppBuffer();
89
90 if (ret != APP_ERR_OK) {
91 MS_LOG(ERROR) << "Failed to deinitialize dvppCommon_, ret = " << ret;
92 return ret;
93 }
94 MS_LOG(INFO) << "dvppCommon_ object deinitialized successfully";
95 dvppCommon_.reset();
96
97 // Release stream
98 if (stream_ != nullptr) {
99 ret = aclrtDestroyStream(stream_);
100 if (ret != APP_ERR_OK) {
101 MS_LOG(ERROR) << "Failed to destroy stream, ret = " << ret;
102 stream_ = nullptr;
103 return ret;
104 }
105 stream_ = nullptr;
106 }
107 MS_LOG(INFO) << "The stream is destroyed successfully";
108 return APP_ERR_OK;
109 }
110
111 /*
112 * @description: Initialize DvppCommon object
113 * @return: aclError which is error code of ACL API
114 */
InitModule()115 APP_ERROR MDAclProcess::InitModule() {
116 // Create Dvpp JpegD object
117 dvppCommon_ = std::make_shared<DvppCommon>(context_, stream_);
118 if (dvppCommon_ == nullptr) {
119 MS_LOG(ERROR) << "Failed to create dvppCommon_ object";
120 return APP_ERR_COMM_INIT_FAIL;
121 }
122 MS_LOG(INFO) << "DvppCommon object created successfully";
123 APP_ERROR ret = dvppCommon_->Init();
124 if (ret != APP_ERR_OK) {
125 MS_LOG(ERROR) << "Failed to initialize dvppCommon_ object, ret = " << ret;
126 return ret;
127 }
128 MS_LOG(INFO) << "DvppCommon object initialized successfully";
129 return APP_ERR_OK;
130 }
131
132 /*
133 * @description: Initialize MDAclProcess resources
134 * @return: aclError which is error code of ACL API
135 */
InitResource()136 APP_ERROR MDAclProcess::InitResource() {
137 APP_ERROR ret = aclrtSetCurrentContext(context_);
138 if (ret != APP_ERR_OK) {
139 MS_LOG(ERROR) << "Failed to get ACL context, ret = " << ret;
140 return ret;
141 }
142 MS_LOG(INFO) << "The context is created successfully";
143 ret = aclrtCreateStream(&stream_); // Create stream for application
144 if (ret != APP_ERR_OK) {
145 MS_LOG(ERROR) << "Failed to create ACL stream, ret = " << ret;
146 return ret;
147 }
148 MS_LOG(INFO) << "The stream is created successfully";
149 // Initialize dvpp module
150 if (InitModule() != APP_ERR_OK) {
151 return APP_ERR_COMM_INIT_FAIL;
152 }
153 return APP_ERR_OK;
154 }
155
GetDeviceModule()156 std::shared_ptr<DvppCommon> MDAclProcess::GetDeviceModule() { return dvppCommon_; }
157
GetContext()158 aclrtContext MDAclProcess::GetContext() { return context_; }
159
GetStream()160 aclrtStream MDAclProcess::GetStream() { return stream_; }
161
162 /*
163 * Sink data from Tensor(On host) to DeviceTensor(On device)
164 * Two cases are different, jpeg and png
165 */
H2D_Sink(const std::shared_ptr<mindspore::dataset::Tensor> & input,std::shared_ptr<mindspore::dataset::DeviceTensor> & device_input)166 APP_ERROR MDAclProcess::H2D_Sink(const std::shared_ptr<mindspore::dataset::Tensor> &input,
167 std::shared_ptr<mindspore::dataset::DeviceTensor> &device_input) {
168 // Recall the context created in InitResource()
169 APP_ERROR ret = aclrtSetCurrentContext(context_);
170 if (ret != APP_ERR_OK) {
171 MS_LOG(ERROR) << "Failed to get ACL context, ret = " << ret;
172 return ret;
173 }
174
175 RawData imageinfo;
176 uint32_t filesize = input->SizeInBytes();
177
178 imageinfo.lenOfByte = filesize;
179 unsigned char *buffer = const_cast<unsigned char *>(input->GetBuffer());
180 imageinfo.data = static_cast<void *>(buffer);
181
182 // Transfer RawData(Raw image) from host to device, which we call sink
183 if (IsNonEmptyJPEG(input)) { // case JPEG
184 ret = dvppCommon_->SinkImageH2D(imageinfo, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
185 } else { // case PNG
186 ret = dvppCommon_->SinkImageH2D(imageinfo);
187 }
188 if (ret != APP_ERR_OK) {
189 MS_LOG(ERROR) << "Failed to transport Tensor to device, ret = " << ret;
190 return ret;
191 }
192 auto deviceInputData = dvppCommon_->GetInputImage();
193
194 const mindspore::dataset::DataType dvpp_data_type(mindspore::dataset::DataType::DE_UINT8);
195 const mindspore::dataset::TensorShape dvpp_shape({1, 1, 1});
196 auto rc = mindspore::dataset::DeviceTensor::CreateEmpty(dvpp_shape, dvpp_data_type, &device_input);
197 if (rc.IsError()) {
198 MS_LOG(ERROR) << "Failed to allocate memory, error msg is " << rc;
199 return APP_ERR_ACL_BAD_ALLOC;
200 }
201 rc =
202 device_input->SetAttributes(deviceInputData->data, deviceInputData->dataSize, deviceInputData->width,
203 deviceInputData->widthStride, deviceInputData->height, deviceInputData->heightStride);
204 if (rc.IsError()) {
205 MS_LOG(ERROR) << "Failed to initialize device attribution, error msg is " << rc;
206 return APP_ERR_ACL_INVALID_PARAM;
207 }
208 return APP_ERR_OK;
209 }
210
D2H_Pop(const std::shared_ptr<mindspore::dataset::DeviceTensor> & device_output,std::shared_ptr<mindspore::dataset::Tensor> & output)211 APP_ERROR MDAclProcess::D2H_Pop(const std::shared_ptr<mindspore::dataset::DeviceTensor> &device_output,
212 std::shared_ptr<mindspore::dataset::Tensor> &output) {
213 void *resHostBuf = nullptr;
214 APP_ERROR ret = aclrtMallocHost(&resHostBuf, device_output->DeviceDataSize());
215 if (ret != APP_ERR_OK) {
216 MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
217 return ret;
218 }
219 std::shared_ptr<void> outBuf(resHostBuf, aclrtFreeHost);
220 processedInfo_ = outBuf;
221 // Memcpy the output data from device to host
222 ret = aclrtMemcpy(outBuf.get(), device_output->DeviceDataSize(), device_output->GetDeviceBuffer(),
223 device_output->DeviceDataSize(), ACL_MEMCPY_DEVICE_TO_HOST);
224 if (ret != APP_ERR_OK) {
225 MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
226 return ret;
227 }
228 auto data = std::static_pointer_cast<unsigned char>(processedInfo_);
229 unsigned char *ret_ptr = data.get();
230
231 mindspore::dataset::dsize_t dvppDataSize = device_output->DeviceDataSize();
232 const mindspore::dataset::TensorShape dvpp_shape({dvppDataSize, 1, 1});
233 uint32_t _output_width_ = device_output->GetYuvStrideShape()[0];
234 uint32_t _output_widthStride_ = device_output->GetYuvStrideShape()[1];
235 uint32_t _output_height_ = device_output->GetYuvStrideShape()[2];
236 uint32_t _output_heightStride_ = device_output->GetYuvStrideShape()[3];
237 const mindspore::dataset::DataType dvpp_data_type(mindspore::dataset::DataType::DE_UINT8);
238 auto rc = mindspore::dataset::Tensor::CreateFromMemory(dvpp_shape, dvpp_data_type, ret_ptr, &output);
239 if (rc.IsError()) {
240 MS_LOG(ERROR) << "Failed to allocate memory, error msg is " << rc;
241 return APP_ERR_ACL_BAD_ALLOC;
242 }
243 rc = output->SetYuvShape(_output_width_, _output_widthStride_, _output_height_, _output_heightStride_);
244 if (rc.IsError()) {
245 MS_LOG(ERROR) << "Failed to set yuv shape, error msg is " << rc;
246 return APP_ERR_ACL_INVALID_PARAM;
247 }
248 if (!output->HasData()) {
249 return APP_ERR_COMM_ALLOC_MEM;
250 }
251 return APP_ERR_OK;
252 }
253
JPEG_D(const RawData & ImageInfo)254 APP_ERROR MDAclProcess::JPEG_D(const RawData &ImageInfo) {
255 MS_LOG(WARNING) << "It's deprecated to use kCpu as input device for Dvpp operators to compute, because it's slow and "
256 "unsafe, we recommend you to set input device as MapTargetDevice::kAscend for Dvpp operators. "
257 "This API will be removed later";
258 struct timeval begin = {0};
259 struct timeval end = {0};
260 gettimeofday(&begin, nullptr);
261 // deal with image
262 APP_ERROR ret = JPEG_D_(ImageInfo);
263 if (ret != APP_ERR_OK) {
264 MS_LOG(ERROR) << "Failed to decode, ret = " << ret;
265 return ret;
266 }
267 gettimeofday(&end, nullptr);
268 // Calculate the time cost of preprocess
269 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
270 const double fps = 1 * SEC2MS / costMs;
271 MS_LOG(INFO) << "[dvpp decode Delay] cost: " << costMs << "ms\tfps: " << fps;
272 // Get output of resize module
273 std::shared_ptr<DvppDataInfo> DecodeOutData = dvppCommon_->GetDecodedImage();
274 if (!DecodeOutData) {
275 MS_LOG(ERROR) << "Decode Data returns NULL";
276 return APP_ERR_COMM_INVALID_POINTER;
277 }
278
279 // Alloc host memory for the inference output according to the size of output
280 void *resHostBuf = nullptr;
281 ret = aclrtMallocHost(&resHostBuf, DecodeOutData->dataSize);
282 if (ret != APP_ERR_OK) {
283 MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
284 return ret;
285 }
286 std::shared_ptr<void> outBuf(resHostBuf, aclrtFreeHost);
287 processedInfo_ = outBuf;
288 // Memcpy the output data from device to host
289 ret = aclrtMemcpy(outBuf.get(), DecodeOutData->dataSize, DecodeOutData->data, DecodeOutData->dataSize,
290 ACL_MEMCPY_DEVICE_TO_HOST);
291 if (ret != APP_ERR_OK) {
292 MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
293 return ret;
294 }
295 return APP_ERR_OK;
296 }
297
JPEG_D()298 APP_ERROR MDAclProcess::JPEG_D() {
299 struct timeval begin = {0};
300 struct timeval end = {0};
301 gettimeofday(&begin, nullptr);
302 // deal with image
303 APP_ERROR ret = JPEG_D_();
304 if (ret != APP_ERR_OK) {
305 MS_LOG(ERROR) << "Failed to decode, ret = " << ret;
306 return ret;
307 }
308 gettimeofday(&end, nullptr);
309 // Calculate the time cost of preprocess
310 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
311 const double fps = 1 * SEC2MS / costMs;
312 MS_LOG(INFO) << "[dvpp decode Delay] cost: " << costMs << "ms\tfps: " << fps;
313 // Get output of resize module
314 std::shared_ptr<DvppDataInfo> DecodeOutData = dvppCommon_->GetDecodedImage();
315 if (!DecodeOutData) {
316 MS_LOG(ERROR) << "Decode Data returns NULL";
317 return APP_ERR_COMM_INVALID_POINTER;
318 }
319 return APP_ERR_OK;
320 }
321
JPEG_D_(const RawData & ImageInfo)322 APP_ERROR MDAclProcess::JPEG_D_(const RawData &ImageInfo) {
323 APP_ERROR ret = dvppCommon_->CombineJpegdProcess(ImageInfo, PIXEL_FORMAT_YUV_SEMIPLANAR_420, true);
324 if (ret != APP_ERR_OK) {
325 MS_LOG(ERROR) << "Failed to process decode, ret = " << ret << ".";
326 return ret;
327 }
328 return APP_ERR_OK;
329 }
330
JPEG_D_()331 APP_ERROR MDAclProcess::JPEG_D_() {
332 auto input_ = dvppCommon_->GetInputImage();
333 auto decode_output_ = dvppCommon_->GetDecodedImage();
334 APP_ERROR ret = dvppCommon_->SinkCombineJpegdProcess(input_, decode_output_, true);
335 if (ret != APP_ERR_OK) {
336 MS_LOG(ERROR) << "Failed to process decode, ret = " << ret << ".";
337 return ret;
338 }
339 return APP_ERR_OK;
340 }
341
JPEG_R(const DvppDataInfo & ImageInfo)342 APP_ERROR MDAclProcess::JPEG_R(const DvppDataInfo &ImageInfo) {
343 MS_LOG(WARNING) << "It's deprecated to use kCpu as input device for Dvpp operators to compute, because it's slow and "
344 "unsafe, we recommend you to set input device as MapTargetDevice::kAscend for Dvpp operators. "
345 "This API will be removed later";
346 struct timeval begin = {0};
347 struct timeval end = {0};
348 gettimeofday(&begin, nullptr);
349 // deal with image
350 APP_ERROR ret = JPEG_R_(ImageInfo);
351 if (ret != APP_ERR_OK) {
352 MS_LOG(ERROR) << "Failed to resize, ret = " << ret;
353 return ret;
354 }
355 gettimeofday(&end, nullptr);
356 // Calculate the time cost of preprocess
357 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
358 const double fps = 1 * SEC2MS / costMs;
359 MS_LOG(INFO) << "[dvpp resize Delay] cost: " << costMs << "ms\tfps: " << fps;
360 // Get output of resize module
361 std::shared_ptr<DvppDataInfo> ResizeOutData = dvppCommon_->GetResizedImage();
362 if (!ResizeOutData) {
363 MS_LOG(ERROR) << "Resize Data returns NULL";
364 return APP_ERR_COMM_INVALID_POINTER;
365 }
366 // Alloc host memory for the inference output according to the size of output
367 void *resHostBuf = nullptr;
368 ret = aclrtMallocHost(&resHostBuf, ResizeOutData->dataSize);
369 if (ret != APP_ERR_OK) {
370 MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
371 return ret;
372 }
373 std::shared_ptr<void> outBuf(resHostBuf, aclrtFreeHost);
374 processedInfo_ = outBuf;
375 // Memcpy the output data from device to host
376 ret = aclrtMemcpy(outBuf.get(), ResizeOutData->dataSize, ResizeOutData->data, ResizeOutData->dataSize,
377 ACL_MEMCPY_DEVICE_TO_HOST);
378 if (ret != APP_ERR_OK) {
379 MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
380 return ret;
381 }
382 return APP_ERR_OK;
383 }
384
JPEG_R(std::string & last_step)385 APP_ERROR MDAclProcess::JPEG_R(std::string &last_step) {
386 struct timeval begin = {0};
387 struct timeval end = {0};
388 gettimeofday(&begin, nullptr);
389 // deal with image
390 APP_ERROR ret = JPEG_R_(last_step);
391 if (ret != APP_ERR_OK) {
392 MS_LOG(ERROR) << "Failed to resize, ret = " << ret;
393 return ret;
394 }
395 gettimeofday(&end, nullptr);
396 // Calculate the time cost of preprocess
397 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
398 const double fps = 1 * SEC2MS / costMs;
399 MS_LOG(INFO) << "[dvpp resize Delay] cost: " << costMs << "ms\tfps: " << fps;
400 // Get output of resize module
401 std::shared_ptr<DvppDataInfo> ResizeOutData = dvppCommon_->GetResizedImage();
402 if (!ResizeOutData) {
403 MS_LOG(ERROR) << "Resize Data returns NULL";
404 return APP_ERR_COMM_INVALID_POINTER;
405 }
406 return APP_ERR_OK;
407 }
408
JPEG_R_(const DvppDataInfo & ImageInfo)409 APP_ERROR MDAclProcess::JPEG_R_(const DvppDataInfo &ImageInfo) {
410 APP_ERROR ret = dvppCommon_->TransferYuvDataH2D(ImageInfo);
411 if (ret != APP_ERR_OK) {
412 MS_LOG(ERROR) << "Failed to copy data from host to device, ret = " << ret << ".";
413 return ret;
414 }
415 std::shared_ptr<DvppDataInfo> decoded_image = dvppCommon_->GetDecodedImage();
416 uint32_t pri_h = decoded_image->heightStride;
417 uint32_t pri_w = decoded_image->widthStride;
418 // Define the resize shape
419 DvppDataInfo resizeOut;
420 ret = ResizeConfigFilter(resizeOut, pri_w, pri_h);
421 if (ret != APP_ERR_OK) {
422 MS_LOG(ERROR) << "Failed to config resize parameter, ret = " << ret << ".";
423 return ret;
424 }
425 ret = dvppCommon_->CombineResizeProcess(*decoded_image, resizeOut, true);
426 if (ret != APP_ERR_OK) {
427 MS_LOG(ERROR) << "Failed to process resize, ret = " << ret << ".";
428 return ret;
429 }
430 return APP_ERR_OK;
431 }
432
JPEG_R_(std::string & last_step)433 APP_ERROR MDAclProcess::JPEG_R_(std::string &last_step) {
434 std::shared_ptr<DvppDataInfo> input_image = std::make_shared<DvppDataInfo>();
435 if (last_step == "Decode") {
436 input_image = dvppCommon_->GetDecodedImage();
437 } else {
438 input_image = dvppCommon_->GetCropedImage();
439 }
440 if (!input_image->data) {
441 MS_LOG(ERROR) << "Failed to get data for resize, please verify last step operation";
442 return APP_ERR_DVPP_RESIZE_FAIL;
443 }
444 uint32_t pri_h = input_image->heightStride;
445 uint32_t pri_w = input_image->widthStride;
446 DvppDataInfo resizeOut;
447 auto ret = ResizeConfigFilter(resizeOut, pri_w, pri_h);
448 if (ret != APP_ERR_OK) {
449 MS_LOG(ERROR) << "Failed to config resize, ret = " << ret << ".";
450 return ret;
451 }
452 ret = dvppCommon_->CombineResizeProcess(*input_image, resizeOut, true);
453 if (ret != APP_ERR_OK) {
454 MS_LOG(ERROR) << "Failed to process resize, ret = " << ret << ".";
455 return ret;
456 }
457 return APP_ERR_OK;
458 }
459
JPEG_C(const DvppDataInfo & ImageInfo)460 APP_ERROR MDAclProcess::JPEG_C(const DvppDataInfo &ImageInfo) {
461 MS_LOG(WARNING) << "It's deprecated to use kCpu as input device for Dvpp operators to compute, because it's slow and "
462 "unsafe, we recommend you to set input device as MapTargetDevice::kAscend for Dvpp operators. "
463 "This API will be removed later";
464 struct timeval begin = {0};
465 struct timeval end = {0};
466 gettimeofday(&begin, nullptr);
467 // deal with image
468 APP_ERROR ret = JPEG_C_(ImageInfo);
469 if (ret != APP_ERR_OK) {
470 MS_LOG(ERROR) << "Failed to crop image, ret = " << ret;
471 return ret;
472 }
473 gettimeofday(&end, nullptr);
474 // Calculate the time cost of preprocess
475 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
476 const double fps = 1 * SEC2MS / costMs;
477 MS_LOG(INFO) << "[dvpp crop Delay] cost: " << costMs << "ms\tfps: " << fps;
478 // Get output of resize module
479 std::shared_ptr<DvppDataInfo> CropOutData = dvppCommon_->GetCropedImage();
480 if (!CropOutData) {
481 MS_LOG(ERROR) << "Crop Data returns NULL";
482 return APP_ERR_COMM_INVALID_POINTER;
483 }
484 // Alloc host memory for the inference output according to the size of output
485 void *resHostBuf = nullptr;
486 ret = aclrtMallocHost(&resHostBuf, CropOutData->dataSize);
487 if (ret != APP_ERR_OK) {
488 MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
489 return ret;
490 }
491 std::shared_ptr<void> outBuf(resHostBuf, aclrtFreeHost);
492 processedInfo_ = outBuf;
493 // Memcpy the output data from device to host
494 ret = aclrtMemcpy(outBuf.get(), CropOutData->dataSize, CropOutData->data, CropOutData->dataSize,
495 ACL_MEMCPY_DEVICE_TO_HOST);
496 if (ret != APP_ERR_OK) {
497 MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
498 return ret;
499 }
500 return APP_ERR_OK;
501 }
502
JPEG_C(std::string & last_step)503 APP_ERROR MDAclProcess::JPEG_C(std::string &last_step) {
504 struct timeval begin = {0};
505 struct timeval end = {0};
506 gettimeofday(&begin, nullptr);
507 // deal with image
508 APP_ERROR ret = JPEG_C_(last_step);
509 if (ret != APP_ERR_OK) {
510 MS_LOG(ERROR) << "Failed to crop image, ret = " << ret;
511 return ret;
512 }
513 gettimeofday(&end, nullptr);
514 // Calculate the time cost of preprocess
515 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
516 const double fps = 1 * SEC2MS / costMs;
517 MS_LOG(INFO) << "[dvpp crop Delay] cost: " << costMs << "ms\tfps: " << fps;
518 // Get output of resize module
519 std::shared_ptr<DvppDataInfo> CropOutData = dvppCommon_->GetCropedImage();
520 if (!CropOutData) {
521 MS_LOG(ERROR) << "Crop Data returns NULL";
522 return APP_ERR_COMM_INVALID_POINTER;
523 }
524 return APP_ERR_OK;
525 }
526
JPEG_C_(const DvppDataInfo & ImageInfo)527 APP_ERROR MDAclProcess::JPEG_C_(const DvppDataInfo &ImageInfo) {
528 APP_ERROR ret = dvppCommon_->TransferYuvDataH2D(ImageInfo);
529 if (ret != APP_ERR_OK) {
530 MS_LOG(ERROR) << "Failed to copy data from host to device, ret = " << ret << ".";
531 return ret;
532 }
533 // Unneccessary to be image after resize, maybe after decode, we store both of them in DecodedImage()
534 std::shared_ptr<DvppDataInfo> resized_image = dvppCommon_->GetDecodedImage();
535 uint32_t pri_h = resized_image->heightStride;
536 uint32_t pri_w = resized_image->widthStride;
537 // Validate the crop shape
538 DvppDataInfo cropOut;
539 cropOut.width = cropWidth_;
540 cropOut.height = cropHeight_;
541 if (cropOut.width > pri_w || cropOut.height > pri_h) {
542 MS_LOG(ERROR) << "Crop size can not excceed resize, please verify your input [CROP SIZE] parameters";
543 return APP_ERR_COMM_INVALID_PARAM;
544 }
545 cropOut.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
546 DvppCropInputInfo cropInfo;
547 cropInfo.dataInfo = *resized_image;
548 // Define crop area
549 CropRoiConfig cropCfg;
550 CropConfigFilter(cropCfg, cropInfo, *resized_image);
551 ret = dvppCommon_->CombineCropProcess(cropInfo, cropOut, true);
552 if (ret != APP_ERR_OK) {
553 MS_LOG(ERROR) << "Failed to process center crop, ret = " << ret << ".";
554 return ret;
555 }
556 return APP_ERR_OK;
557 }
558
JPEG_C_(std::string & last_step)559 APP_ERROR MDAclProcess::JPEG_C_(std::string &last_step) {
560 std::shared_ptr<DvppDataInfo> input_image = std::make_shared<DvppDataInfo>();
561 if (last_step == "Resize") {
562 input_image = dvppCommon_->GetResizedImage();
563 } else {
564 input_image = dvppCommon_->GetDecodedImage();
565 }
566 if (!input_image->data) {
567 MS_LOG(ERROR) << "Failed to get input data for crop, please verify last step operation";
568 return APP_ERR_DVPP_CROP_FAIL;
569 }
570 uint32_t pri_h = input_image->heightStride;
571 uint32_t pri_w = input_image->widthStride;
572 DvppDataInfo cropOut;
573 cropOut.width = cropWidth_;
574 cropOut.height = cropHeight_;
575 if (cropOut.width > pri_w || cropOut.height > pri_h) {
576 MS_LOG(ERROR) << "Crop size can not excceed resize, please verify your input [CROP SIZE] parameters";
577 return APP_ERR_COMM_INVALID_PARAM;
578 }
579 cropOut.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
580 DvppCropInputInfo cropInfo;
581 cropInfo.dataInfo = *input_image;
582 // Define crop area
583 CropRoiConfig cropCfg;
584 CropConfigFilter(cropCfg, cropInfo, *input_image);
585 APP_ERROR ret = dvppCommon_->CombineCropProcess(cropInfo, cropOut, true);
586 if (ret != APP_ERR_OK) {
587 MS_LOG(ERROR) << "Failed to process center crop, ret = " << ret << ".";
588 return ret;
589 }
590 return APP_ERR_OK;
591 }
592
PNG_D(const RawData & ImageInfo)593 APP_ERROR MDAclProcess::PNG_D(const RawData &ImageInfo) {
594 MS_LOG(WARNING) << "It's deprecated to use kCpu as input device for Dvpp operators to compute, because it's slow and "
595 "unsafe, we recommend you to set input device as MapTargetDevice::kAscend for Dvpp operators. "
596 "This API will be removed later";
597 struct timeval begin = {0};
598 struct timeval end = {0};
599 gettimeofday(&begin, nullptr);
600 // deal with image
601 APP_ERROR ret = PNG_D_(ImageInfo);
602 if (ret != APP_ERR_OK) {
603 MS_LOG(ERROR) << "Failed to decode, ret = " << ret;
604 return ret;
605 }
606 gettimeofday(&end, nullptr);
607 // Calculate the time cost of preprocess
608 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
609 const double fps = 1 * SEC2MS / costMs;
610 MS_LOG(INFO) << "[dvpp Delay] cost: " << costMs << "ms\tfps: " << fps;
611 // Get output of resize module
612 std::shared_ptr<DvppDataInfo> DecodeOutData = dvppCommon_->GetDecodedImage();
613 if (!DecodeOutData) {
614 MS_LOG(ERROR) << "ResizedOutData returns NULL";
615 return APP_ERR_COMM_INVALID_POINTER;
616 }
617 // Alloc host memory for the inference output according to the size of output
618 void *resHostBuf = nullptr;
619 ret = aclrtMallocHost(&resHostBuf, DecodeOutData->dataSize);
620 if (ret != APP_ERR_OK) {
621 MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
622 return ret;
623 }
624 std::shared_ptr<void> outBuf(resHostBuf, aclrtFreeHost);
625 processedInfo_ = outBuf;
626 // Memcpy the output data from device to host
627 ret = aclrtMemcpy(outBuf.get(), DecodeOutData->dataSize, DecodeOutData->data, DecodeOutData->dataSize,
628 ACL_MEMCPY_DEVICE_TO_HOST);
629 if (ret != APP_ERR_OK) {
630 MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
631 return ret;
632 }
633 return APP_ERR_OK;
634 }
635
PNG_D()636 APP_ERROR MDAclProcess::PNG_D() {
637 struct timeval begin = {0};
638 struct timeval end = {0};
639 gettimeofday(&begin, nullptr);
640 // deal with image
641 APP_ERROR ret = PNG_D_();
642 if (ret != APP_ERR_OK) {
643 MS_LOG(ERROR) << "Failed to decode, ret = " << ret;
644 return ret;
645 }
646 gettimeofday(&end, nullptr);
647 // Calculate the time cost of preprocess
648 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
649 const double fps = 1 * SEC2MS / costMs;
650 MS_LOG(INFO) << "[dvpp decode Delay] cost: " << costMs << "ms\tfps: " << fps;
651 // Get output of resize module
652 std::shared_ptr<DvppDataInfo> DecodeOutData = dvppCommon_->GetDecodedImage();
653 if (!DecodeOutData) {
654 MS_LOG(ERROR) << "Decode Data returns NULL";
655 return APP_ERR_COMM_INVALID_POINTER;
656 }
657 return APP_ERR_OK;
658 }
659
PNG_D_(const RawData & ImageInfo)660 APP_ERROR MDAclProcess::PNG_D_(const RawData &ImageInfo) {
661 APP_ERROR ret = dvppCommon_->CombinePngdProcess(ImageInfo, PIXEL_FORMAT_RGB_888, true);
662 if (ret != APP_ERR_OK) {
663 MS_LOG(ERROR) << "Failed to process decode, ret = " << ret << ".";
664 return ret;
665 }
666 return APP_ERR_OK;
667 }
668
PNG_D_()669 APP_ERROR MDAclProcess::PNG_D_() {
670 auto input_ = dvppCommon_->GetInputImage();
671 auto decode_output_ = dvppCommon_->GetDecodedImage();
672 APP_ERROR ret = dvppCommon_->SinkCombinePngdProcess(input_, decode_output_, true);
673 if (ret != APP_ERR_OK) {
674 MS_LOG(ERROR) << "Failed to process decode, ret = " << ret << ".";
675 return ret;
676 }
677 return APP_ERR_OK;
678 }
679
680 /*
681 * @description: Decode and scale the picture, and write the result to a file
682 * @param: imageFile specifies the image path to be processed
683 * @return: aclError which is error code of ACL API
684 */
JPEG_DRC(const RawData & ImageInfo)685 APP_ERROR MDAclProcess::JPEG_DRC(const RawData &ImageInfo) {
686 MS_LOG(WARNING) << "It's deprecated to use kCpu as input device for Dvpp operators to compute, because it's slow and "
687 "unsafe, we recommend you to set input device as MapTargetDevice::kAscend for Dvpp operators. "
688 "This API will be removed later";
689 struct timeval begin = {0};
690 struct timeval end = {0};
691 gettimeofday(&begin, nullptr);
692 // deal with image
693 APP_ERROR ret = JPEG_DRC_(ImageInfo);
694 if (ret != APP_ERR_OK) {
695 MS_LOG(ERROR) << "Failed to decode or resize or crop, ret = " << ret;
696 return ret;
697 }
698 gettimeofday(&end, nullptr);
699 // Calculate the time cost of preprocess
700 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
701 const double fps = 1 * SEC2MS / costMs;
702 MS_LOG(INFO) << "[dvpp Delay] cost: " << costMs << "ms\tfps: " << fps;
703 // Get output of resize module
704 /* 测试Device内存
705 */
706 std::shared_ptr<DvppDataInfo> CropOutData = dvppCommon_->GetCropedImage();
707 if (CropOutData->dataSize == 0) {
708 MS_LOG(ERROR) << "CropOutData return NULL";
709 return APP_ERR_COMM_INVALID_POINTER;
710 }
711 // Alloc host memory for the inference output according to the size of output
712 void *resHostBuf = nullptr;
713 ret = aclrtMallocHost(&resHostBuf, CropOutData->dataSize);
714 if (ret != APP_ERR_OK) {
715 MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
716 return ret;
717 }
718 std::shared_ptr<void> outBuf(resHostBuf, aclrtFreeHost);
719 processedInfo_ = outBuf;
720 // Memcpy the output data from device to host
721 ret = aclrtMemcpy(outBuf.get(), CropOutData->dataSize, CropOutData->data, CropOutData->dataSize,
722 ACL_MEMCPY_DEVICE_TO_HOST);
723 if (ret != APP_ERR_OK) {
724 MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
725 return ret;
726 }
727 return APP_ERR_OK;
728 }
729
JPEG_DRC()730 APP_ERROR MDAclProcess::JPEG_DRC() {
731 struct timeval begin = {0};
732 struct timeval end = {0};
733 gettimeofday(&begin, nullptr);
734 // deal with image
735 APP_ERROR ret = JPEG_D_();
736 if (ret != APP_ERR_OK) {
737 MS_LOG(ERROR) << "Failed to decode, ret = " << ret;
738 return ret;
739 }
740 std::string last_step = "Decode";
741 ret = JPEG_R_(last_step);
742 if (ret != APP_ERR_OK) {
743 MS_LOG(ERROR) << "Failed to resize, ret = " << ret;
744 return ret;
745 }
746 last_step = "Resize";
747 ret = JPEG_C_(last_step);
748 if (ret != APP_ERR_OK) {
749 MS_LOG(ERROR) << "Failed to crop, ret = " << ret;
750 return ret;
751 }
752 // Get output of crop module
753 std::shared_ptr<DvppDataInfo> CropOutData = dvppCommon_->GetCropedImage();
754 if (!CropOutData) {
755 MS_LOG(ERROR) << "Decode Data returns NULL";
756 return APP_ERR_COMM_INVALID_POINTER;
757 }
758 gettimeofday(&end, nullptr);
759 // Calculate the time cost of preprocess
760 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
761 const double fps = 1 * SEC2MS / costMs;
762 MS_LOG(INFO) << "[dvpp (Decode + Resize + Crop) Delay] cost: " << costMs << "ms\tfps: " << fps;
763 return APP_ERR_OK;
764 }
765
766 /*
767 * @description: Read image files, and perform decoding and scaling
768 * @param: imageFile specifies the image path to be processed
769 * @return: aclError which is error code of ACL API
770 */
JPEG_DRC_(const RawData & ImageInfo)771 APP_ERROR MDAclProcess::JPEG_DRC_(const RawData &ImageInfo) {
772 // Decode process
773 APP_ERROR ret = dvppCommon_->CombineJpegdProcess(ImageInfo, PIXEL_FORMAT_YUV_SEMIPLANAR_420, true);
774 if (ret != APP_ERR_OK) {
775 MS_LOG(ERROR) << "Failed to process decode, ret = " << ret << ".";
776 return ret;
777 }
778 // Get output of decoded jpeg image, decodeOutData locates on device
779 std::shared_ptr<DvppDataInfo> decodeOutData = dvppCommon_->GetDecodedImage();
780
781 if (decodeOutData == nullptr) {
782 MS_LOG(ERROR) << "Decode output buffer is null.";
783 return APP_ERR_COMM_INVALID_POINTER;
784 }
785 uint32_t pri_h = decodeOutData->heightStride;
786 uint32_t pri_w = decodeOutData->widthStride;
787 // Define output of resize jpeg image
788 DvppDataInfo resizeOut;
789 ret = ResizeConfigFilter(resizeOut, pri_w, pri_h);
790 if (ret != APP_ERR_OK) {
791 MS_LOG(ERROR) << "Failed to config resize, ret = " << ret << ".";
792 return ret;
793 }
794 // Run resize application function
795 ret = dvppCommon_->CombineResizeProcess(*decodeOutData, resizeOut, true);
796 if (ret != APP_ERR_OK) {
797 MS_LOG(ERROR) << "Failed to process resize, ret = " << ret << ".";
798 return ret;
799 }
800 // Get output of resize jpeg image, resizeOutData locates on device
801 std::shared_ptr<DvppDataInfo> resizeOutData = dvppCommon_->GetResizedImage();
802 if (resizeOutData == nullptr) {
803 MS_LOG(ERROR) << "resize output buffer is null.";
804 return APP_ERR_COMM_INVALID_POINTER;
805 }
806 // Define output of crop jpeg image
807 DvppDataInfo cropOut;
808 cropOut.width = cropWidth_;
809 cropOut.height = cropHeight_;
810 cropOut.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
811 // Define input of crop jpeg image
812 DvppCropInputInfo cropInfo;
813 cropInfo.dataInfo = *resizeOutData;
814 // Define crop area
815 CropRoiConfig cropCfg;
816 CropConfigFilter(cropCfg, cropInfo, resizeOut);
817 ret = dvppCommon_->CombineCropProcess(cropInfo, cropOut, true);
818 if (ret != APP_ERR_OK) {
819 MS_LOG(ERROR) << "Failed to process center crop, ret = " << ret << ".";
820 return ret;
821 }
822 return APP_ERR_OK;
823 }
824
JPEG_DR(const RawData & ImageInfo)825 APP_ERROR MDAclProcess::JPEG_DR(const RawData &ImageInfo) {
826 MS_LOG(WARNING) << "It's deprecated to use kCpu as input device for Dvpp operators to compute, because it's slow and "
827 "unsafe, we recommend you to set input device as MapTargetDevice::kAscend for Dvpp operators. "
828 "This API will be removed later";
829 struct timeval begin = {0};
830 struct timeval end = {0};
831 gettimeofday(&begin, nullptr);
832 // deal with image
833 APP_ERROR ret = JPEG_DR_(ImageInfo);
834 if (ret != APP_ERR_OK) {
835 MS_LOG(ERROR) << "Failed to decode or resize, ret = " << ret;
836 return ret;
837 }
838 gettimeofday(&end, nullptr);
839 // Calculate the time cost of preprocess
840 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
841 const double fps = 1 * SEC2MS / costMs;
842 MS_LOG(INFO) << "[dvpp Delay] cost: " << costMs << "ms\tfps: " << fps;
843 // Get output of resize module
844 std::shared_ptr<DvppDataInfo> ResizeOutData = dvppCommon_->GetResizedImage();
845 if (!ResizeOutData) {
846 MS_LOG(ERROR) << "ResizedOutData returns NULL";
847 return APP_ERR_COMM_INVALID_POINTER;
848 }
849 // Alloc host memory for the inference output according to the size of output
850 void *resHostBuf = nullptr;
851 ret = aclrtMallocHost(&resHostBuf, ResizeOutData->dataSize);
852 if (ret != APP_ERR_OK) {
853 MS_LOG(ERROR) << "Failed to allocate memory from host ret = " << ret;
854 return ret;
855 }
856 std::shared_ptr<void> outBuf(resHostBuf, aclrtFreeHost);
857 processedInfo_ = outBuf;
858 // Memcpy the output data from device to host
859 ret = aclrtMemcpy(outBuf.get(), ResizeOutData->dataSize, ResizeOutData->data, ResizeOutData->dataSize,
860 ACL_MEMCPY_DEVICE_TO_HOST);
861 if (ret != APP_ERR_OK) {
862 MS_LOG(ERROR) << "Failed to copy memory from device to host, ret = " << ret;
863 return ret;
864 }
865 return APP_ERR_OK;
866 }
867
JPEG_DR()868 APP_ERROR MDAclProcess::JPEG_DR() {
869 struct timeval begin = {0};
870 struct timeval end = {0};
871 gettimeofday(&begin, nullptr);
872 // deal with image
873 APP_ERROR ret = JPEG_D_();
874 if (ret != APP_ERR_OK) {
875 MS_LOG(ERROR) << "Failed to decode, ret = " << ret;
876 return ret;
877 }
878 std::string last_step = "Decode";
879 ret = JPEG_R_(last_step);
880 if (ret != APP_ERR_OK) {
881 MS_LOG(ERROR) << "Failed to resize, ret = " << ret;
882 return ret;
883 }
884 // Get output of resize module
885 std::shared_ptr<DvppDataInfo> ResizeOutData = dvppCommon_->GetResizedImage();
886 if (!ResizeOutData) {
887 MS_LOG(ERROR) << "Decode Data returns NULL";
888 return APP_ERR_COMM_INVALID_POINTER;
889 }
890 gettimeofday(&end, nullptr);
891 // Calculate the time cost of preprocess
892 const double costMs = SEC2MS * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) / SEC2MS;
893 const double fps = 1 * SEC2MS / costMs;
894 MS_LOG(INFO) << "[dvpp (Decode + Resize) Delay] cost: " << costMs << "ms\tfps: " << fps;
895 return APP_ERR_OK;
896 }
897
JPEG_DR_(const RawData & ImageInfo)898 APP_ERROR MDAclProcess::JPEG_DR_(const RawData &ImageInfo) {
899 // Decode process
900 APP_ERROR ret = dvppCommon_->CombineJpegdProcess(ImageInfo, PIXEL_FORMAT_YUV_SEMIPLANAR_420, true);
901 if (ret != APP_ERR_OK) {
902 MS_LOG(ERROR) << "Failed to process decode, ret = " << ret << ".";
903 return ret;
904 }
905 // Get output of decoded jpeg image
906 std::shared_ptr<DvppDataInfo> decodeOutData = dvppCommon_->GetDecodedImage();
907 if (decodeOutData == nullptr) {
908 MS_LOG(ERROR) << "Decode output buffer is null.";
909 return APP_ERR_COMM_INVALID_POINTER;
910 }
911 // Define output of resize jpeg image
912 uint32_t pri_h = decodeOutData->heightStride;
913 uint32_t pri_w = decodeOutData->widthStride;
914 DvppDataInfo resizeOut;
915 ret = ResizeConfigFilter(resizeOut, pri_w, pri_h);
916 if (ret != APP_ERR_OK) {
917 MS_LOG(ERROR) << "Failed to config resize, ret = " << ret << ".";
918 return ret;
919 }
920 // Run resize application function
921 ret = dvppCommon_->CombineResizeProcess(*decodeOutData, resizeOut, true);
922 if (ret != APP_ERR_OK) {
923 MS_LOG(ERROR) << "Failed to process resize, ret = " << ret << ".";
924 return ret;
925 }
926 return APP_ERR_OK;
927 }
928
CropConfigFilter(CropRoiConfig & cfg,DvppCropInputInfo & cropinfo,DvppDataInfo & resizeinfo)929 void MDAclProcess::CropConfigFilter(CropRoiConfig &cfg, DvppCropInputInfo &cropinfo, DvppDataInfo &resizeinfo) {
930 if (resizeHeight_ != 0) {
931 cfg.up = (resizeHeight_ - cropHeight_) / 2;
932 if (cfg.up % 2 != 0) {
933 cfg.up++;
934 }
935 cfg.down = resizeHeight_ - (resizeHeight_ - cropHeight_) / 2;
936 if (cfg.down % 2 == 0) {
937 cfg.down--;
938 }
939 } else {
940 cfg.up = (resizeinfo.height - cropHeight_) / 2;
941 if (cfg.up % 2 != 0) {
942 cfg.up++;
943 }
944 cfg.down = resizeinfo.height - (resizeinfo.height - cropHeight_) / 2;
945 if (cfg.down % 2 == 0) {
946 cfg.down--;
947 }
948 }
949 if (resizeWidth_ != 0) {
950 cfg.left = (resizeWidth_ - cropWidth_) / 2;
951 if (cfg.left % 2 != 0) {
952 cfg.left++;
953 }
954 cfg.right = resizeWidth_ - (resizeWidth_ - cropWidth_) / 2;
955 if (cfg.right % 2 == 0) {
956 cfg.right--;
957 }
958 } else {
959 cfg.left = (resizeinfo.width - cropWidth_) / 2;
960 if (cfg.left % 2 != 0) {
961 cfg.left++;
962 }
963 cfg.right = resizeinfo.width - (resizeinfo.width - cropWidth_) / 2;
964 if (cfg.right % 2 == 0) {
965 cfg.right--;
966 }
967 }
968 cropinfo.roi = cfg;
969 }
970
ResizeConfigFilter(DvppDataInfo & resizeinfo,const uint32_t pri_w_,const uint32_t pri_h_) const971 APP_ERROR MDAclProcess::ResizeConfigFilter(DvppDataInfo &resizeinfo, const uint32_t pri_w_,
972 const uint32_t pri_h_) const {
973 if (resizeWidth_ != 0) { // 如果输入参数个数为2,按指定参数缩放
974 resizeinfo.width = resizeWidth_;
975 resizeinfo.widthStride = DVPP_ALIGN_UP(resizeWidth_, VPC_STRIDE_WIDTH);
976 resizeinfo.height = resizeHeight_;
977 resizeinfo.heightStride = DVPP_ALIGN_UP(resizeHeight_, VPC_STRIDE_HEIGHT);
978 } else { // 如果输入参数个数为1,保持原图片比例缩放
979 if (pri_h_ >= pri_w_) {
980 resizeinfo.width = resizeHeight_; // 若输入参数个数为1,则只有resizeHeight_有值
981 resizeinfo.widthStride = DVPP_ALIGN_UP(resizeinfo.width, VPC_STRIDE_WIDTH);
982 resizeinfo.height = uint32_t(resizeHeight_ * pri_h_ / pri_w_);
983 resizeinfo.heightStride = DVPP_ALIGN_UP(resizeinfo.height, VPC_STRIDE_HEIGHT);
984 } else {
985 resizeinfo.width = uint32_t(resizeHeight_ * pri_w_ / pri_h_);
986 resizeinfo.widthStride = DVPP_ALIGN_UP(resizeinfo.width, VPC_STRIDE_WIDTH);
987 resizeinfo.height = resizeHeight_;
988 resizeinfo.heightStride = DVPP_ALIGN_UP(resizeinfo.height, VPC_STRIDE_HEIGHT);
989 }
990 }
991 resizeinfo.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
992 return APP_ERR_OK;
993 }
994 /*
995 * @description: Obtain result data of memory
996 * @param: processed_data is result data info pointer
997 * @return: Address of data in the memory
998 */
Get_Memory_Data()999 std::shared_ptr<void> MDAclProcess::Get_Memory_Data() { return processedInfo_; }
1000
Get_Croped_DeviceData()1001 std::shared_ptr<DvppDataInfo> MDAclProcess::Get_Croped_DeviceData() { return dvppCommon_->GetCropedImage(); }
1002
Get_Resized_DeviceData()1003 std::shared_ptr<DvppDataInfo> MDAclProcess::Get_Resized_DeviceData() { return dvppCommon_->GetResizedImage(); }
1004
Get_Decode_DeviceData()1005 std::shared_ptr<DvppDataInfo> MDAclProcess::Get_Decode_DeviceData() { return dvppCommon_->GetDecodedImage(); }
1006
SetResizeParas(uint32_t width,uint32_t height)1007 APP_ERROR MDAclProcess::SetResizeParas(uint32_t width, uint32_t height) {
1008 resizeWidth_ = width;
1009 resizeHeight_ = height;
1010 return APP_ERR_OK;
1011 }
1012
SetCropParas(uint32_t width,uint32_t height)1013 APP_ERROR MDAclProcess::SetCropParas(uint32_t width, uint32_t height) {
1014 cropWidth_ = width;
1015 cropHeight_ = height;
1016 return APP_ERR_OK;
1017 }
1018
device_memory_release()1019 APP_ERROR MDAclProcess::device_memory_release() {
1020 dvppCommon_->ReleaseDvppBuffer();
1021 MS_LOG(INFO) << "Device memory release successfully";
1022 return APP_ERR_OK;
1023 }
1024
Get_Primary_Shape()1025 std::vector<uint32_t> MDAclProcess::Get_Primary_Shape() {
1026 std::vector<uint32_t> pri_shape;
1027 if (!dvppCommon_) {
1028 pri_shape.emplace_back(dvppCommon_->GetDecodedImage()->heightStride);
1029 pri_shape.emplace_back(dvppCommon_->GetDecodedImage()->widthStride);
1030 }
1031 return pri_shape;
1032 }
1033