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.cc 44 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. #include "serving/acl/dvpp_process.h"
  17. #include <jpeglib.h>
  18. #include <fstream>
  19. #include <unordered_map>
  20. #include <nlohmann/json.hpp>
  21. #include "include/infer_log.h"
  22. namespace mindspore {
  23. namespace inference {
  24. DvppProcess::DvppProcess() {}
  25. DvppProcess::~DvppProcess() {}
  26. static uint32_t ToEven(uint32_t num) { return (num + 1) / 2 * 2; }
  27. static uint32_t ToOdd(uint32_t num) {
  28. if (num == 0) {
  29. return 1;
  30. }
  31. return (num + 1) / 2 * 2 - 1;
  32. }
  33. class DvppJsonConfigParser {
  34. public:
  35. DvppJsonConfigParser() = default;
  36. ~DvppJsonConfigParser() = default;
  37. Status InitWithJsonConfig(const std::string &json_config);
  38. DvppDecodePara GetDecodePara() const { return decode_para_; }
  39. DvppResizePara GetResizePara() const { return resize_para_; }
  40. DvppCropPara GetCropPara() const { return crop_para_; }
  41. DvppCropAndPastePara GetCropAndPastePara() const { return crop_and_paste_para_; }
  42. bool HasResizeConfig() const { return resize_flag_; }
  43. bool HasCropConfig() const { return crop_flag_; }
  44. bool HasCropAndPasteConfig() const { return crop_and_paste_flag_; }
  45. private:
  46. DvppDecodePara decode_para_;
  47. DvppResizePara resize_para_;
  48. DvppCropPara crop_para_;
  49. DvppCropAndPastePara crop_and_paste_para_;
  50. bool resize_flag_ = false;
  51. bool crop_flag_ = false;
  52. bool crop_and_paste_flag_ = false;
  53. Status GetStringValue(const nlohmann::json &json_item, const std::string &key, std::string &val);
  54. Status GetIntValue(const nlohmann::json &json_item, const std::string &key, uint32_t &val);
  55. Status ParseInputPara(const nlohmann::json &preprocess_item);
  56. Status ParseDecodePara(const nlohmann::json &preprocess_item);
  57. Status ParseResizePara(const nlohmann::json &json_item);
  58. Status ParseCropPara(const nlohmann::json &json_item);
  59. Status ParseCropAndPastePara(const nlohmann::json &json_item);
  60. Status InitWithJsonConfigImp(const std::string &json_config);
  61. };
  62. Status DvppProcess::InitResource(aclrtStream stream) {
  63. stream_ = stream;
  64. aclError acl_ret;
  65. dvpp_channel_desc_ = acldvppCreateChannelDesc();
  66. if (dvpp_channel_desc_ == nullptr) {
  67. MSI_LOG_ERROR << "acldvppCreateChannelDesc failed";
  68. return FAILED;
  69. }
  70. acl_ret = acldvppCreateChannel(dvpp_channel_desc_);
  71. if (acl_ret != ACL_ERROR_NONE) {
  72. MSI_LOG_ERROR << "acldvppCreateChannel failed, acl return " << acl_ret;
  73. return FAILED;
  74. }
  75. MSI_LOG_INFO << "End init dvpp process resource";
  76. return SUCCESS;
  77. }
  78. void DvppProcess::DestroyResource() {
  79. if (dvpp_channel_desc_ != nullptr) {
  80. auto acl_ret = acldvppDestroyChannel(dvpp_channel_desc_);
  81. if (acl_ret != ACL_ERROR_NONE) {
  82. MSI_LOG_ERROR << "acldvppDestroyChannel failed, acl return " << acl_ret;
  83. }
  84. acl_ret = acldvppDestroyChannelDesc(dvpp_channel_desc_);
  85. if (acl_ret != ACL_ERROR_NONE) {
  86. MSI_LOG_ERROR << "acldvppDestroyChannelDesc failed, acl return " << acl_ret;
  87. }
  88. dvpp_channel_desc_ = nullptr;
  89. }
  90. }
  91. void DvppProcess::Finalize() {
  92. DestroyDecodeDesc();
  93. DestroyVpcOutputDesc();
  94. DestroyResource();
  95. if (resize_config_ != nullptr) {
  96. acldvppDestroyResizeConfig(resize_config_);
  97. resize_config_ = nullptr;
  98. }
  99. if (crop_area_ != nullptr) {
  100. acldvppDestroyRoiConfig(crop_area_);
  101. crop_area_ = nullptr;
  102. }
  103. if (paste_area_ != nullptr) {
  104. acldvppDestroyRoiConfig(paste_area_);
  105. paste_area_ = nullptr;
  106. }
  107. if (input_pic_dev_buffer_ != nullptr) {
  108. acldvppFree(input_pic_dev_buffer_);
  109. }
  110. input_pic_buffer_size_ = 0;
  111. MSI_LOG_INFO << "End dvpp process finalize";
  112. }
  113. Status DvppProcess::InitJpegDecodePara(const DvppDecodePara &decode_para) {
  114. decode_para_ = decode_para;
  115. MSI_LOG_INFO << "Init decode para, pixel_format " << decode_para_.pixel_format;
  116. return SUCCESS;
  117. }
  118. Status DvppProcess::InitResizePara(const DvppResizePara &resize_para) {
  119. resize_para_ = resize_para;
  120. MSI_LOG_INFO << "Init resize para, "
  121. << "output_width " << resize_para_.output_width << ", output_height " << resize_para_.output_height;
  122. to_resize_flag_ = true;
  123. to_crop_flag_ = false;
  124. to_crop_and_paste_flag_ = false;
  125. Status ret = InitResizeOutputDesc();
  126. if (ret != SUCCESS) {
  127. MSI_LOG_ERROR << "InitResizeOutputDesc failed";
  128. }
  129. return ret;
  130. }
  131. Status DvppProcess::InitCommonCropPara(DvppCropInfo &crop_info, uint32_t output_width, uint32_t output_height) {
  132. if (crop_info.crop_type == kDvppCropTypeOffset) {
  133. if (CheckAndAdjustRoiArea(crop_info.crop_area) != SUCCESS) {
  134. MSI_LOG_ERROR << "Check and adjust crop area failed";
  135. return FAILED;
  136. }
  137. MSI_LOG_INFO << "Init common crop para, crop type offset "
  138. << ", left " << crop_info.crop_area.left << ", right " << crop_info.crop_area.right << ", top "
  139. << crop_info.crop_area.top << ", bottom " << crop_info.crop_area.bottom << ", output_width "
  140. << output_width << ", output_height " << output_height;
  141. } else {
  142. crop_info.crop_width = ToEven(crop_info.crop_width);
  143. crop_info.crop_height = ToEven(crop_info.crop_height);
  144. if (CheckRoiAreaWidthHeight(crop_info.crop_width, crop_info.crop_height) != SUCCESS) {
  145. MSI_LOG_ERROR << "Check crop area width and height failed, actually width " << crop_info.crop_width << " height "
  146. << crop_info.crop_height;
  147. return FAILED;
  148. }
  149. MSI_LOG_INFO << "Init common crop para, crop type centre "
  150. << ", crop_width " << crop_info.crop_width << ", crop_height " << crop_info.crop_height
  151. << ", output_width " << output_width << ", output_height " << output_height;
  152. }
  153. return SUCCESS;
  154. }
  155. Status DvppProcess::InitCropPara(const DvppCropPara &crop_para) {
  156. crop_para_ = crop_para;
  157. if (InitCommonCropPara(crop_para_.crop_info, crop_para_.output_width, crop_para_.output_height) != SUCCESS) {
  158. MSI_LOG_ERROR << "Init common crop para failed in InitCropPara";
  159. return FAILED;
  160. }
  161. to_crop_flag_ = true;
  162. to_resize_flag_ = false;
  163. to_crop_and_paste_flag_ = false;
  164. Status ret = InitCropOutputDesc();
  165. if (ret != SUCCESS) {
  166. MSI_LOG_ERROR << "InitCropOutputDesc failed";
  167. }
  168. return ret;
  169. }
  170. Status DvppProcess::InitCropAndPastePara(const DvppCropAndPastePara &crop_and_paste_para) {
  171. crop_and_paste_para_ = crop_and_paste_para;
  172. if (InitCommonCropPara(crop_and_paste_para_.crop_info, crop_and_paste_para_.output_width,
  173. crop_and_paste_para_.output_height) != SUCCESS) {
  174. MSI_LOG_ERROR << "Init common crop para failed in InitCropAndPastePara";
  175. return FAILED;
  176. }
  177. auto &paste_area = crop_and_paste_para_.paste_area;
  178. if (CheckAndAdjustRoiArea(paste_area) != SUCCESS) {
  179. MSI_LOG_ERROR << "Check and adjust paste area failed";
  180. return FAILED;
  181. }
  182. MSI_LOG_INFO << "Init crop and paste para, paste info: "
  183. << ", left " << paste_area.left << ", right " << paste_area.right << ", top " << paste_area.top
  184. << ", bottom " << paste_area.bottom;
  185. to_crop_and_paste_flag_ = true;
  186. to_crop_flag_ = false;
  187. to_resize_flag_ = false;
  188. Status ret = InitCropAndPasteOutputDesc();
  189. if (ret != SUCCESS) {
  190. MSI_LOG_ERROR << "InitCropAndPasteOutputDesc failed";
  191. }
  192. return ret;
  193. }
  194. Status DvppProcess::InputInputBuffer(const void *pic_buffer, size_t pic_buffer_size) {
  195. aclError acl_ret;
  196. if (pic_buffer_size != input_pic_buffer_size_) {
  197. acldvppFree(input_pic_dev_buffer_);
  198. input_pic_buffer_size_ = 0;
  199. acl_ret = acldvppMalloc(&input_pic_dev_buffer_, pic_buffer_size);
  200. if (acl_ret != ACL_ERROR_NONE) {
  201. MSI_LOG_ERROR << "acldvppMalloc input picture buffer on device failed, buffer size " << pic_buffer_size;
  202. return FAILED;
  203. }
  204. input_pic_buffer_size_ = pic_buffer_size;
  205. }
  206. acl_ret =
  207. aclrtMemcpy(input_pic_dev_buffer_, input_pic_buffer_size_, pic_buffer, pic_buffer_size, ACL_MEMCPY_HOST_TO_DEVICE);
  208. if (acl_ret != ACL_ERROR_NONE) {
  209. MSI_LOG_ERROR << "aclrtMemcpy input picture buffer to device, buffer size " << pic_buffer_size;
  210. return FAILED;
  211. }
  212. return SUCCESS;
  213. }
  214. static void JpegErrorExitCustom(j_common_ptr cinfo) {
  215. char jpeg_last_error_msg[JMSG_LENGTH_MAX] = {0};
  216. if (cinfo != nullptr && cinfo->err != nullptr && cinfo->err->format_message != nullptr) {
  217. (*(cinfo->err->format_message))(cinfo, jpeg_last_error_msg);
  218. }
  219. throw std::runtime_error(jpeg_last_error_msg);
  220. }
  221. Status DvppProcess::GetJpegWidthHeight(const void *pic_buffer, size_t pic_buffer_size, uint32_t &image_width,
  222. uint32_t &image_height) {
  223. struct jpeg_decompress_struct jpeg_header;
  224. struct jpeg_error_mgr jpeg_error;
  225. jpeg_header.err = jpeg_std_error(&jpeg_error);
  226. jpeg_error.error_exit = JpegErrorExitCustom;
  227. try {
  228. jpeg_create_decompress(&jpeg_header);
  229. jpeg_mem_src(&jpeg_header, reinterpret_cast<const unsigned char *>(pic_buffer), pic_buffer_size);
  230. (void)jpeg_read_header(&jpeg_header, TRUE);
  231. } catch (std::runtime_error &e) {
  232. jpeg_destroy_decompress(&jpeg_header);
  233. MSI_LOG_ERROR << "jpeg images read failed, " << e.what();
  234. return INFER_STATUS(INVALID_INPUTS) << "jpeg images decode failed";
  235. }
  236. image_width = jpeg_header.image_width;
  237. image_height = jpeg_header.image_height;
  238. if (jpeg_header.jpeg_color_space != JCS_YCbCr) {
  239. MSI_LOG_ERROR << "Expect color space YUV(YCbCr), current " << jpeg_header.jpeg_color_space;
  240. jpeg_destroy_decompress(&jpeg_header);
  241. return INFER_STATUS(INVALID_INPUTS) << "Expect color space YUV(YCbCr), current " << jpeg_header.jpeg_color_space;
  242. }
  243. if (jpeg_header.dc_huff_tbl_ptrs[0] == nullptr) {
  244. MSI_LOG_ERROR << "Only support Huffman code";
  245. jpeg_destroy_decompress(&jpeg_header);
  246. return INFER_STATUS(INVALID_INPUTS) << "Only support Huffman code";
  247. }
  248. jpeg_destroy_decompress(&jpeg_header);
  249. const uint32_t min_width = 32;
  250. const uint32_t max_width = 8192;
  251. const uint32_t min_height = 32;
  252. const uint32_t max_height = 8192;
  253. if (image_width < min_width || image_width > max_width) {
  254. MSI_LOG_ERROR << "expect image width [" << min_width << ", " << max_width << "], the real image width is "
  255. << image_width;
  256. return INFER_STATUS(INVALID_INPUTS) << "expect image width [" << min_width << ", " << max_width
  257. << "], the real image width is " << image_width;
  258. }
  259. if (image_height < min_height || image_height > max_height) {
  260. MSI_LOG_ERROR << "expect image height [" << min_height << ", " << max_height << "], the real image height is "
  261. << image_height;
  262. return INFER_STATUS(INVALID_INPUTS) << "expect image height [" << min_height << ", " << max_height
  263. << "], the real image height is " << image_height;
  264. }
  265. return SUCCESS;
  266. }
  267. Status DvppProcess::Process(const void *pic_buffer, size_t pic_buffer_size, void *&output_device_buffer,
  268. size_t &output_size) {
  269. if (dvpp_channel_desc_ == nullptr) {
  270. MSI_LOG_ERROR << "Process failed, dvpp not inited";
  271. return FAILED;
  272. }
  273. uint32_t image_width = 0;
  274. uint32_t image_height = 0;
  275. Status ret = GetJpegWidthHeight(pic_buffer, pic_buffer_size, image_width, image_height);
  276. if (ret != SUCCESS) {
  277. MSI_LOG_ERROR << "Get jpeg image height and width failed";
  278. return ret;
  279. }
  280. MSI_LOG_INFO << "Get jpeg width " << image_width << ", height " << image_height;
  281. ret = InitDecodeOutputDesc(image_width, image_height);
  282. if (ret != SUCCESS) {
  283. MSI_LOG_ERROR << "InitDecodeOutputDesc failed";
  284. return FAILED;
  285. }
  286. ret = UpdateCropArea(image_width, image_height);
  287. if (ret != SUCCESS) {
  288. MSI_LOG_ERROR << "Update crop area failed";
  289. return ret;
  290. }
  291. ret = CheckResizeImageInfo(image_width, image_height);
  292. if (ret != SUCCESS) {
  293. MSI_LOG_ERROR << "Check resize para failed";
  294. return ret;
  295. }
  296. if (InputInputBuffer(pic_buffer, pic_buffer_size) != SUCCESS) {
  297. MSI_LOG_ERROR << "InputInputBuffer failed";
  298. return FAILED;
  299. }
  300. if (ProcessDecode() != SUCCESS) {
  301. MSI_LOG_ERROR << "Process Decode failed";
  302. return INFER_STATUS(INVALID_INPUTS) << "Decode image failed";
  303. }
  304. MSI_LOG_INFO << "Process Decode success";
  305. if (to_resize_flag_) {
  306. if (ProcessResize() != SUCCESS) {
  307. MSI_LOG_ERROR << "Process Resize failed";
  308. return INFER_STATUS(FAILED) << "Resize image failed";
  309. }
  310. MSI_LOG_INFO << "Process Resize success";
  311. } else if (to_crop_flag_) {
  312. if (ProcessCrop() != SUCCESS) {
  313. MSI_LOG_ERROR << "Process Crop failed";
  314. return INFER_STATUS(FAILED) << "Crop image failed";
  315. }
  316. MSI_LOG_INFO << "Process Crop success";
  317. } else if (to_crop_and_paste_flag_) {
  318. if (ProcessCropAndPaste() != SUCCESS) {
  319. MSI_LOG_ERROR << "Process Crop And Paste failed";
  320. return INFER_STATUS(FAILED) << "Crop And Paste image failed";
  321. }
  322. MSI_LOG_INFO << "Process Crop And Paste success";
  323. }
  324. if (vpc_output_buffer_dev_ == nullptr) {
  325. output_device_buffer = decode_output_buffer_dev_;
  326. output_size = decode_output_buffer_size_;
  327. } else {
  328. output_device_buffer = vpc_output_buffer_dev_;
  329. output_size = vpc_output_buffer_size_;
  330. }
  331. MSI_LOG_INFO << "Process dvpp success";
  332. return SUCCESS;
  333. }
  334. Status DvppProcess::Process(const std::vector<const void *> &pic_buffer_list,
  335. const std::vector<size_t> &pic_buffer_size_list, void *&output_device_buffer,
  336. size_t &output_size) {
  337. auto batch_size = pic_buffer_list.size();
  338. if (batch_size == 0 || batch_size != pic_buffer_size_list.size()) {
  339. MSI_LOG_ERROR << "invalid batch size " << batch_size << ", pic size count" << pic_buffer_size_list.size();
  340. return FAILED;
  341. }
  342. MSI_LOG_INFO << "Begin dvpp process, batch size " << batch_size;
  343. if (batch_size == 1) {
  344. return Process(pic_buffer_list[0], pic_buffer_size_list[0], output_device_buffer, output_size);
  345. }
  346. size_t total_buffer_size = vpc_output_buffer_size_ * batch_size;
  347. if (batch_size_ != batch_size) {
  348. if (batch_vpc_output_buffer_dev_ != nullptr) {
  349. acldvppFree(batch_vpc_output_buffer_dev_);
  350. batch_vpc_output_buffer_dev_ = nullptr;
  351. }
  352. batch_size_ = batch_size;
  353. auto acl_rt = acldvppMalloc(&batch_vpc_output_buffer_dev_, total_buffer_size);
  354. if (acl_rt != ACL_ERROR_NONE) {
  355. MSI_LOG_ERROR << "acldvppMalloc failed, buffer size " << total_buffer_size;
  356. return FAILED;
  357. }
  358. }
  359. for (size_t i = 0; i < batch_size; i++) {
  360. const void *pic_buffer = pic_buffer_list[i];
  361. uint32_t pic_size = pic_buffer_size_list[i];
  362. if (pic_buffer == nullptr || pic_size == 0) {
  363. MSI_LOG_ERROR << "Get " << 0 << "th images failed";
  364. return FAILED;
  365. }
  366. void *output_dev_buffer_tmp = nullptr;
  367. size_t output_buffer_size_tmp = 0;
  368. Status ret = Process(pic_buffer, pic_size, output_dev_buffer_tmp, output_buffer_size_tmp);
  369. if (ret != SUCCESS) {
  370. MSI_LOG_ERROR << "dvpp process failed";
  371. return ret;
  372. }
  373. aclrtMemcpy(static_cast<uint8_t *>(batch_vpc_output_buffer_dev_) + vpc_output_buffer_size_ * i,
  374. total_buffer_size - vpc_output_buffer_size_ * i, output_dev_buffer_tmp, vpc_output_buffer_size_,
  375. ACL_MEMCPY_DEVICE_TO_DEVICE);
  376. MSI_LOG_INFO << "Dvpp process " << i << " th images success, input pic size " << pic_size << " output buffer size "
  377. << output_buffer_size_tmp;
  378. }
  379. output_device_buffer = batch_vpc_output_buffer_dev_;
  380. output_size = total_buffer_size;
  381. MSI_LOG_INFO << "End dvpp process, batch size " << batch_size << ", output size " << output_size;
  382. return SUCCESS;
  383. }
  384. uint32_t DvppProcess::AlignmentHelper(uint32_t org_size, uint32_t alignment) const {
  385. if (alignment == 0) {
  386. return 0;
  387. }
  388. return (org_size + alignment - 1) / alignment * alignment;
  389. }
  390. uint32_t DvppProcess::GetImageBufferSize(uint32_t stride_width, uint32_t stride_height,
  391. acldvppPixelFormat pixel_format) const {
  392. if (stride_height == 0 || stride_width == 0) {
  393. MSI_LOG_ERROR << "invalid stride height or width, stride_width " << stride_width << " stride_height "
  394. << stride_height;
  395. return 0;
  396. }
  397. if (UINT32_MAX / 3 < stride_height || UINT32_MAX / (3 * stride_height) < stride_width) {
  398. MSI_LOG_ERROR << "invalid stride height or width, stride_width " << stride_width << " stride_height "
  399. << stride_height;
  400. return 0;
  401. }
  402. if (pixel_format == PIXEL_FORMAT_YUV_SEMIPLANAR_420 || pixel_format == PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
  403. return stride_width * stride_height * 3 / 2; // 420
  404. } else if (pixel_format == PIXEL_FORMAT_YUV_SEMIPLANAR_422 || pixel_format == PIXEL_FORMAT_YVU_SEMIPLANAR_422) {
  405. return stride_width * stride_height * 2; // 422
  406. } else if (pixel_format == PIXEL_FORMAT_YUV_SEMIPLANAR_444 || pixel_format == PIXEL_FORMAT_YVU_SEMIPLANAR_444) {
  407. return stride_width * stride_height * 3; // 444
  408. }
  409. MSI_LOG_ERROR << "Not support pixel format " << pixel_format;
  410. return 0;
  411. }
  412. Status DvppProcess::GetPicDescStride(uint32_t width, uint32_t height, uint32_t &stride_width, uint32_t &stride_height) {
  413. const uint32_t width_alignment = 16;
  414. const uint32_t height_alignment = 2;
  415. const uint32_t stride_width_minimum = 32;
  416. const uint32_t stride_width_maximum = 4096;
  417. const uint32_t stride_height_minimum = 6;
  418. const uint32_t stride_height_maximum = 4096;
  419. stride_width = AlignmentHelper(width, width_alignment);
  420. stride_height = AlignmentHelper(height, height_alignment);
  421. if (stride_width == 0 || stride_height == 0) {
  422. MSI_LOG_ERROR << "Init VPC output desc failed, get stride width or height failed";
  423. return FAILED;
  424. }
  425. if (stride_width < stride_width_minimum || stride_width > stride_width_maximum) {
  426. MSI_LOG_ERROR << "Expect stride width [" << stride_width_minimum << ", " << stride_width_maximum
  427. << "], current stride width " << stride_width << " given width " << width;
  428. return FAILED;
  429. }
  430. if (stride_height < stride_height_minimum || stride_height > stride_height_maximum) {
  431. MSI_LOG_ERROR << "Expect stride height [" << stride_height_minimum << ", " << stride_height_maximum
  432. << "], current stride height " << stride_height << " given height " << height;
  433. return FAILED;
  434. }
  435. return SUCCESS;
  436. }
  437. Status DvppProcess::GetPicDescStrideDecode(uint32_t width, uint32_t height, uint32_t &stride_width,
  438. uint32_t &stride_height) {
  439. const uint32_t width_alignment = 128;
  440. const uint32_t height_alignment = 16;
  441. const uint32_t width_minimum = 32;
  442. const uint32_t width_maximum = 4096; // decode support 8192, dvpp(resize/crop/crop&paste) support 4096
  443. const uint32_t height_minimum = 32;
  444. const uint32_t height_maximum = 4096; // decode support 8192, dvpp(resize/crop/crop&paste) support 4096
  445. if (width < width_minimum || width > width_maximum) {
  446. MSI_LOG_ERROR << "Expect width [" << width_minimum << ", " << width_maximum << "], current width " << width;
  447. return INFER_STATUS(INVALID_INPUTS) << "Expect width [" << width_minimum << ", " << width_maximum
  448. << "], current width " << width;
  449. }
  450. if (height < height_minimum || height > height_maximum) {
  451. MSI_LOG_ERROR << "Expect height [" << height_minimum << ", " << height_maximum << "], current height " << height;
  452. return INFER_STATUS(INVALID_INPUTS) << "Expect height [" << height_minimum << ", " << height_maximum
  453. << "], current height " << height;
  454. }
  455. stride_width = AlignmentHelper(width, width_alignment);
  456. stride_height = AlignmentHelper(height, height_alignment);
  457. if (stride_width == 0 || stride_height == 0) {
  458. MSI_LOG_ERROR << "Init decode output desc failed, get stride width or height failed";
  459. return FAILED;
  460. }
  461. return SUCCESS;
  462. }
  463. Status DvppProcess::InitVpcOutputDesc(uint32_t output_width, uint32_t output_height, acldvppPixelFormat pixel_format) {
  464. DestroyVpcOutputDesc();
  465. uint32_t vpc_stride_width = 0;
  466. uint32_t vpc_stride_height = 0;
  467. if (GetPicDescStride(output_width, output_height, vpc_stride_width, vpc_stride_height) != SUCCESS) {
  468. MSI_LOG_ERROR << "Init VPC output desc failed, get VPC output stride width/height failed";
  469. return FAILED;
  470. }
  471. vpc_output_buffer_size_ = GetImageBufferSize(vpc_stride_width, vpc_stride_height, pixel_format);
  472. if (vpc_output_buffer_size_ == 0) {
  473. MSI_LOG_ERROR << "Init VPC output desc failed, get image buffer size failed";
  474. return FAILED;
  475. }
  476. auto acl_ret = acldvppMalloc(&vpc_output_buffer_dev_, vpc_output_buffer_size_);
  477. if (acl_ret != ACL_ERROR_NONE) {
  478. MSI_LOG_ERROR << "Init VPC output desc failed, malloc dvpp memory failed";
  479. return FAILED;
  480. }
  481. vpc_output_desc_ = acldvppCreatePicDesc();
  482. if (vpc_output_desc_ == nullptr) {
  483. MSI_LOG_ERROR << "Init VPC output desc failed, create pic desc failed";
  484. return FAILED;
  485. }
  486. acldvppSetPicDescData(vpc_output_desc_, vpc_output_buffer_dev_);
  487. acldvppSetPicDescSize(vpc_output_desc_, vpc_output_buffer_size_);
  488. acldvppSetPicDescFormat(vpc_output_desc_, pixel_format);
  489. acldvppSetPicDescWidth(vpc_output_desc_, output_width);
  490. acldvppSetPicDescHeight(vpc_output_desc_, output_height);
  491. acldvppSetPicDescWidthStride(vpc_output_desc_, vpc_stride_width);
  492. acldvppSetPicDescHeightStride(vpc_output_desc_, vpc_stride_height);
  493. MSI_LOG_INFO << "Init VPC output desc success";
  494. return SUCCESS;
  495. }
  496. void DvppProcess::DestroyVpcOutputDesc() {
  497. if (vpc_output_desc_ != nullptr) {
  498. acldvppDestroyPicDesc(vpc_output_desc_);
  499. vpc_output_desc_ = nullptr;
  500. }
  501. if (vpc_output_buffer_dev_ != nullptr) {
  502. acldvppFree(vpc_output_buffer_dev_);
  503. vpc_output_buffer_dev_ = nullptr;
  504. }
  505. if (batch_vpc_output_buffer_dev_ != nullptr) {
  506. acldvppFree(batch_vpc_output_buffer_dev_);
  507. batch_vpc_output_buffer_dev_ = nullptr;
  508. }
  509. vpc_output_buffer_size_ = 0;
  510. MSI_LOG_INFO << "End destroy vpc desc";
  511. }
  512. Status DvppProcess::InitDecodeOutputDesc(uint32_t image_width, uint32_t image_height) {
  513. if (decode_output_buffer_dev_ != nullptr && image_width == pic_width_ && image_height == pic_height_) {
  514. return SUCCESS;
  515. }
  516. DestroyDecodeDesc();
  517. pic_width_ = image_width;
  518. pic_height_ = image_height;
  519. uint32_t stride_width = 0;
  520. uint32_t stride_height = 0;
  521. Status ret = GetPicDescStrideDecode(pic_width_, pic_height_, stride_width, stride_height);
  522. if (ret != SUCCESS) {
  523. MSI_LOG_ERROR << "Init VPC output desc failed, get VPC output stride width/height failed";
  524. return ret;
  525. }
  526. decode_output_buffer_size_ = GetImageBufferSize(stride_width, stride_height, decode_para_.pixel_format);
  527. if (decode_output_buffer_size_ == 0) {
  528. MSI_LOG_ERROR << "Init decode output desc failed, get image buffer size failed";
  529. return FAILED;
  530. }
  531. auto acl_ret = acldvppMalloc(&decode_output_buffer_dev_, decode_output_buffer_size_);
  532. if (acl_ret != ACL_ERROR_NONE) {
  533. MSI_LOG_ERROR << "Init decode output desc failed, malloc dvpp memory failed";
  534. return FAILED;
  535. }
  536. decode_output_desc_ = acldvppCreatePicDesc();
  537. if (decode_output_desc_ == nullptr) {
  538. MSI_LOG_ERROR << "Init decode output desc failed, create pic desc failed";
  539. return FAILED;
  540. }
  541. acldvppSetPicDescData(decode_output_desc_, decode_output_buffer_dev_);
  542. acldvppSetPicDescSize(decode_output_desc_, decode_output_buffer_size_);
  543. acldvppSetPicDescFormat(decode_output_desc_, decode_para_.pixel_format);
  544. acldvppSetPicDescWidth(decode_output_desc_, pic_width_);
  545. acldvppSetPicDescHeight(decode_output_desc_, pic_height_);
  546. acldvppSetPicDescWidthStride(decode_output_desc_, stride_width);
  547. acldvppSetPicDescHeightStride(decode_output_desc_, stride_height);
  548. MSI_LOG_INFO << "Init decode output desc success";
  549. return SUCCESS;
  550. }
  551. Status DvppProcess::CheckRoiAreaWidthHeight(uint32_t width, uint32_t height) {
  552. const uint32_t min_crop_width = 10;
  553. const uint32_t max_crop_width = 4096;
  554. const uint32_t min_crop_height = 6;
  555. const uint32_t max_crop_height = 4096;
  556. if (width < min_crop_width || width > max_crop_width) {
  557. MSI_LOG_ERROR << "Expect roi area width in [" << min_crop_width << ", " << max_crop_width << "], actually "
  558. << width;
  559. return FAILED;
  560. }
  561. if (height < min_crop_height || height > max_crop_height) {
  562. MSI_LOG_ERROR << "Expect roi area height in [" << min_crop_height << ", " << max_crop_height << "], actually "
  563. << height;
  564. return FAILED;
  565. }
  566. return SUCCESS;
  567. }
  568. Status DvppProcess::CheckAndAdjustRoiArea(DvppRoiArea &area) {
  569. if (area.right < area.left) {
  570. MSI_LOG_ERROR << "check roi area failed, left " << area.left << ", right " << area.right;
  571. return FAILED;
  572. }
  573. if (area.bottom < area.top) {
  574. MSI_LOG_ERROR << "check roi area failed, top " << area.top << ", bottom " << area.bottom;
  575. return FAILED;
  576. }
  577. area.left = ToEven(area.left);
  578. area.top = ToEven(area.top);
  579. area.right = ToOdd(area.right);
  580. area.bottom = ToOdd(area.bottom);
  581. auto width = area.right - area.left + 1;
  582. auto height = area.bottom - area.top + 1;
  583. if (CheckRoiAreaWidthHeight(width, height) != SUCCESS) {
  584. MSI_LOG_ERROR << "Check roi area width and height failed,"
  585. << " actually width " << width << " left " << area.left << ", right " << area.right
  586. << " actually height " << height << " top " << area.top << ", bottom " << area.bottom;
  587. return FAILED;
  588. }
  589. return SUCCESS;
  590. }
  591. Status DvppProcess::UpdateCropArea(uint32_t image_width, uint32_t image_height) {
  592. DvppCropInfo *crop_info = nullptr;
  593. if (to_crop_flag_) {
  594. crop_info = &crop_para_.crop_info;
  595. } else if (to_crop_and_paste_flag_) {
  596. crop_info = &crop_and_paste_para_.crop_info;
  597. } else {
  598. return SUCCESS;
  599. }
  600. if (crop_info->crop_type != kDvppCropTypeCentre) {
  601. return SUCCESS;
  602. }
  603. if (image_width < crop_info->crop_width) {
  604. MSI_LOG_ERROR << "Image width " << image_width << "smaller than crop width " << crop_info->crop_width;
  605. return INFER_STATUS(INVALID_INPUTS) << "Image width " << image_width << "smaller than crop width "
  606. << crop_info->crop_width;
  607. }
  608. if (image_height < crop_info->crop_height) {
  609. MSI_LOG_ERROR << "Image height " << image_height << "smaller than crop height " << crop_info->crop_height;
  610. return INFER_STATUS(INVALID_INPUTS) << "Image width " << image_width << "smaller than crop width "
  611. << crop_info->crop_width;
  612. }
  613. uint32_t left = ToEven((image_width - crop_info->crop_width) / 2);
  614. uint32_t top = ToEven((image_height - crop_info->crop_height) / 2);
  615. uint32_t right = ToOdd(left + crop_info->crop_width);
  616. uint32_t bottom = ToOdd(top + crop_info->crop_height);
  617. auto acl_ret = acldvppSetRoiConfig(crop_area_, left, right, top, bottom);
  618. if (acl_ret != ACL_ERROR_NONE) {
  619. MSI_LOG_ERROR << "Update Crop Area failed";
  620. return FAILED;
  621. }
  622. MSI_LOG_INFO << "Update crop area, crop type centre, crop info: "
  623. << ", left " << left << ", right " << right << ", top " << top << ", bottom " << bottom;
  624. return SUCCESS;
  625. }
  626. Status DvppProcess::CheckResizeImageInfo(uint32_t image_width, uint32_t image_height) const {
  627. if (!to_resize_flag_) {
  628. return SUCCESS;
  629. }
  630. // resize ratio required [1/32, 16]
  631. auto check_resize_ratio = [](uint32_t before_resize, uint32_t after_resize) {
  632. if (before_resize == 0 || after_resize == 0) {
  633. return false;
  634. }
  635. if (before_resize / after_resize > 32) {
  636. return false;
  637. }
  638. if (after_resize / before_resize > 16) {
  639. return false;
  640. }
  641. return true;
  642. };
  643. if (!check_resize_ratio(image_width, resize_para_.output_width)) {
  644. MSI_LOG_ERROR << "Resize ratio required [1/32, 16], current width resize from " << image_width << " to "
  645. << resize_para_.output_width;
  646. return INFER_STATUS(INVALID_INPUTS) << "Resize ratio required [1/32, 16], current width resize from " << image_width
  647. << " to " << resize_para_.output_width;
  648. }
  649. if (!check_resize_ratio(image_height, resize_para_.output_height)) {
  650. MSI_LOG_ERROR << "Resize ratio required [1/32, 16], current height resize from " << image_height << " to "
  651. << resize_para_.output_height;
  652. return INFER_STATUS(INVALID_INPUTS) << "Resize ratio required [1/32, 16], current height resize from "
  653. << image_height << " to " << resize_para_.output_height;
  654. }
  655. return SUCCESS;
  656. }
  657. void DvppProcess::DestroyDecodeDesc() {
  658. if (decode_output_desc_ != nullptr) {
  659. acldvppDestroyPicDesc(decode_output_desc_);
  660. decode_output_desc_ = nullptr;
  661. }
  662. if (decode_output_buffer_dev_ != nullptr) {
  663. acldvppFree(decode_output_buffer_dev_);
  664. decode_output_buffer_dev_ = nullptr;
  665. }
  666. decode_output_buffer_size_ = 0;
  667. MSI_LOG_INFO << "End destroy decode desc";
  668. }
  669. Status DvppProcess::InitResizeOutputDesc() {
  670. if (InitVpcOutputDesc(resize_para_.output_width, resize_para_.output_height, decode_para_.pixel_format) != SUCCESS) {
  671. MSI_LOG_ERROR << "Init VPC output desc failed";
  672. return FAILED;
  673. }
  674. if (resize_config_ == nullptr) {
  675. resize_config_ = acldvppCreateResizeConfig();
  676. if (resize_config_ == nullptr) {
  677. MSI_LOG_ERROR << "Create Resize config failed";
  678. return FAILED;
  679. }
  680. }
  681. return SUCCESS;
  682. }
  683. Status DvppProcess::InitRoiAreaConfig(acldvppRoiConfig *&roi_area, const DvppRoiArea &init_para) {
  684. if (roi_area == nullptr) {
  685. roi_area = acldvppCreateRoiConfig(init_para.left, init_para.right, init_para.top, init_para.bottom);
  686. if (roi_area == nullptr) {
  687. MSI_LOG_ERROR << "Create Roi config failed";
  688. return FAILED;
  689. }
  690. } else {
  691. auto acl_ret = acldvppSetRoiConfig(roi_area, init_para.left, init_para.right, init_para.top, init_para.bottom);
  692. if (acl_ret != ACL_ERROR_NONE) {
  693. MSI_LOG_ERROR << "Set Roi config failed";
  694. return FAILED;
  695. }
  696. }
  697. return SUCCESS;
  698. }
  699. Status DvppProcess::InitCropOutputDesc() {
  700. if (InitVpcOutputDesc(crop_para_.output_width, crop_para_.output_height, decode_para_.pixel_format) != SUCCESS) {
  701. MSI_LOG_ERROR << "Init VPC output desc failed";
  702. return FAILED;
  703. }
  704. if (InitRoiAreaConfig(crop_area_, crop_para_.crop_info.crop_area) != SUCCESS) {
  705. MSI_LOG_ERROR << "Init crop area failed";
  706. return FAILED;
  707. }
  708. return SUCCESS;
  709. }
  710. Status DvppProcess::InitCropAndPasteOutputDesc() {
  711. if (InitVpcOutputDesc(crop_and_paste_para_.output_width, crop_and_paste_para_.output_height,
  712. decode_para_.pixel_format) != SUCCESS) {
  713. MSI_LOG_ERROR << "Init VPC output desc failed";
  714. return FAILED;
  715. }
  716. if (InitRoiAreaConfig(crop_area_, crop_and_paste_para_.crop_info.crop_area) != SUCCESS) {
  717. MSI_LOG_ERROR << "Init crop area failed";
  718. return FAILED;
  719. }
  720. if (InitRoiAreaConfig(paste_area_, crop_and_paste_para_.paste_area) != SUCCESS) {
  721. MSI_LOG_ERROR << "Init paste area failed";
  722. return FAILED;
  723. }
  724. return SUCCESS;
  725. }
  726. Status DvppProcess::ProcessDecode() {
  727. aclError acl_ret;
  728. acl_ret = acldvppJpegDecodeAsync(dvpp_channel_desc_, input_pic_dev_buffer_, input_pic_buffer_size_,
  729. decode_output_desc_, stream_);
  730. if (acl_ret != ACL_ERROR_NONE) {
  731. MSI_LOG_ERROR << "acldvppJpegDecodeAsync failed, acl return " << acl_ret;
  732. return FAILED;
  733. }
  734. acl_ret = aclrtSynchronizeStream(stream_);
  735. if (acl_ret != ACL_ERROR_NONE) {
  736. MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
  737. return FAILED;
  738. }
  739. return SUCCESS;
  740. }
  741. Status DvppProcess::ProcessResize() {
  742. aclError acl_ret;
  743. acl_ret = acldvppVpcResizeAsync(dvpp_channel_desc_, decode_output_desc_, vpc_output_desc_, resize_config_, stream_);
  744. if (acl_ret != ACL_ERROR_NONE) {
  745. MSI_LOG_ERROR << "acldvppVpcResizeAsync failed, acl return " << acl_ret;
  746. return FAILED;
  747. }
  748. acl_ret = aclrtSynchronizeStream(stream_);
  749. if (acl_ret != ACL_ERROR_NONE) {
  750. MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
  751. return FAILED;
  752. }
  753. return SUCCESS;
  754. }
  755. Status DvppProcess::ProcessCrop() {
  756. aclError acl_ret;
  757. acl_ret = acldvppVpcCropAsync(dvpp_channel_desc_, decode_output_desc_, vpc_output_desc_, crop_area_, stream_);
  758. if (acl_ret != ACL_ERROR_NONE) {
  759. MSI_LOG_ERROR << "acldvppVpcCropAsync failed, acl return " << acl_ret;
  760. return FAILED;
  761. }
  762. acl_ret = aclrtSynchronizeStream(stream_);
  763. if (acl_ret != ACL_ERROR_NONE) {
  764. MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
  765. return FAILED;
  766. }
  767. return SUCCESS;
  768. }
  769. Status DvppProcess::ProcessCropAndPaste() {
  770. aclError acl_ret;
  771. acl_ret = acldvppVpcCropAndPasteAsync(dvpp_channel_desc_, decode_output_desc_, vpc_output_desc_, crop_area_,
  772. paste_area_, stream_);
  773. if (acl_ret != ACL_ERROR_NONE) {
  774. MSI_LOG_ERROR << "acldvppVpcCropAndPasteAsync failed, acl return " << acl_ret;
  775. return FAILED;
  776. }
  777. acl_ret = aclrtSynchronizeStream(stream_);
  778. if (acl_ret != ACL_ERROR_NONE) {
  779. MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
  780. return FAILED;
  781. }
  782. return SUCCESS;
  783. }
  784. Status DvppJsonConfigParser::GetStringValue(const nlohmann::json &json_item, const std::string &key, std::string &val) {
  785. auto it = json_item.find(key);
  786. if (it == json_item.end()) {
  787. MSI_LOG_ERROR << "get string item " << key << " failed";
  788. return FAILED;
  789. }
  790. if (!it->is_string()) {
  791. MSI_LOG_ERROR << "item " << key << " value is not string type";
  792. return FAILED;
  793. }
  794. val = it->get<std::string>();
  795. return SUCCESS;
  796. }
  797. Status DvppJsonConfigParser::GetIntValue(const nlohmann::json &json_item, const std::string &key, uint32_t &val) {
  798. auto it = json_item.find(key);
  799. if (it == json_item.end()) {
  800. MSI_LOG_ERROR << "get string item " << key << " failed";
  801. return FAILED;
  802. }
  803. if (!it->is_number_integer()) {
  804. MSI_LOG_ERROR << "item " << key << " value is not integer type";
  805. return FAILED;
  806. }
  807. val = it->get<uint32_t>();
  808. return SUCCESS;
  809. }
  810. Status DvppJsonConfigParser::ParseInputPara(const nlohmann::json &preprocess_item) {
  811. auto input = preprocess_item.find("input");
  812. if (input == preprocess_item.end()) {
  813. MSI_LOG_ERROR << "get input failed";
  814. return FAILED;
  815. }
  816. if (!input->is_object()) {
  817. MSI_LOG_ERROR << "input is not object";
  818. return FAILED;
  819. }
  820. return SUCCESS;
  821. }
  822. Status DvppJsonConfigParser::ParseDecodePara(const nlohmann::json &preprocess_item) {
  823. auto decode_para = preprocess_item.find("decode_para");
  824. if (decode_para == preprocess_item.end()) {
  825. MSI_LOG_ERROR << "get input failed";
  826. return FAILED;
  827. }
  828. if (!decode_para->is_object()) {
  829. MSI_LOG_ERROR << "input is not object";
  830. return FAILED;
  831. }
  832. const std::unordered_map<std::string, acldvppPixelFormat> pixel_format_map = {
  833. {"YUV420SP", PIXEL_FORMAT_YUV_SEMIPLANAR_420}, {"YVU420SP", PIXEL_FORMAT_YVU_SEMIPLANAR_420},
  834. {"YUV422SP", PIXEL_FORMAT_YUV_SEMIPLANAR_422}, {"YVU422SP", PIXEL_FORMAT_YVU_SEMIPLANAR_422},
  835. {"YUV444SP", PIXEL_FORMAT_YUV_SEMIPLANAR_444}, {"YVU444SP", PIXEL_FORMAT_YVU_SEMIPLANAR_444},
  836. };
  837. std::string pixel_format;
  838. if (GetStringValue(*decode_para, "out_pixel_format", pixel_format) != SUCCESS) {
  839. MSI_LOG_ERROR << "get op out_pixel_format failed";
  840. return FAILED;
  841. }
  842. auto format = pixel_format_map.find(pixel_format);
  843. if (format == pixel_format_map.end()) {
  844. MSI_LOG_ERROR << "unsupported out_pixel_format " << pixel_format;
  845. return FAILED;
  846. }
  847. decode_para_.pixel_format = format->second;
  848. return SUCCESS;
  849. }
  850. Status DvppJsonConfigParser::ParseResizePara(const nlohmann::json &json_item) {
  851. if (GetIntValue(json_item, "out_width", resize_para_.output_width) != SUCCESS) {
  852. return FAILED;
  853. }
  854. if (GetIntValue(json_item, "out_height", resize_para_.output_height) != SUCCESS) {
  855. return FAILED;
  856. }
  857. resize_flag_ = true;
  858. return SUCCESS;
  859. }
  860. Status DvppJsonConfigParser::ParseCropPara(const nlohmann::json &json_item) {
  861. if (GetIntValue(json_item, "out_width", crop_para_.output_width) != SUCCESS) {
  862. return FAILED;
  863. }
  864. if (GetIntValue(json_item, "out_height", crop_para_.output_height) != SUCCESS) {
  865. return FAILED;
  866. }
  867. auto &crop_info = crop_para_.crop_info;
  868. std::string crop_type = "crop_type";
  869. if (GetStringValue(json_item, "crop_type", crop_type) != SUCCESS) {
  870. return FAILED;
  871. }
  872. if (crop_type == "offset") {
  873. MSI_LOG_INFO << "Crop type is 'offset'";
  874. crop_info.crop_type = kDvppCropTypeOffset;
  875. auto &crop_area = crop_info.crop_area;
  876. if (GetIntValue(json_item, "crop_left", crop_area.left) != SUCCESS) {
  877. return FAILED;
  878. }
  879. if (GetIntValue(json_item, "crop_top", crop_area.top) != SUCCESS) {
  880. return FAILED;
  881. }
  882. if (GetIntValue(json_item, "crop_right", crop_area.right) != SUCCESS) {
  883. return FAILED;
  884. }
  885. if (GetIntValue(json_item, "crop_bottom", crop_area.bottom) != SUCCESS) {
  886. return FAILED;
  887. }
  888. } else if (crop_type == "centre") {
  889. MSI_LOG_INFO << "Crop type is 'centre'";
  890. if (GetIntValue(json_item, "crop_width", crop_info.crop_width) != SUCCESS) {
  891. return FAILED;
  892. }
  893. if (GetIntValue(json_item, "crop_height", crop_info.crop_height) != SUCCESS) {
  894. return FAILED;
  895. }
  896. crop_info.crop_type = kDvppCropTypeCentre;
  897. } else {
  898. MSI_LOG_ERROR << "Invalid crop type " << crop_type << ", expect offset or centre";
  899. return FAILED;
  900. }
  901. crop_flag_ = true;
  902. return SUCCESS;
  903. }
  904. Status DvppJsonConfigParser::ParseCropAndPastePara(const nlohmann::json &json_item) {
  905. // crop info
  906. if (GetIntValue(json_item, "out_width", crop_and_paste_para_.output_width) != SUCCESS) {
  907. return FAILED;
  908. }
  909. if (GetIntValue(json_item, "out_height", crop_and_paste_para_.output_height) != SUCCESS) {
  910. return FAILED;
  911. }
  912. auto &crop_info = crop_and_paste_para_.crop_info;
  913. std::string crop_type = "crop_type";
  914. if (GetStringValue(json_item, "crop_type", crop_type) != SUCCESS) {
  915. return FAILED;
  916. }
  917. if (crop_type == "offset") {
  918. MSI_LOG_INFO << "Crop type is 'offset'";
  919. crop_info.crop_type = kDvppCropTypeOffset;
  920. auto &crop_area = crop_info.crop_area;
  921. if (GetIntValue(json_item, "crop_left", crop_area.left) != SUCCESS) {
  922. return FAILED;
  923. }
  924. if (GetIntValue(json_item, "crop_top", crop_area.top) != SUCCESS) {
  925. return FAILED;
  926. }
  927. if (GetIntValue(json_item, "crop_right", crop_area.right) != SUCCESS) {
  928. return FAILED;
  929. }
  930. if (GetIntValue(json_item, "crop_bottom", crop_area.bottom) != SUCCESS) {
  931. return FAILED;
  932. }
  933. } else if (crop_type == "centre") {
  934. MSI_LOG_INFO << "Crop type is 'centre'";
  935. if (GetIntValue(json_item, "crop_width", crop_info.crop_width) != SUCCESS) {
  936. return FAILED;
  937. }
  938. if (GetIntValue(json_item, "crop_height", crop_info.crop_height) != SUCCESS) {
  939. return FAILED;
  940. }
  941. crop_info.crop_type = kDvppCropTypeCentre;
  942. } else {
  943. MSI_LOG_ERROR << "Invalid crop type " << crop_type << ", expect offset or centre";
  944. return FAILED;
  945. }
  946. // paste info
  947. auto &paste_area = crop_and_paste_para_.paste_area;
  948. if (GetIntValue(json_item, "paste_left", paste_area.left) != SUCCESS) {
  949. return FAILED;
  950. }
  951. if (GetIntValue(json_item, "paste_top", paste_area.top) != SUCCESS) {
  952. return FAILED;
  953. }
  954. if (GetIntValue(json_item, "paste_right", paste_area.right) != SUCCESS) {
  955. return FAILED;
  956. }
  957. if (GetIntValue(json_item, "paste_bottom", paste_area.bottom) != SUCCESS) {
  958. return FAILED;
  959. }
  960. crop_and_paste_flag_ = true;
  961. return SUCCESS;
  962. }
  963. Status DvppJsonConfigParser::InitWithJsonConfigImp(const std::string &json_config) {
  964. std::ifstream fp(json_config);
  965. if (!fp.is_open()) {
  966. MSI_LOG_ERROR << "read json config file failed";
  967. return FAILED;
  968. }
  969. const auto &model_info = nlohmann::json::parse(fp);
  970. auto preprocess_list = model_info.find("preprocess");
  971. if (preprocess_list == model_info.end()) {
  972. MSI_LOG_ERROR << "get preprocess failed";
  973. return FAILED;
  974. }
  975. if (!preprocess_list->is_array()) {
  976. MSI_LOG_ERROR << "preprocess is not array";
  977. return FAILED;
  978. }
  979. if (preprocess_list->empty()) {
  980. MSI_LOG_ERROR << "preprocess size is 0";
  981. return FAILED;
  982. }
  983. auto &preprocess = preprocess_list->at(0);
  984. // input
  985. if (ParseInputPara(preprocess) != SUCCESS) {
  986. MSI_LOG_ERROR << "parse input failed";
  987. return FAILED;
  988. }
  989. // decode para
  990. if (ParseDecodePara(preprocess) != SUCCESS) {
  991. MSI_LOG_ERROR << "parse decode failed";
  992. return FAILED;
  993. }
  994. // ops
  995. auto dvpp_process = preprocess.find("dvpp_process");
  996. if (dvpp_process == preprocess.end()) {
  997. MSI_LOG_ERROR << "get dvpp_process failed";
  998. return FAILED;
  999. }
  1000. if (!dvpp_process->is_object()) {
  1001. MSI_LOG_ERROR << "dvpp_process is not array";
  1002. return FAILED;
  1003. }
  1004. const auto &item = *dvpp_process;
  1005. std::string op_name;
  1006. if (GetStringValue(item, "op_name", op_name) != SUCCESS) {
  1007. return FAILED;
  1008. }
  1009. if (op_name == "resize") {
  1010. if (ParseResizePara(item) != SUCCESS) {
  1011. MSI_LOG_ERROR << "Parse resize para failed";
  1012. return FAILED;
  1013. }
  1014. } else if (op_name == "crop") {
  1015. if (ParseCropPara(item) != SUCCESS) {
  1016. MSI_LOG_ERROR << "Parse crop para failed";
  1017. return FAILED;
  1018. }
  1019. } else if (op_name == "crop_and_paste") {
  1020. if (ParseCropAndPastePara(item) != SUCCESS) {
  1021. MSI_LOG_ERROR << "Parse decode para failed";
  1022. return FAILED;
  1023. }
  1024. } else {
  1025. MSI_LOG_ERROR << "Unsupported op name " << op_name << ", expect resize, crop or crop_and_paste";
  1026. return FAILED;
  1027. }
  1028. return SUCCESS;
  1029. }
  1030. Status DvppJsonConfigParser::InitWithJsonConfig(const std::string &json_config) {
  1031. try {
  1032. auto ret = InitWithJsonConfigImp(json_config);
  1033. if (ret != SUCCESS) {
  1034. MSI_LOG_ERROR << "init dvpp with json config failed, json config " << json_config;
  1035. return FAILED;
  1036. }
  1037. } catch (nlohmann::json::exception &e) {
  1038. MSI_LOG_ERROR << "init dvpp with json config failed, json config " << json_config << ", error: " << e.what();
  1039. return FAILED;
  1040. }
  1041. MSI_LOG_INFO << "Init with json config " << json_config << " success";
  1042. return SUCCESS;
  1043. }
  1044. Status DvppProcess::InitWithJsonConfig(const std::string &json_config) {
  1045. DvppJsonConfigParser parser;
  1046. if (parser.InitWithJsonConfig(json_config) != SUCCESS) {
  1047. MSI_LOG_ERROR << "init json config failed";
  1048. return FAILED;
  1049. }
  1050. if (InitJpegDecodePara(parser.GetDecodePara()) != SUCCESS) {
  1051. MSI_LOG_ERROR << "init decode para failed";
  1052. return FAILED;
  1053. }
  1054. if (parser.HasResizeConfig()) {
  1055. if (InitResizePara(parser.GetResizePara()) != SUCCESS) {
  1056. MSI_LOG_ERROR << "init resize para failed";
  1057. return FAILED;
  1058. }
  1059. } else if (parser.HasCropConfig()) {
  1060. if (InitCropPara(parser.GetCropPara()) != SUCCESS) {
  1061. MSI_LOG_ERROR << "init crop para failed";
  1062. return FAILED;
  1063. }
  1064. } else if (parser.HasCropAndPasteConfig()) {
  1065. if (InitCropAndPastePara(parser.GetCropAndPastePara()) != SUCCESS) {
  1066. MSI_LOG_ERROR << "init crop and paste para failed";
  1067. return FAILED;
  1068. }
  1069. }
  1070. return SUCCESS;
  1071. }
  1072. } // namespace inference
  1073. } // namespace mindspore