|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "gxf/std/tensor.hpp" |
| 19 | +#include "holoscan/holoscan.hpp" |
| 20 | +#include "pva_unsharp_mask/pva_unsharp_mask.hpp" |
| 21 | + |
| 22 | +#include <holoscan/operators/holoviz/holoviz.hpp> |
| 23 | +#include <holoscan/operators/video_stream_recorder/video_stream_recorder.hpp> |
| 24 | +#include <holoscan/operators/video_stream_replayer/video_stream_replayer.hpp> |
| 25 | +#include <holoscan/core/system/gpu_resource_monitor.hpp> |
| 26 | + |
| 27 | +#include <iostream> |
| 28 | +#include <string> |
| 29 | + |
| 30 | +namespace holoscan::ops { |
| 31 | +class PreCompiledPVAExecutor : public Operator { |
| 32 | + public: |
| 33 | + HOLOSCAN_OPERATOR_FORWARD_ARGS(PreCompiledPVAExecutor); |
| 34 | + PreCompiledPVAExecutor() = default; |
| 35 | + |
| 36 | + void setup(OperatorSpec& spec) override { |
| 37 | + spec.param(allocator_, "allocator", "Allocator", "Allocator to allocate output tensor."); |
| 38 | + spec.input<gxf::Entity>("input"); |
| 39 | + spec.output<gxf::Entity>("output"); |
| 40 | + } |
| 41 | + void compute(InputContext& op_input, OutputContext& op_output, |
| 42 | + ExecutionContext& context) override { |
| 43 | + auto maybe_input_message = op_input.receive<gxf::Entity>("input"); |
| 44 | + if (!maybe_input_message.has_value()) { |
| 45 | + HOLOSCAN_LOG_ERROR("Failed to receive input message gxf::Entity"); |
| 46 | + return; |
| 47 | + } |
| 48 | + auto input_tensor = maybe_input_message.value().get<holoscan::Tensor>(); |
| 49 | + if (!input_tensor) { |
| 50 | + HOLOSCAN_LOG_ERROR("Failed to receive holoscan::Tensor from input message gxf::Entity"); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // get handle to underlying nvidia::gxf::Allocator from std::shared_ptr<holoscan::Allocator> |
| 55 | + auto allocator = nvidia::gxf::Handle<nvidia::gxf::Allocator>::Create( |
| 56 | + fragment()->executor().context(), allocator_->gxf_cid()); |
| 57 | + |
| 58 | + // cast Holoscan::Tensor to nvidia::gxf::Tensor to use its APIs directly |
| 59 | + nvidia::gxf::Tensor input_tensor_gxf{input_tensor->dl_ctx()}; |
| 60 | + |
| 61 | + auto out_message = CreateTensorMap( |
| 62 | + context.context(), |
| 63 | + allocator.value(), |
| 64 | + {{"output", |
| 65 | + nvidia::gxf::MemoryStorageType::kDevice, |
| 66 | + input_tensor_gxf.shape(), |
| 67 | + nvidia::gxf::PrimitiveType::kUnsigned8, |
| 68 | + 0, |
| 69 | + nvidia::gxf::ComputeTrivialStrides( |
| 70 | + input_tensor_gxf.shape(), |
| 71 | + nvidia::gxf::PrimitiveTypeSize(nvidia::gxf::PrimitiveType::kUnsigned8))}}, |
| 72 | + false); |
| 73 | + |
| 74 | + if (!out_message) { std::runtime_error("failed to create out_message"); } |
| 75 | + const auto output_tensor = out_message.value().get<nvidia::gxf::Tensor>(); |
| 76 | + if (!output_tensor) { std::runtime_error("failed to create out_tensor"); } |
| 77 | + |
| 78 | + uint8_t* input_tensor_data = static_cast<uint8_t*>(input_tensor->data()); |
| 79 | + uint8_t* output_tensor_data = static_cast<uint8_t*>(output_tensor.value()->pointer()); |
| 80 | + if (output_tensor_data == nullptr) { |
| 81 | + throw std::runtime_error("Failed to allocate memory for the output image"); |
| 82 | + } |
| 83 | + |
| 84 | + const int32_t imageWidth{static_cast<int32_t>(input_tensor->shape()[1])}; |
| 85 | + const int32_t imageHeight{static_cast<int32_t>(input_tensor->shape()[0])}; |
| 86 | + const int32_t inputLinePitch{static_cast<int32_t>(input_tensor->shape()[1])}; |
| 87 | + const int32_t outputLinePitch{static_cast<int32_t>(input_tensor->shape()[1])}; |
| 88 | + |
| 89 | + if (!pvaOperatorTask_.isInitialized()) { |
| 90 | + pvaOperatorTask_.init(imageWidth, imageHeight, inputLinePitch, outputLinePitch); |
| 91 | + } |
| 92 | + pvaOperatorTask_.process(input_tensor_data, output_tensor_data); |
| 93 | + auto result = gxf::Entity(std::move(out_message.value())); |
| 94 | + |
| 95 | + op_output.emit(result, "output"); |
| 96 | + } |
| 97 | + |
| 98 | + private: |
| 99 | + Parameter<std::shared_ptr<Allocator>> allocator_; |
| 100 | + PvaUnsharpMask pvaOperatorTask_; |
| 101 | +}; |
| 102 | +} // namespace holoscan::ops |
| 103 | + |
| 104 | +class App : public holoscan::Application { |
| 105 | + public: |
| 106 | + void compose() override { |
| 107 | + using namespace holoscan; |
| 108 | + |
| 109 | + uint32_t max_width{1920}; |
| 110 | + uint32_t max_height{1080}; |
| 111 | + int64_t source_block_size = max_width * max_height * 3; |
| 112 | + |
| 113 | + std::shared_ptr<BlockMemoryPool> pva_allocator = |
| 114 | + make_resource<BlockMemoryPool>("allocator", 1, source_block_size, 1); |
| 115 | + |
| 116 | + auto precompiledpva = make_operator<ops::PreCompiledPVAExecutor>( |
| 117 | + "precompiledpva", Arg("allocator") = pva_allocator); |
| 118 | + |
| 119 | + auto source = make_operator<ops::VideoStreamReplayerOp>("replayer", from_config("replayer")); |
| 120 | + |
| 121 | + auto recorder = make_operator<ops::VideoStreamRecorderOp>("recorder", from_config("recorder")); |
| 122 | + auto visualizer1 = make_operator<ops::HolovizOp>( |
| 123 | + "holoviz1", from_config("holoviz"), Arg("window_title") = std::string("Original Stream")); |
| 124 | + auto visualizer2 = |
| 125 | + make_operator<ops::HolovizOp>("holoviz2", |
| 126 | + from_config("holoviz"), |
| 127 | + Arg("window_title") = std::string("Image Sharpened Stream")); |
| 128 | + |
| 129 | + add_flow(source, precompiledpva); |
| 130 | + add_flow(source, visualizer1, {{"output", "receivers"}}); |
| 131 | + // add_flow(precompiledpva, recorder); |
| 132 | + add_flow(precompiledpva, visualizer2, {{"output", "receivers"}}); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +int main(int argc, char** argv) { |
| 137 | + auto app = holoscan::make_application<App>(); |
| 138 | + |
| 139 | + auto config_path = std::filesystem::canonical(argv[0]).parent_path(); |
| 140 | + config_path += "/main.yaml"; |
| 141 | + app->config(config_path); |
| 142 | + |
| 143 | + app->run(); |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
0 commit comments