|
| 1 | +/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 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 "tensorflow/lite/c/common.h" |
| 17 | +#include "tensorflow/lite/kernels/internal/compatibility.h" |
| 18 | +#include "tensorflow/lite/kernels/kernel_util.h" |
| 19 | +#include "tensorflow/lite/micro/kernels/decode_state.h" |
| 20 | +#include "tensorflow/lite/micro/kernels/kernel_util.h" |
| 21 | +#include "tensorflow/lite/micro/micro_context.h" |
| 22 | +#include "tensorflow/lite/micro/micro_log.h" |
| 23 | + |
| 24 | +namespace tflite { |
| 25 | +namespace { |
| 26 | + |
| 27 | +TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { |
| 28 | + const size_t num_inputs = NumInputs(node); |
| 29 | + const size_t num_outputs = NumOutputs(node); |
| 30 | + TF_LITE_ENSURE(context, num_outputs > 0); |
| 31 | + TF_LITE_ENSURE_EQ(context, num_inputs, num_outputs * 2); |
| 32 | + |
| 33 | + MicroContext* const micro_context = GetMicroContext(context); |
| 34 | + |
| 35 | + node->user_data = micro_context->AllocatePersistentBuffer( |
| 36 | + num_outputs * sizeof(DecodeState*)); |
| 37 | + TF_LITE_ENSURE(context, node->user_data != nullptr); |
| 38 | + DecodeState** const dsp_arr = |
| 39 | + reinterpret_cast<DecodeState**>(node->user_data); |
| 40 | + |
| 41 | + TfLiteTensor* input = nullptr; |
| 42 | + TfLiteTensor* ancillary = nullptr; |
| 43 | + TfLiteTensor* output = nullptr; |
| 44 | + TfLiteStatus status = kTfLiteOk; |
| 45 | + |
| 46 | + for (size_t i = 0; i < num_inputs; i += 2) { |
| 47 | + input = micro_context->AllocateTempInputTensor(node, i); |
| 48 | + if (input == nullptr) { |
| 49 | + MicroPrintf("failed to allocate input tensor %u", i); |
| 50 | + status = kTfLiteError; |
| 51 | + break; |
| 52 | + } |
| 53 | + ancillary = micro_context->AllocateTempInputTensor(node, i + 1); |
| 54 | + if (ancillary == nullptr) { |
| 55 | + MicroPrintf("failed to allocate ancillary tensor %u", i + 1); |
| 56 | + status = kTfLiteError; |
| 57 | + break; |
| 58 | + } |
| 59 | + output = micro_context->AllocateTempOutputTensor(node, i / 2); |
| 60 | + if (output == nullptr) { |
| 61 | + MicroPrintf("failed to allocate output tensor %u", i / 2); |
| 62 | + status = kTfLiteError; |
| 63 | + break; |
| 64 | + } |
| 65 | + |
| 66 | + if (DecodeState::Version(*ancillary) != 1) { |
| 67 | + MicroPrintf("version %u != 1", DecodeState::Version(*ancillary)); |
| 68 | + status = kTfLiteError; |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + DecodeState* dsp = nullptr; |
| 73 | + switch (DecodeState::Type(*ancillary)) { |
| 74 | + case DecodeState::kDcmTypeLUT: |
| 75 | + dsp = DecodeState::CreateDecodeStateLUT( |
| 76 | + context, micro_context->GetAlternateProfiler()); |
| 77 | + break; |
| 78 | + case DecodeState::kDcmTypeCustom: |
| 79 | + MicroPrintf("Custom decode type not yet supported"); |
| 80 | + break; |
| 81 | + default: |
| 82 | + MicroPrintf("unsupported decode type %u", |
| 83 | + DecodeState::Type(*ancillary)); |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + if (dsp != nullptr) { |
| 88 | + status = dsp->Setup(*input, *ancillary, *output); |
| 89 | + if (status != kTfLiteOk) { |
| 90 | + break; |
| 91 | + } |
| 92 | + dsp_arr[i / 2] = dsp; |
| 93 | + } else { |
| 94 | + MicroPrintf("failed to allocate DecodeState[%u]", i / 2); |
| 95 | + status = kTfLiteError; |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + micro_context->DeallocateTempTfLiteTensor(input); |
| 100 | + micro_context->DeallocateTempTfLiteTensor(ancillary); |
| 101 | + micro_context->DeallocateTempTfLiteTensor(output); |
| 102 | + input = nullptr; |
| 103 | + ancillary = nullptr; |
| 104 | + output = nullptr; |
| 105 | + } |
| 106 | + |
| 107 | + if (input != nullptr) { |
| 108 | + micro_context->DeallocateTempTfLiteTensor(input); |
| 109 | + } |
| 110 | + if (ancillary != nullptr) { |
| 111 | + micro_context->DeallocateTempTfLiteTensor(ancillary); |
| 112 | + } |
| 113 | + if (output != nullptr) { |
| 114 | + micro_context->DeallocateTempTfLiteTensor(output); |
| 115 | + } |
| 116 | + |
| 117 | + return status; |
| 118 | +} |
| 119 | + |
| 120 | +TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
| 121 | + const size_t num_inputs = NumInputs(node); |
| 122 | + DecodeState** const dsp_arr = |
| 123 | + reinterpret_cast<DecodeState**>(node->user_data); |
| 124 | + |
| 125 | + for (size_t i = 0; i < num_inputs; i += 2) { |
| 126 | + const TfLiteEvalTensor* input = |
| 127 | + tflite::micro::GetEvalInput(context, node, i); |
| 128 | + TF_LITE_ENSURE(context, input != nullptr); |
| 129 | + const TfLiteEvalTensor* ancillary = |
| 130 | + tflite::micro::GetEvalInput(context, node, i + 1); |
| 131 | + TF_LITE_ENSURE(context, ancillary != nullptr); |
| 132 | + const TfLiteEvalTensor* output = |
| 133 | + tflite::micro::GetEvalOutput(context, node, i / 2); |
| 134 | + TF_LITE_ENSURE(context, output != nullptr); |
| 135 | + |
| 136 | + TfLiteStatus status = dsp_arr[i / 2]->Decode(*input, *ancillary, *output); |
| 137 | + TF_LITE_ENSURE(context, status == kTfLiteOk); |
| 138 | + } |
| 139 | + |
| 140 | + return kTfLiteOk; |
| 141 | +} |
| 142 | + |
| 143 | +} // namespace |
| 144 | + |
| 145 | +TFLMRegistration Register_DECODE() { |
| 146 | + return tflite::micro::RegisterOp(nullptr, Prepare, Eval); |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace tflite |
0 commit comments