You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dvpp_process.h 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef INC_DVPP_PROCESS_ACL
  17. #define INC_DVPP_PROCESS_ACL
  18. #include <vector>
  19. #include <string>
  20. #include "acl/acl.h"
  21. #include "acl/acl_mdl.h"
  22. #include "acl/acl_rt.h"
  23. #include "acl/ops/acl_dvpp.h"
  24. #include "include/inference.h"
  25. namespace mindspore::inference {
  26. struct DvppDecodePara {
  27. acldvppPixelFormat pixel_format = PIXEL_FORMAT_YUV_SEMIPLANAR_420;
  28. };
  29. struct DvppResizePara {
  30. uint32_t output_width = 0;
  31. uint32_t output_height = 0;
  32. };
  33. enum DvppCropType {
  34. // crop left,top,right,bottom is given in config
  35. kDvppCropTypeOffset = 0,
  36. // crop left,top,right,bottom is calculated by image width/height and output crop width/height
  37. kDvppCropTypeCentre = 1,
  38. };
  39. struct DvppRoiArea {
  40. uint32_t left = 0;
  41. uint32_t top = 0;
  42. uint32_t right = 0;
  43. uint32_t bottom = 0;
  44. };
  45. struct DvppCropInfo {
  46. DvppCropType crop_type = kDvppCropTypeOffset;
  47. DvppRoiArea crop_area; // when kDvppCropTypeOffset
  48. uint32_t crop_width = 0; // when kDvppCropTypeCentre
  49. uint32_t crop_height = 0; // when kDvppCropTypeCentre
  50. };
  51. struct DvppCropPara {
  52. DvppCropInfo crop_info;
  53. uint32_t output_width = 0;
  54. uint32_t output_height = 0;
  55. };
  56. struct DvppCropAndPastePara {
  57. DvppCropInfo crop_info;
  58. DvppRoiArea paste_area;
  59. uint32_t output_width = 0;
  60. uint32_t output_height = 0;
  61. };
  62. class DvppProcess {
  63. public:
  64. DvppProcess();
  65. ~DvppProcess();
  66. Status InitResource(aclrtStream stream);
  67. void Finalize();
  68. Status InitJpegDecodePara(const DvppDecodePara &decode_para); // jpeg decode + (resize | crop)
  69. Status InitResizePara(const DvppResizePara &resize_para); // jpeg decode + resize
  70. Status InitCropPara(const DvppCropPara &crop_para); // jpeg decode + crop
  71. Status InitCropAndPastePara(const DvppCropAndPastePara &crop_and_paste_para); // jpeg decode + crop&paste
  72. Status InitWithJsonConfig(const std::string &json_config);
  73. // output device buffer will be destroy by DvppProcess itself.
  74. Status Process(const void *pic_buffer, size_t pic_buffer_size, void *&output_device_buffer, size_t &output_size);
  75. Status Process(const std::vector<const void *> &pic_buffer_list, const std::vector<size_t> &pic_buffer_size_list,
  76. void *&output_device_buffer, size_t &output_size);
  77. private:
  78. uint32_t pic_width_ = 0;
  79. uint32_t pic_height_ = 0;
  80. DvppDecodePara decode_para_;
  81. DvppResizePara resize_para_;
  82. DvppCropPara crop_para_;
  83. DvppCropAndPastePara crop_and_paste_para_;
  84. // only one of the resize or crop flag can be true
  85. bool to_resize_flag_ = false;
  86. bool to_crop_flag_ = false;
  87. bool to_crop_and_paste_flag_ = false;
  88. void *input_pic_dev_buffer_ = nullptr;
  89. uint32_t input_pic_buffer_size_ = 0;
  90. uint32_t decode_output_buffer_size_ = 0;
  91. void *decode_output_buffer_dev_ = nullptr;
  92. acldvppPicDesc *decode_output_desc_ = nullptr;
  93. acldvppResizeConfig *resize_config_ = nullptr;
  94. acldvppRoiConfig *crop_area_ = nullptr;
  95. acldvppRoiConfig *paste_area_ = nullptr;
  96. acldvppPicDesc *vpc_output_desc_ = nullptr;
  97. void *vpc_output_buffer_dev_ = nullptr; // vpc_output_buffer_size_ length
  98. uint32_t vpc_output_buffer_size_ = 0;
  99. void *batch_vpc_output_buffer_dev_ = nullptr; // batch_size_ * vpc_output_buffer_size_ length
  100. uint32_t batch_size_ = 0;
  101. aclrtStream stream_ = nullptr;
  102. acldvppChannelDesc *dvpp_channel_desc_ = nullptr;
  103. uint32_t AlignmentHelper(uint32_t org_size, uint32_t alignment) const;
  104. uint32_t GetImageBufferSize(uint32_t stride_width, uint32_t stride_height, acldvppPixelFormat pixel_format) const;
  105. Status GetPicDescStride(uint32_t width, uint32_t height, uint32_t &stride_width, uint32_t &stride_height);
  106. Status GetPicDescStrideDecode(uint32_t width, uint32_t height, uint32_t &stride_width, uint32_t &stride_height);
  107. Status InputInputBuffer(const void *pic_buffer, size_t pic_buffer_size);
  108. Status InitDecodeOutputDesc(uint32_t image_width,
  109. uint32_t image_height); // decode_output_desc_, decode_output_buffer_dev_
  110. Status CheckRoiAreaWidthHeight(uint32_t width, uint32_t height);
  111. Status CheckAndAdjustRoiArea(DvppRoiArea &area);
  112. Status UpdateCropArea(uint32_t image_width, uint32_t image_height);
  113. Status CheckResizeImageInfo(uint32_t image_width, uint32_t image_height) const;
  114. void DestroyDecodeDesc();
  115. Status InitVpcOutputDesc(uint32_t output_width, uint32_t output_height,
  116. acldvppPixelFormat pixel_format); // vpc_output_desc_, vpc_output_buffer_dev_batch_
  117. Status InitRoiAreaConfig(acldvppRoiConfig *&roi_area, const DvppRoiArea &init_para);
  118. Status InitCommonCropPara(DvppCropInfo &crop_info, uint32_t out_width, uint32_t out_height);
  119. Status InitResizeOutputDesc(); // vpc_output_desc_, vpc_output_buffer_dev_, resize_config
  120. Status InitCropOutputDesc(); // vpc_output_desc_, vpc_output_buffer_dev_, crop_area_
  121. Status InitCropAndPasteOutputDesc(); // vpc_output_desc_, vpc_output_buffer_dev_, crop_area_, paste_area_
  122. void DestroyVpcOutputDesc();
  123. Status ProcessDecode();
  124. Status ProcessResize();
  125. Status ProcessCrop();
  126. Status ProcessCropAndPaste();
  127. void DestroyResource();
  128. Status GetJpegWidthHeight(const void *pic_buffer, size_t pic_buffer_size, uint32_t &image_width,
  129. uint32_t &image_height);
  130. };
  131. } // namespace mindspore::inference
  132. #endif // INC_DVPP_PROCESS_ACL