dvpp_process.cc 42.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/**
 * Copyright 2020 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "serving/acl/dvpp_process.h"
#include <jpeglib.h>
#include <fstream>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include "include/infer_log.h"

namespace mindspore {
namespace inference {

DvppProcess::DvppProcess() {}

DvppProcess::~DvppProcess() {}

static uint32_t ToEven(uint32_t num) { return (num + 1) / 2 * 2; }

static uint32_t ToOdd(uint32_t num) {
  if (num == 0) {
    return 1;
  }
  return (num + 1) / 2 * 2 - 1;
}

class DvppJsonConfigParser {
 public:
  DvppJsonConfigParser() = default;
  ~DvppJsonConfigParser() = default;

  Status InitWithJsonConfig(const std::string &json_config);
  DvppDecodePara GetDecodePara() const { return decode_para_; }
  DvppResizePara GetResizePara() const { return resize_para_; }
  DvppCropPara GetCropPara() const { return crop_para_; }
  DvppCropAndPastePara GetCropAndPastePara() const { return crop_and_paste_para_; }
  bool HasResizeConfig() const { return resize_flag_; }
  bool HasCropConfig() const { return crop_flag_; }
  bool HasCropAndPasteConfig() const { return crop_and_paste_flag_; }

 private:
  DvppDecodePara decode_para_;
  DvppResizePara resize_para_;
  DvppCropPara crop_para_;
  DvppCropAndPastePara crop_and_paste_para_;
  bool resize_flag_ = false;
  bool crop_flag_ = false;
  bool crop_and_paste_flag_ = false;

  Status GetStringValue(const nlohmann::json &json_item, const std::string &key, std::string &val);
  Status GetIntValue(const nlohmann::json &json_item, const std::string &key, uint32_t &val);
  Status ParseInputPara(const nlohmann::json &preprocess_item);
  Status ParseDecodePara(const nlohmann::json &preprocess_item);
  Status ParseResizePara(const nlohmann::json &json_item);
  Status ParseCropPara(const nlohmann::json &json_item);
  Status ParseCropAndPastePara(const nlohmann::json &json_item);
  Status InitWithJsonConfigImp(const std::string &json_config);
};

Status DvppProcess::InitResource(aclrtStream stream) {
  stream_ = stream;
  aclError acl_ret;
  dvpp_channel_desc_ = acldvppCreateChannelDesc();
  if (dvpp_channel_desc_ == nullptr) {
    MSI_LOG_ERROR << "acldvppCreateChannelDesc failed";
    return FAILED;
  }
  acl_ret = acldvppCreateChannel(dvpp_channel_desc_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "acldvppCreateChannel failed, acl return " << acl_ret;
    return FAILED;
  }
  MSI_LOG_INFO << "End init dvpp process resource";
  return SUCCESS;
}

void DvppProcess::DestroyResource() {
  if (dvpp_channel_desc_ != nullptr) {
    auto acl_ret = acldvppDestroyChannel(dvpp_channel_desc_);
    if (acl_ret != ACL_ERROR_NONE) {
      MSI_LOG_ERROR << "acldvppDestroyChannel failed, acl return " << acl_ret;
    }
    acl_ret = acldvppDestroyChannelDesc(dvpp_channel_desc_);
    if (acl_ret != ACL_ERROR_NONE) {
      MSI_LOG_ERROR << "acldvppDestroyChannelDesc failed, acl return " << acl_ret;
    }
    dvpp_channel_desc_ = nullptr;
  }
}

void DvppProcess::Finalize() {
  DestroyDecodeDesc();
  DestroyVpcOutputDesc();
  DestroyResource();
  if (resize_config_ != nullptr) {
    acldvppDestroyResizeConfig(resize_config_);
    resize_config_ = nullptr;
  }
  if (crop_area_ != nullptr) {
    acldvppDestroyRoiConfig(crop_area_);
    crop_area_ = nullptr;
  }
  if (paste_area_ != nullptr) {
    acldvppDestroyRoiConfig(paste_area_);
    paste_area_ = nullptr;
  }
  if (input_pic_dev_buffer_ != nullptr) {
    acldvppFree(input_pic_dev_buffer_);
  }
  input_pic_buffer_size_ = 0;
  MSI_LOG_INFO << "End dvpp process finalize";
}

Status DvppProcess::InitJpegDecodePara(const DvppDecodePara &decode_para) {
  decode_para_ = decode_para;
  MSI_LOG_INFO << "Init decode para, pixel_format " << decode_para_.pixel_format;
  return SUCCESS;
}

Status DvppProcess::InitResizePara(const DvppResizePara &resize_para) {
  resize_para_ = resize_para;
  MSI_LOG_INFO << "Init resize para, "
               << "output_width " << resize_para_.output_width << ", output_height " << resize_para_.output_height;
  to_resize_flag_ = true;
  to_crop_flag_ = false;
  to_crop_and_paste_flag_ = false;
  Status ret = InitResizeOutputDesc();
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "InitResizeOutputDesc failed";
  }
  return ret;
}

Status DvppProcess::InitCommonCropPara(DvppCropInfo &crop_info, uint32_t output_width, uint32_t output_height) {
  if (crop_info.crop_type == kDvppCropTypeOffset) {
    if (CheckAndAdjustRoiArea(crop_info.crop_area) != SUCCESS) {
      MSI_LOG_ERROR << "Check and adjust crop area failed";
      return FAILED;
    }
    MSI_LOG_INFO << "Init common crop para, crop type offset "
                 << ", left " << crop_info.crop_area.left << ", right " << crop_info.crop_area.right << ", top "
                 << crop_info.crop_area.top << ", bottom " << crop_info.crop_area.bottom << ", output_width "
                 << output_width << ", output_height " << output_height;
  } else {
    crop_info.crop_width = ToEven(crop_info.crop_width);
    crop_info.crop_height = ToEven(crop_info.crop_height);
    if (CheckRoiAreaWidthHeight(crop_info.crop_width, crop_info.crop_height) != SUCCESS) {
      MSI_LOG_ERROR << "Check crop area width and height failed, actually width " << crop_info.crop_width << " height "
                    << crop_info.crop_height;
      return FAILED;
    }
    MSI_LOG_INFO << "Init common crop para, crop type centre "
                 << ", crop_width " << crop_info.crop_width << ", crop_height " << crop_info.crop_height
                 << ", output_width " << output_width << ", output_height " << output_height;
  }
  return SUCCESS;
}

Status DvppProcess::InitCropPara(const DvppCropPara &crop_para) {
  crop_para_ = crop_para;
  if (InitCommonCropPara(crop_para_.crop_info, crop_para_.output_width, crop_para_.output_height) != SUCCESS) {
    MSI_LOG_ERROR << "Init common crop para failed in InitCropPara";
    return FAILED;
  }
  to_crop_flag_ = true;
  to_resize_flag_ = false;
  to_crop_and_paste_flag_ = false;
  Status ret = InitCropOutputDesc();
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "InitCropOutputDesc failed";
  }
  return ret;
}

Status DvppProcess::InitCropAndPastePara(const DvppCropAndPastePara &crop_and_paste_para) {
  crop_and_paste_para_ = crop_and_paste_para;
  if (InitCommonCropPara(crop_and_paste_para_.crop_info, crop_and_paste_para_.output_width,
                         crop_and_paste_para_.output_height) != SUCCESS) {
    MSI_LOG_ERROR << "Init common crop para failed in InitCropAndPastePara";
    return FAILED;
  }
  auto &paste_area = crop_and_paste_para_.paste_area;
  if (CheckAndAdjustRoiArea(paste_area) != SUCCESS) {
    MSI_LOG_ERROR << "Check and adjust paste area failed";
    return FAILED;
  }
  MSI_LOG_INFO << "Init crop and paste para, paste info: "
               << ", left " << paste_area.left << ", right " << paste_area.right << ", top " << paste_area.top
               << ", bottom " << paste_area.bottom;

  to_crop_and_paste_flag_ = true;
  to_crop_flag_ = false;
  to_resize_flag_ = false;
  Status ret = InitCropAndPasteOutputDesc();
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "InitCropAndPasteOutputDesc failed";
  }
  return ret;
}

Status DvppProcess::InputInputBuffer(const void *pic_buffer, size_t pic_buffer_size) {
  aclError acl_ret;
  if (pic_buffer_size != input_pic_buffer_size_) {
    acldvppFree(input_pic_dev_buffer_);
    input_pic_buffer_size_ = 0;
    acl_ret = acldvppMalloc(&input_pic_dev_buffer_, pic_buffer_size);
    if (acl_ret != ACL_ERROR_NONE) {
      MSI_LOG_ERROR << "acldvppMalloc input picture buffer on device failed, buffer size " << pic_buffer_size;
      return FAILED;
    }
    input_pic_buffer_size_ = pic_buffer_size;
  }
  acl_ret =
    aclrtMemcpy(input_pic_dev_buffer_, input_pic_buffer_size_, pic_buffer, pic_buffer_size, ACL_MEMCPY_HOST_TO_DEVICE);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "aclrtMemcpy input picture buffer to device, buffer size " << pic_buffer_size;
    return FAILED;
  }
  return SUCCESS;
}

static void JpegErrorExitCustom(j_common_ptr cinfo) {
X
xuyongfei 已提交
236
  char jpeg_last_error_msg[JMSG_LENGTH_MAX] = {0};
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  if (cinfo != nullptr && cinfo->err != nullptr && cinfo->err->format_message != nullptr) {
    (*(cinfo->err->format_message))(cinfo, jpeg_last_error_msg);
  }
  throw std::runtime_error(jpeg_last_error_msg);
}

Status DvppProcess::GetJpegWidthHeight(const void *pic_buffer, size_t pic_buffer_size, uint32_t &image_width,
                                       uint32_t &image_height) {
  struct jpeg_decompress_struct jpeg_header;
  struct jpeg_error_mgr jpeg_error;
  jpeg_header.err = jpeg_std_error(&jpeg_error);
  jpeg_error.error_exit = JpegErrorExitCustom;
  try {
    jpeg_create_decompress(&jpeg_header);
    jpeg_mem_src(&jpeg_header, reinterpret_cast<const unsigned char *>(pic_buffer), pic_buffer_size);
    (void)jpeg_read_header(&jpeg_header, TRUE);
  } catch (std::runtime_error &e) {
    jpeg_destroy_decompress(&jpeg_header);
    MSI_LOG_ERROR << "jpeg images read failed, " << e.what();
    return INFER_STATUS(INVALID_INPUTS) << "jpeg images decode failed";
  }
  image_width = jpeg_header.image_width;
  image_height = jpeg_header.image_height;

  if (jpeg_header.jpeg_color_space != JCS_YCbCr) {
    MSI_LOG_ERROR << "Expect color space YUV(YCbCr), current " << jpeg_header.jpeg_color_space;
    jpeg_destroy_decompress(&jpeg_header);
    return INFER_STATUS(INVALID_INPUTS) << "Expect color space YUV(YCbCr), current " << jpeg_header.jpeg_color_space;
  }
  if (jpeg_header.dc_huff_tbl_ptrs[0] == nullptr) {
    MSI_LOG_ERROR << "Only support Huffman code";
    jpeg_destroy_decompress(&jpeg_header);
    return INFER_STATUS(INVALID_INPUTS) << "Only support Huffman code";
  }
  jpeg_destroy_decompress(&jpeg_header);

  const uint32_t min_width = 32;
  const uint32_t max_width = 8192;
  const uint32_t min_height = 32;
  const uint32_t max_height = 8192;
  if (image_width < min_width || image_width > max_width) {
    MSI_LOG_ERROR << "expect image width [" << min_width << ", " << max_width << "], the real image width is "
                  << image_width;
    return INFER_STATUS(INVALID_INPUTS) << "expect image width [" << min_width << ", " << max_width
                                        << "], the real image width is " << image_width;
  }
  if (image_height < min_height || image_height > max_height) {
    MSI_LOG_ERROR << "expect image height [" << min_height << ", " << max_height << "], the real image height is "
                  << image_height;
    return INFER_STATUS(INVALID_INPUTS) << "expect image height [" << min_height << ", " << max_height
                                        << "], the real image height is " << image_height;
  }
  return SUCCESS;
}

Status DvppProcess::Process(const void *pic_buffer, size_t pic_buffer_size, void *&output_device_buffer,
                            size_t &output_size) {
  if (dvpp_channel_desc_ == nullptr) {
    MSI_LOG_ERROR << "Process failed, dvpp not inited";
    return FAILED;
  }
  uint32_t image_width = 0;
  uint32_t image_height = 0;
  Status ret = GetJpegWidthHeight(pic_buffer, pic_buffer_size, image_width, image_height);
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "Get jpeg image height and width failed";
    return ret;
  }
  MSI_LOG_INFO << "Get jpeg width " << image_width << ", height " << image_height;
  ret = InitDecodeOutputDesc(image_width, image_height);
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "InitDecodeOutputDesc failed";
    return FAILED;
  }
  ret = UpdateCropArea(image_width, image_height);
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "Update crop area failed";
    return ret;
  }
  ret = CheckResizeImageInfo(image_width, image_height);
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "Check resize para failed";
    return ret;
  }
  if (InputInputBuffer(pic_buffer, pic_buffer_size) != SUCCESS) {
    MSI_LOG_ERROR << "InputInputBuffer failed";
    return FAILED;
  }
  if (ProcessDecode() != SUCCESS) {
    MSI_LOG_ERROR << "Process Decode failed";
    return INFER_STATUS(INVALID_INPUTS) << "Decode image failed";
  }
  MSI_LOG_INFO << "Process Decode success";
  if (to_resize_flag_) {
    if (ProcessResize() != SUCCESS) {
      MSI_LOG_ERROR << "Process Resize failed";
      return INFER_STATUS(FAILED) << "Resize image failed";
    }
    MSI_LOG_INFO << "Process Resize success";
  } else if (to_crop_flag_) {
    if (ProcessCrop() != SUCCESS) {
      MSI_LOG_ERROR << "Process Crop failed";
      return INFER_STATUS(FAILED) << "Crop image failed";
    }
    MSI_LOG_INFO << "Process Crop success";
  } else if (to_crop_and_paste_flag_) {
    if (ProcessCropAndPaste() != SUCCESS) {
      MSI_LOG_ERROR << "Process Crop And Paste failed";
      return INFER_STATUS(FAILED) << "Crop And Paste image failed";
    }
    MSI_LOG_INFO << "Process Crop And Paste success";
  }
  if (vpc_output_buffer_dev_ == nullptr) {
    output_device_buffer = decode_output_buffer_dev_;
    output_size = decode_output_buffer_size_;
  } else {
    output_device_buffer = vpc_output_buffer_dev_;
    output_size = vpc_output_buffer_size_;
  }
  MSI_LOG_INFO << "Process dvpp success";
  return SUCCESS;
}

Status DvppProcess::Process(const std::vector<const void *> &pic_buffer_list,
                            const std::vector<size_t> &pic_buffer_size_list, void *&output_device_buffer,
                            size_t &output_size) {
  auto batch_size = pic_buffer_list.size();
  if (batch_size == 0 || batch_size != pic_buffer_size_list.size()) {
    MSI_LOG_ERROR << "invalid batch size " << batch_size << ", pic size count" << pic_buffer_size_list.size();
    return FAILED;
  }
  MSI_LOG_INFO << "Begin dvpp process, batch size " << batch_size;
  if (batch_size == 1) {
    return Process(pic_buffer_list[0], pic_buffer_size_list[0], output_device_buffer, output_size);
  }
  size_t total_buffer_size = vpc_output_buffer_size_ * batch_size;
  if (batch_size_ != batch_size) {
    if (batch_vpc_output_buffer_dev_ != nullptr) {
      acldvppFree(batch_vpc_output_buffer_dev_);
      batch_vpc_output_buffer_dev_ = nullptr;
    }
    batch_size_ = batch_size;
    auto acl_rt = acldvppMalloc(&batch_vpc_output_buffer_dev_, total_buffer_size);
    if (acl_rt != ACL_ERROR_NONE) {
      MSI_LOG_ERROR << "acldvppMalloc failed, buffer size " << total_buffer_size;
      return FAILED;
    }
  }
  for (size_t i = 0; i < batch_size; i++) {
    const void *pic_buffer = pic_buffer_list[i];
    uint32_t pic_size = pic_buffer_size_list[i];
    if (pic_buffer == nullptr || pic_size == 0) {
      MSI_LOG_ERROR << "Get " << 0 << "th images failed";
      return FAILED;
    }
    void *output_dev_buffer_tmp = nullptr;
    size_t output_buffer_size_tmp = 0;
    Status ret = Process(pic_buffer, pic_size, output_dev_buffer_tmp, output_buffer_size_tmp);
    if (ret != SUCCESS) {
      MSI_LOG_ERROR << "dvpp process failed";
      return ret;
    }
    aclrtMemcpy(static_cast<uint8_t *>(batch_vpc_output_buffer_dev_) + vpc_output_buffer_size_ * i,
                total_buffer_size - vpc_output_buffer_size_ * i, output_dev_buffer_tmp, vpc_output_buffer_size_,
                ACL_MEMCPY_DEVICE_TO_DEVICE);

    MSI_LOG_INFO << "Dvpp process " << i << " th images success, input pic size " << pic_size << " output buffer size "
                 << output_buffer_size_tmp;
  }
  output_device_buffer = batch_vpc_output_buffer_dev_;
  output_size = total_buffer_size;
  MSI_LOG_INFO << "End dvpp process, batch size " << batch_size << ", output size " << output_size;
  return SUCCESS;
}

uint32_t DvppProcess::AlignmentHelper(uint32_t org_size, uint32_t alignment) const {
  if (alignment == 0) {
    return 0;
  }
  return (org_size + alignment - 1) / alignment * alignment;
}

uint32_t DvppProcess::GetImageBufferSize(uint32_t stride_width, uint32_t stride_height,
                                         acldvppPixelFormat pixel_format) const {
  if (stride_height == 0 || stride_width == 0) {
    MSI_LOG_ERROR << "invalid stride height or width, stride_width " << stride_width << " stride_height "
                  << stride_height;
    return 0;
  }
  if (UINT32_MAX / 3 < stride_height || UINT32_MAX / (3 * stride_height) < stride_width) {
    MSI_LOG_ERROR << "invalid stride height or width, stride_width " << stride_width << " stride_height "
                  << stride_height;
    return 0;
  }
  if (pixel_format == PIXEL_FORMAT_YUV_SEMIPLANAR_420 || pixel_format == PIXEL_FORMAT_YVU_SEMIPLANAR_420) {
    return stride_width * stride_height * 3 / 2;  // 420
  } else if (pixel_format == PIXEL_FORMAT_YUV_SEMIPLANAR_422 || pixel_format == PIXEL_FORMAT_YVU_SEMIPLANAR_422) {
    return stride_width * stride_height * 2;  // 422
  } else if (pixel_format == PIXEL_FORMAT_YUV_SEMIPLANAR_444 || pixel_format == PIXEL_FORMAT_YVU_SEMIPLANAR_444) {
    return stride_width * stride_height * 3;  // 444
  }
  MSI_LOG_ERROR << "Not support pixel format " << pixel_format;
  return 0;
}

Status DvppProcess::GetPicDescStride(uint32_t width, uint32_t height, uint32_t &stride_width, uint32_t &stride_height) {
  const uint32_t width_alignment = 16;
  const uint32_t height_alignment = 2;
  const uint32_t stride_width_minimum = 32;
  const uint32_t stride_width_maximum = 4096;
  const uint32_t stride_height_minimum = 6;
  const uint32_t stride_height_maximum = 4096;

  stride_width = AlignmentHelper(width, width_alignment);
  stride_height = AlignmentHelper(height, height_alignment);
  if (stride_width == 0 || stride_height == 0) {
    MSI_LOG_ERROR << "Init VPC output desc failed, get stride width or height failed";
    return FAILED;
  }
  if (stride_width < stride_width_minimum || stride_width > stride_width_maximum) {
    MSI_LOG_ERROR << "Expect stride width [" << stride_width_minimum << ", " << stride_width_maximum
                  << "], current stride width " << stride_width << " given width " << width;
    return FAILED;
  }
  if (stride_height < stride_height_minimum || stride_height > stride_height_maximum) {
    MSI_LOG_ERROR << "Expect stride height [" << stride_height_minimum << ", " << stride_height_maximum
                  << "], current stride height " << stride_height << " given height " << height;
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::GetPicDescStrideDecode(uint32_t width, uint32_t height, uint32_t &stride_width,
                                           uint32_t &stride_height) {
  const uint32_t width_alignment = 128;
  const uint32_t height_alignment = 16;
  const uint32_t width_minimum = 32;
  const uint32_t width_maximum = 4096;  // decode support 8192, dvpp(resize/crop/crop&paste) support 4096
  const uint32_t height_minimum = 32;
  const uint32_t height_maximum = 4096;  // decode support 8192, dvpp(resize/crop/crop&paste) support 4096
  if (width < width_minimum || width > width_maximum) {
    MSI_LOG_ERROR << "Expect width [" << width_minimum << ", " << width_maximum << "], current width " << width;
    return INFER_STATUS(INVALID_INPUTS) << "Expect width [" << width_minimum << ", " << width_maximum
                                        << "], current width " << width;
  }
  if (height < height_minimum || height > height_maximum) {
    MSI_LOG_ERROR << "Expect height [" << height_minimum << ", " << height_maximum << "], current height " << height;
    return INFER_STATUS(INVALID_INPUTS) << "Expect height [" << height_minimum << ", " << height_maximum
                                        << "], current height " << height;
  }
  stride_width = AlignmentHelper(width, width_alignment);
  stride_height = AlignmentHelper(height, height_alignment);
  if (stride_width == 0 || stride_height == 0) {
    MSI_LOG_ERROR << "Init decode output desc failed, get stride width or height failed";
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::InitVpcOutputDesc(uint32_t output_width, uint32_t output_height, acldvppPixelFormat pixel_format) {
  DestroyVpcOutputDesc();
  uint32_t vpc_stride_width = 0;
  uint32_t vpc_stride_height = 0;
  if (GetPicDescStride(output_width, output_height, vpc_stride_width, vpc_stride_height) != SUCCESS) {
    MSI_LOG_ERROR << "Init VPC output desc failed, get VPC output stride width/height failed";
    return FAILED;
  }
  vpc_output_buffer_size_ = GetImageBufferSize(vpc_stride_width, vpc_stride_height, pixel_format);
  if (vpc_output_buffer_size_ == 0) {
    MSI_LOG_ERROR << "Init VPC output desc failed, get image buffer size failed";
    return FAILED;
  }
  auto acl_ret = acldvppMalloc(&vpc_output_buffer_dev_, vpc_output_buffer_size_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "Init VPC output desc failed, malloc dvpp memory failed";
    return FAILED;
  }
  vpc_output_desc_ = acldvppCreatePicDesc();
  if (vpc_output_desc_ == nullptr) {
    MSI_LOG_ERROR << "Init VPC output desc failed, create pic desc failed";
    return FAILED;
  }
  acldvppSetPicDescData(vpc_output_desc_, vpc_output_buffer_dev_);
  acldvppSetPicDescSize(vpc_output_desc_, vpc_output_buffer_size_);
  acldvppSetPicDescFormat(vpc_output_desc_, pixel_format);
  acldvppSetPicDescWidth(vpc_output_desc_, output_width);
  acldvppSetPicDescHeight(vpc_output_desc_, output_height);
  acldvppSetPicDescWidthStride(vpc_output_desc_, vpc_stride_width);
  acldvppSetPicDescHeightStride(vpc_output_desc_, vpc_stride_height);
  MSI_LOG_INFO << "Init VPC output desc success";
  return SUCCESS;
}

void DvppProcess::DestroyVpcOutputDesc() {
  if (vpc_output_desc_ != nullptr) {
    acldvppDestroyPicDesc(vpc_output_desc_);
    vpc_output_desc_ = nullptr;
  }
  if (vpc_output_buffer_dev_ != nullptr) {
    acldvppFree(vpc_output_buffer_dev_);
    vpc_output_buffer_dev_ = nullptr;
  }
  if (batch_vpc_output_buffer_dev_ != nullptr) {
    acldvppFree(batch_vpc_output_buffer_dev_);
    batch_vpc_output_buffer_dev_ = nullptr;
  }
  vpc_output_buffer_size_ = 0;
  MSI_LOG_INFO << "End destroy vpc desc";
}

Status DvppProcess::InitDecodeOutputDesc(uint32_t image_width, uint32_t image_height) {
  if (decode_output_buffer_dev_ != nullptr && image_width == pic_width_ && image_height == pic_height_) {
    return SUCCESS;
  }
  DestroyDecodeDesc();

  pic_width_ = image_width;
  pic_height_ = image_height;

  uint32_t stride_width = 0;
  uint32_t stride_height = 0;
  Status ret = GetPicDescStrideDecode(pic_width_, pic_height_, stride_width, stride_height);
  if (ret != SUCCESS) {
    MSI_LOG_ERROR << "Init VPC output desc failed, get VPC output stride width/height failed";
    return ret;
  }

  decode_output_buffer_size_ = GetImageBufferSize(stride_width, stride_height, decode_para_.pixel_format);
  if (decode_output_buffer_size_ == 0) {
    MSI_LOG_ERROR << "Init decode output desc failed, get image buffer size failed";
    return FAILED;
  }
  auto acl_ret = acldvppMalloc(&decode_output_buffer_dev_, decode_output_buffer_size_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "Init decode output desc failed, malloc dvpp memory failed";
    return FAILED;
  }
  decode_output_desc_ = acldvppCreatePicDesc();
  if (decode_output_desc_ == nullptr) {
    MSI_LOG_ERROR << "Init decode output desc failed, create pic desc failed";
    return FAILED;
  }
  acldvppSetPicDescData(decode_output_desc_, decode_output_buffer_dev_);
  acldvppSetPicDescSize(decode_output_desc_, decode_output_buffer_size_);
  acldvppSetPicDescFormat(decode_output_desc_, decode_para_.pixel_format);
  acldvppSetPicDescWidth(decode_output_desc_, pic_width_);
  acldvppSetPicDescHeight(decode_output_desc_, pic_height_);
  acldvppSetPicDescWidthStride(decode_output_desc_, stride_width);
  acldvppSetPicDescHeightStride(decode_output_desc_, stride_height);
  MSI_LOG_INFO << "Init decode output desc success";
  return SUCCESS;
}

Status DvppProcess::CheckRoiAreaWidthHeight(uint32_t width, uint32_t height) {
  const uint32_t min_crop_width = 10;
  const uint32_t max_crop_width = 4096;
  const uint32_t min_crop_height = 6;
  const uint32_t max_crop_height = 4096;

  if (width < min_crop_width || width > max_crop_width) {
    MSI_LOG_ERROR << "Expect roi area width in [" << min_crop_width << ", " << max_crop_width << "], actually "
                  << width;
    return FAILED;
  }
  if (height < min_crop_height || height > max_crop_height) {
    MSI_LOG_ERROR << "Expect roi area height in [" << min_crop_height << ", " << max_crop_height << "], actually "
                  << height;
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::CheckAndAdjustRoiArea(DvppRoiArea &area) {
  if (area.right < area.left) {
    MSI_LOG_ERROR << "check roi area failed, left " << area.left << ", right " << area.right;
    return FAILED;
  }
  if (area.bottom < area.top) {
    MSI_LOG_ERROR << "check roi area failed, top " << area.top << ", bottom " << area.bottom;
    return FAILED;
  }

  area.left = ToEven(area.left);
  area.top = ToEven(area.top);
  area.right = ToOdd(area.right);
  area.bottom = ToOdd(area.bottom);

  auto width = area.right - area.left + 1;
  auto height = area.bottom - area.top + 1;
  if (CheckRoiAreaWidthHeight(width, height) != SUCCESS) {
    MSI_LOG_ERROR << "Check roi area width and height failed,"
                  << " actually width " << width << " left " << area.left << ", right " << area.right
                  << " actually height " << height << " top " << area.top << ", bottom " << area.bottom;
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::UpdateCropArea(uint32_t image_width, uint32_t image_height) {
  DvppCropInfo *crop_info = nullptr;
  if (to_crop_flag_) {
    crop_info = &crop_para_.crop_info;
  } else if (to_crop_and_paste_flag_) {
    crop_info = &crop_and_paste_para_.crop_info;
  } else {
    return SUCCESS;
  }
  if (crop_info->crop_type != kDvppCropTypeCentre) {
    return SUCCESS;
  }
  if (image_width < crop_info->crop_width) {
    MSI_LOG_ERROR << "Image width " << image_width << "smaller than crop width " << crop_info->crop_width;
    return INFER_STATUS(INVALID_INPUTS) << "Image width " << image_width << "smaller than crop width "
                                        << crop_info->crop_width;
  }
  if (image_height < crop_info->crop_height) {
    MSI_LOG_ERROR << "Image height " << image_height << "smaller than crop height " << crop_info->crop_height;
    return INFER_STATUS(INVALID_INPUTS) << "Image width " << image_width << "smaller than crop width "
                                        << crop_info->crop_width;
  }
  uint32_t left = ToEven((image_width - crop_info->crop_width) / 2);
  uint32_t top = ToEven((image_height - crop_info->crop_height) / 2);
  uint32_t right = ToOdd(left + crop_info->crop_width);
  uint32_t bottom = ToOdd(top + crop_info->crop_height);

  auto acl_ret = acldvppSetRoiConfig(crop_area_, left, right, top, bottom);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "Update Crop Area failed";
    return FAILED;
  }
  MSI_LOG_INFO << "Update crop area, crop type centre, crop info: "
               << ", left " << left << ", right " << right << ", top " << top << ", bottom " << bottom;
  return SUCCESS;
}

Status DvppProcess::CheckResizeImageInfo(uint32_t image_width, uint32_t image_height) const {
  if (!to_resize_flag_) {
    return SUCCESS;
  }
  // resize ratio required [1/32, 16]
  auto check_resize_ratio = [](uint32_t before_resize, uint32_t after_resize) {
    if (before_resize == 0 || after_resize == 0) {
      return false;
    }
    if (before_resize / after_resize > 32) {
      return false;
    }
    if (after_resize / before_resize > 16) {
      return false;
    }
    return true;
  };
  if (!check_resize_ratio(image_width, resize_para_.output_width)) {
    MSI_LOG_ERROR << "Resize ratio required [1/32, 16], current width resize from " << image_width << " to "
                  << resize_para_.output_width;
    return INFER_STATUS(INVALID_INPUTS) << "Resize ratio required [1/32, 16], current width resize from " << image_width
                                        << " to " << resize_para_.output_width;
  }
  if (!check_resize_ratio(image_height, resize_para_.output_height)) {
    MSI_LOG_ERROR << "Resize ratio required [1/32, 16], current height resize from " << image_height << " to "
                  << resize_para_.output_height;
    return INFER_STATUS(INVALID_INPUTS) << "Resize ratio required [1/32, 16], current height resize from "
                                        << image_height << " to " << resize_para_.output_height;
  }
  return SUCCESS;
}

void DvppProcess::DestroyDecodeDesc() {
  if (decode_output_desc_ != nullptr) {
    acldvppDestroyPicDesc(decode_output_desc_);
    decode_output_desc_ = nullptr;
  }
  if (decode_output_buffer_dev_ != nullptr) {
    acldvppFree(decode_output_buffer_dev_);
    decode_output_buffer_dev_ = nullptr;
  }
  decode_output_buffer_size_ = 0;
  MSI_LOG_INFO << "End destroy decode desc";
}

Status DvppProcess::InitResizeOutputDesc() {
  if (InitVpcOutputDesc(resize_para_.output_width, resize_para_.output_height, decode_para_.pixel_format) != SUCCESS) {
    MSI_LOG_ERROR << "Init VPC output desc failed";
    return FAILED;
  }
  if (resize_config_ == nullptr) {
    resize_config_ = acldvppCreateResizeConfig();
    if (resize_config_ == nullptr) {
      MSI_LOG_ERROR << "Create Resize config failed";
      return FAILED;
    }
  }
  return SUCCESS;
}

Status DvppProcess::InitRoiAreaConfig(acldvppRoiConfig *&roi_area, const DvppRoiArea &init_para) {
  if (roi_area == nullptr) {
    roi_area = acldvppCreateRoiConfig(init_para.left, init_para.right, init_para.top, init_para.bottom);
    if (roi_area == nullptr) {
      MSI_LOG_ERROR << "Create Roi config failed";
      return FAILED;
    }
  } else {
    auto acl_ret = acldvppSetRoiConfig(roi_area, init_para.left, init_para.right, init_para.top, init_para.bottom);
    if (acl_ret != ACL_ERROR_NONE) {
      MSI_LOG_ERROR << "Set Roi config failed";
      return FAILED;
    }
  }
  return SUCCESS;
}

Status DvppProcess::InitCropOutputDesc() {
  if (InitVpcOutputDesc(crop_para_.output_width, crop_para_.output_height, decode_para_.pixel_format) != SUCCESS) {
    MSI_LOG_ERROR << "Init VPC output desc failed";
    return FAILED;
  }
  if (InitRoiAreaConfig(crop_area_, crop_para_.crop_info.crop_area) != SUCCESS) {
    MSI_LOG_ERROR << "Init crop area failed";
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::InitCropAndPasteOutputDesc() {
  if (InitVpcOutputDesc(crop_and_paste_para_.output_width, crop_and_paste_para_.output_height,
                        decode_para_.pixel_format) != SUCCESS) {
    MSI_LOG_ERROR << "Init VPC output desc failed";
    return FAILED;
  }
  if (InitRoiAreaConfig(crop_area_, crop_and_paste_para_.crop_info.crop_area) != SUCCESS) {
    MSI_LOG_ERROR << "Init crop area failed";
    return FAILED;
  }
  if (InitRoiAreaConfig(paste_area_, crop_and_paste_para_.paste_area) != SUCCESS) {
    MSI_LOG_ERROR << "Init paste area failed";
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::ProcessDecode() {
  aclError acl_ret;
  acl_ret = acldvppJpegDecodeAsync(dvpp_channel_desc_, input_pic_dev_buffer_, input_pic_buffer_size_,
                                   decode_output_desc_, stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "acldvppJpegDecodeAsync failed, acl return " << acl_ret;
    return FAILED;
  }
  acl_ret = aclrtSynchronizeStream(stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::ProcessResize() {
  aclError acl_ret;
  acl_ret = acldvppVpcResizeAsync(dvpp_channel_desc_, decode_output_desc_, vpc_output_desc_, resize_config_, stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "acldvppVpcResizeAsync failed, acl return " << acl_ret;
    return FAILED;
  }
  acl_ret = aclrtSynchronizeStream(stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::ProcessCrop() {
  aclError acl_ret;
  acl_ret = acldvppVpcCropAsync(dvpp_channel_desc_, decode_output_desc_, vpc_output_desc_, crop_area_, stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "acldvppVpcCropAsync failed, acl return " << acl_ret;
    return FAILED;
  }
  acl_ret = aclrtSynchronizeStream(stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
    return FAILED;
  }
  return SUCCESS;
}

Status DvppProcess::ProcessCropAndPaste() {
  aclError acl_ret;
  acl_ret = acldvppVpcCropAndPasteAsync(dvpp_channel_desc_, decode_output_desc_, vpc_output_desc_, crop_area_,
                                        paste_area_, stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "acldvppVpcCropAndPasteAsync failed, acl return " << acl_ret;
    return FAILED;
  }
  acl_ret = aclrtSynchronizeStream(stream_);
  if (acl_ret != ACL_ERROR_NONE) {
    MSI_LOG_ERROR << "aclrtSynchronizeStream failed, acl return " << acl_ret;
    return FAILED;
  }
  return SUCCESS;
}

Status DvppJsonConfigParser::GetStringValue(const nlohmann::json &json_item, const std::string &key, std::string &val) {
  auto it = json_item.find(key);
  if (it == json_item.end()) {
    MSI_LOG_ERROR << "get string item " << key << " failed";
    return FAILED;
  }
  if (!it->is_string()) {
    MSI_LOG_ERROR << "item " << key << " value is not string type";
    return FAILED;
  }
  val = it->get<std::string>();
  return SUCCESS;
}

Status DvppJsonConfigParser::GetIntValue(const nlohmann::json &json_item, const std::string &key, uint32_t &val) {
  auto it = json_item.find(key);
  if (it == json_item.end()) {
    MSI_LOG_ERROR << "get string item " << key << " failed";
    return FAILED;
  }
  if (!it->is_number_integer()) {
    MSI_LOG_ERROR << "item " << key << " value is not integer type";
    return FAILED;
  }
  val = it->get<uint32_t>();
  return SUCCESS;
}

Status DvppJsonConfigParser::ParseInputPara(const nlohmann::json &preprocess_item) {
  auto input = preprocess_item.find("input");
  if (input == preprocess_item.end()) {
    MSI_LOG_ERROR << "get input failed";
    return FAILED;
  }
  if (!input->is_object()) {
    MSI_LOG_ERROR << "input is not object";
    return FAILED;
  }
  return SUCCESS;
}

Status DvppJsonConfigParser::ParseDecodePara(const nlohmann::json &preprocess_item) {
  auto decode_para = preprocess_item.find("decode_para");
  if (decode_para == preprocess_item.end()) {
    MSI_LOG_ERROR << "get input failed";
    return FAILED;
  }
  if (!decode_para->is_object()) {
    MSI_LOG_ERROR << "input is not object";
    return FAILED;
  }
  const std::unordered_map<std::string, acldvppPixelFormat> pixel_format_map = {
    {"YUV420SP", PIXEL_FORMAT_YUV_SEMIPLANAR_420}, {"YVU420SP", PIXEL_FORMAT_YVU_SEMIPLANAR_420},
    {"YUV422SP", PIXEL_FORMAT_YUV_SEMIPLANAR_422}, {"YVU422SP", PIXEL_FORMAT_YVU_SEMIPLANAR_422},
    {"YUV444SP", PIXEL_FORMAT_YUV_SEMIPLANAR_444}, {"YVU444SP", PIXEL_FORMAT_YVU_SEMIPLANAR_444},
  };
  std::string pixel_format;
  if (GetStringValue(*decode_para, "out_pixel_format", pixel_format) != SUCCESS) {
    MSI_LOG_ERROR << "get op out_pixel_format failed";
    return FAILED;
  }
  auto format = pixel_format_map.find(pixel_format);
  if (format == pixel_format_map.end()) {
    MSI_LOG_ERROR << "unsupported out_pixel_format " << pixel_format;
    return FAILED;
  }
  decode_para_.pixel_format = format->second;
  return SUCCESS;
}

Status DvppJsonConfigParser::ParseResizePara(const nlohmann::json &json_item) {
  if (GetIntValue(json_item, "out_width", resize_para_.output_width) != SUCCESS) {
    return FAILED;
  }
  if (GetIntValue(json_item, "out_height", resize_para_.output_height) != SUCCESS) {
    return FAILED;
  }
  resize_flag_ = true;
  return SUCCESS;
}

Status DvppJsonConfigParser::ParseCropPara(const nlohmann::json &json_item) {
  if (GetIntValue(json_item, "out_width", crop_para_.output_width) != SUCCESS) {
    return FAILED;
  }
  if (GetIntValue(json_item, "out_height", crop_para_.output_height) != SUCCESS) {
    return FAILED;
  }
  auto &crop_info = crop_para_.crop_info;
  std::string crop_type = "crop_type";
  if (GetStringValue(json_item, "crop_type", crop_type) != SUCCESS) {
    return FAILED;
  }
  if (crop_type == "offset") {
    MSI_LOG_INFO << "Crop type is 'offset'";
    crop_info.crop_type = kDvppCropTypeOffset;
    auto &crop_area = crop_info.crop_area;
    if (GetIntValue(json_item, "crop_left", crop_area.left) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_top", crop_area.top) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_right", crop_area.right) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_bottom", crop_area.bottom) != SUCCESS) {
      return FAILED;
    }
  } else if (crop_type == "centre") {
    MSI_LOG_INFO << "Crop type is 'centre'";
    if (GetIntValue(json_item, "crop_width", crop_info.crop_width) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_height", crop_info.crop_height) != SUCCESS) {
      return FAILED;
    }
    crop_info.crop_type = kDvppCropTypeCentre;
  } else {
    MSI_LOG_ERROR << "Invalid crop type " << crop_type << ", expect offset or centre";
    return FAILED;
  }
  crop_flag_ = true;
  return SUCCESS;
}

Status DvppJsonConfigParser::ParseCropAndPastePara(const nlohmann::json &json_item) {
  // crop info
  if (GetIntValue(json_item, "out_width", crop_and_paste_para_.output_width) != SUCCESS) {
    return FAILED;
  }
  if (GetIntValue(json_item, "out_height", crop_and_paste_para_.output_height) != SUCCESS) {
    return FAILED;
  }
  auto &crop_info = crop_and_paste_para_.crop_info;
  std::string crop_type = "crop_type";
  if (GetStringValue(json_item, "crop_type", crop_type) != SUCCESS) {
    return FAILED;
  }
  if (crop_type == "offset") {
    MSI_LOG_INFO << "Crop type is 'offset'";
    crop_info.crop_type = kDvppCropTypeOffset;
    auto &crop_area = crop_info.crop_area;
    if (GetIntValue(json_item, "crop_left", crop_area.left) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_top", crop_area.top) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_right", crop_area.right) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_bottom", crop_area.bottom) != SUCCESS) {
      return FAILED;
    }
  } else if (crop_type == "centre") {
    MSI_LOG_INFO << "Crop type is 'centre'";
    if (GetIntValue(json_item, "crop_width", crop_info.crop_width) != SUCCESS) {
      return FAILED;
    }
    if (GetIntValue(json_item, "crop_height", crop_info.crop_height) != SUCCESS) {
      return FAILED;
    }
    crop_info.crop_type = kDvppCropTypeCentre;
  } else {
    MSI_LOG_ERROR << "Invalid crop type " << crop_type << ", expect offset or centre";
    return FAILED;
  }
  // paste info
  auto &paste_area = crop_and_paste_para_.paste_area;
  if (GetIntValue(json_item, "paste_left", paste_area.left) != SUCCESS) {
    return FAILED;
  }
  if (GetIntValue(json_item, "paste_top", paste_area.top) != SUCCESS) {
    return FAILED;
  }
  if (GetIntValue(json_item, "paste_right", paste_area.right) != SUCCESS) {
    return FAILED;
  }
  if (GetIntValue(json_item, "paste_bottom", paste_area.bottom) != SUCCESS) {
    return FAILED;
  }
  crop_and_paste_flag_ = true;
  return SUCCESS;
}

Status DvppJsonConfigParser::InitWithJsonConfigImp(const std::string &json_config) {
  std::ifstream fp(json_config);
  if (!fp.is_open()) {
    MSI_LOG_ERROR << "read json config file failed";
    return FAILED;
  }
  const auto &model_info = nlohmann::json::parse(fp);
  auto preprocess_list = model_info.find("preprocess");
  if (preprocess_list == model_info.end()) {
    MSI_LOG_ERROR << "get preprocess failed";
    return FAILED;
  }
  if (!preprocess_list->is_array()) {
    MSI_LOG_ERROR << "preprocess is not array";
    return FAILED;
  }
  if (preprocess_list->empty()) {
    MSI_LOG_ERROR << "preprocess size is 0";
    return FAILED;
  }
  auto &preprocess = preprocess_list->at(0);
  // input
  if (ParseInputPara(preprocess) != SUCCESS) {
    MSI_LOG_ERROR << "parse input failed";
    return FAILED;
  }
  // decode para
  if (ParseDecodePara(preprocess) != SUCCESS) {
    MSI_LOG_ERROR << "parse decode failed";
    return FAILED;
  }
  // ops
  auto dvpp_process = preprocess.find("dvpp_process");
  if (dvpp_process == preprocess.end()) {
    MSI_LOG_ERROR << "get dvpp_process failed";
    return FAILED;
  }
  if (!dvpp_process->is_object()) {
    MSI_LOG_ERROR << "dvpp_process is not array";
    return FAILED;
  }
  const auto &item = *dvpp_process;
  std::string op_name;
  if (GetStringValue(item, "op_name", op_name) != SUCCESS) {
    return FAILED;
  }
  if (op_name == "resize") {
    if (ParseResizePara(item) != SUCCESS) {
      MSI_LOG_ERROR << "Parse resize para failed";
      return FAILED;
    }
  } else if (op_name == "crop") {
    if (ParseCropPara(item) != SUCCESS) {
      MSI_LOG_ERROR << "Parse crop para failed";
      return FAILED;
    }
  } else if (op_name == "crop_and_paste") {
    if (ParseCropAndPastePara(item) != SUCCESS) {
      MSI_LOG_ERROR << "Parse decode para failed";
      return FAILED;
    }
  } else {
C
chenzomi 已提交
1088
    MSI_LOG_ERROR << "Unsupported op name " << op_name << ", expect resize, crop or crop_and_paste";
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
    return FAILED;
  }
  return SUCCESS;
}

Status DvppJsonConfigParser::InitWithJsonConfig(const std::string &json_config) {
  try {
    auto ret = InitWithJsonConfigImp(json_config);
    if (ret != SUCCESS) {
      MSI_LOG_ERROR << "init dvpp with json config failed, json config " << json_config;
      return FAILED;
    }
  } catch (nlohmann::json::exception &e) {
    MSI_LOG_ERROR << "init dvpp with json config failed, json config " << json_config << ", error: " << e.what();
    return FAILED;
  }
  MSI_LOG_INFO << "Init with json config " << json_config << " success";
  return SUCCESS;
}

Status DvppProcess::InitWithJsonConfig(const std::string &json_config) {
  DvppJsonConfigParser parser;
  if (parser.InitWithJsonConfig(json_config) != SUCCESS) {
    MSI_LOG_ERROR << "init json config failed";
    return FAILED;
  }
  if (InitJpegDecodePara(parser.GetDecodePara()) != SUCCESS) {
    MSI_LOG_ERROR << "init decode para failed";
    return FAILED;
  }
  if (parser.HasResizeConfig()) {
    if (InitResizePara(parser.GetResizePara()) != SUCCESS) {
      MSI_LOG_ERROR << "init resize para failed";
      return FAILED;
    }
  } else if (parser.HasCropConfig()) {
    if (InitCropPara(parser.GetCropPara()) != SUCCESS) {
      MSI_LOG_ERROR << "init crop para failed";
      return FAILED;
    }
  } else if (parser.HasCropAndPasteConfig()) {
    if (InitCropAndPastePara(parser.GetCropAndPastePara()) != SUCCESS) {
      MSI_LOG_ERROR << "init crop and paste para failed";
      return FAILED;
    }
  }
  return SUCCESS;
}

}  // namespace inference
}  // namespace mindspore