1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "plugin_helper.h" 17 18 using namespace OHOS::AI; 19 20 #define ARRAY_DATA_DECODE_IMPL(type) \ 21 template<> \ 22 int32_t DataDecoder::DecodeOneParameter(Array<type> &val) \ 23 { \ 24 if (val.size != 0 || val.data != nullptr) { \ 25 HILOGE("[PluginHelper]Fail to decode with non-empty data"); \ 26 return RETCODE_FAILURE; \ 27 } \ 28 if (RecursiveDecode(val.size) != RETCODE_SUCCESS) { \ 29 HILOGE("[PluginHelper]Fail to decode with illegal arraySize"); \ 30 return RETCODE_FAILURE; \ 31 } \ 32 AIE_NEW(val.data, type[val.size]); \ 33 if (val.data == nullptr) { \ 34 HILOGE("[PluginHelper]Fail to allocate buffer for decoder"); \ 35 return RETCODE_FAILURE; \ 36 } \ 37 for (size_t i = 0; i < val.size; ++i) { \ 38 if (DecodeOneParameter(val.data[i]) != RETCODE_SUCCESS) { \ 39 HILOGE("[PluginHelper]Fail to decode arrayData at index %zu", i); \ 40 AIE_DELETE_ARRAY(val.data); \ 41 return RETCODE_FAILURE; \ 42 } \ 43 } \ 44 return RETCODE_SUCCESS; \ 45 } 46 47 #define ARRAY_DATA_ENCODE_IMPL(type) \ 48 template<> \ 49 int32_t DataEncoder::EncodeOneParameter(const Array<type> &val) \ 50 { \ 51 if (val.size == 0 || val.data == nullptr) { \ 52 HILOGE("[PluginHelper]Fail to encode with empty data"); \ 53 return RETCODE_FAILURE; \ 54 } \ 55 if (RecursiveEncode(val.size) != RETCODE_SUCCESS) { \ 56 HILOGE("[PluginHelper]Fail to encode with illegal arraySize"); \ 57 return RETCODE_FAILURE; \ 58 } \ 59 for (size_t i = 0; i < val.size; ++i) { \ 60 if (EncodeOneParameter(val.data[i]) != RETCODE_SUCCESS) { \ 61 HILOGE("[PluginHelper]Fail to encode arrayData at index %zu", i); \ 62 return RETCODE_FAILURE; \ 63 } \ 64 } \ 65 return RETCODE_SUCCESS; \ 66 } 67 68 namespace { 69 using Item = std::pair<int32_t, int32_t>; 70 using Items = std::vector<Item>; 71 const uint8_t PAIR_SIZE = 2; 72 } 73 74 template<> EncodeOneParameter(const Items & outputData)75int32_t DataEncoder::EncodeOneParameter(const Items &outputData) 76 { 77 if (RecursiveEncode(outputData.size() * PAIR_SIZE) != RETCODE_SUCCESS) { 78 HILOGE("[PluginHelper]Fail to encode with illegal arraySize"); 79 return RETCODE_FAILURE; 80 } 81 for (size_t i = 0; i < outputData.size(); ++i) { 82 if (EncodeOneParameter(outputData[i].first) != RETCODE_SUCCESS) { 83 HILOGE("[PluginHelper]Fail to encode labels from outputData"); 84 return RETCODE_FAILURE; 85 } 86 } 87 for (size_t i = 0; i < outputData.size(); ++i) { 88 if (EncodeOneParameter(outputData[i].second) != RETCODE_SUCCESS) { 89 HILOGE("[PluginHelper]Fail to encode scores from outputData"); 90 return RETCODE_FAILURE; 91 } 92 } 93 return RETCODE_SUCCESS; 94 } 95 96 ARRAY_DATA_ENCODE_IMPL(uint8_t); 97 98 ARRAY_DATA_ENCODE_IMPL(uint16_t); 99 100 ARRAY_DATA_ENCODE_IMPL(uint32_t); 101 102 ARRAY_DATA_ENCODE_IMPL(int16_t); 103 104 ARRAY_DATA_ENCODE_IMPL(int32_t); 105 106 ARRAY_DATA_DECODE_IMPL(uint8_t); 107 108 ARRAY_DATA_DECODE_IMPL(uint16_t); 109 110 ARRAY_DATA_DECODE_IMPL(uint32_t); 111 112 ARRAY_DATA_DECODE_IMPL(int16_t); 113 114 ARRAY_DATA_DECODE_IMPL(int32_t);