|
@@ -0,0 +1,291 @@
|
|
|
|
|
+#include "hdr_synthesis.h"
|
|
|
|
|
+#include "hdr_synthesis_priv.h"
|
|
|
|
|
+#include "../cuda_helper.hpp"
|
|
|
|
|
+
|
|
|
|
|
+#include <nppi_arithmetic_and_logical_operations.h>
|
|
|
|
|
+#include <nppi_color_conversion.h>
|
|
|
|
|
+#include <nppi_data_exchange_and_initialization.h>
|
|
|
|
|
+#include <nppi_filtering_functions.h>
|
|
|
|
|
+
|
|
|
|
|
+#include <cassert>
|
|
|
|
|
+#include <numeric>
|
|
|
|
|
+
|
|
|
|
|
+template<typename T>
|
|
|
|
|
+struct smart_buffer {
|
|
|
|
|
+ T *ptr = nullptr;
|
|
|
|
|
+ size_t pitch = 0;
|
|
|
|
|
+
|
|
|
|
|
+ size_t width, height, elem_cnt;
|
|
|
|
|
+
|
|
|
|
|
+ smart_buffer(size_t _width, size_t _height, size_t _elem_cnt)
|
|
|
|
|
+ : width(_width), height(_height), elem_cnt(_elem_cnt) {
|
|
|
|
|
+ auto width_bytes = width * elem_cnt * sizeof(T);
|
|
|
|
|
+ CALL_ASSERT(cudaMallocPitch(&ptr, &pitch, width_bytes, height) == cudaSuccess);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ~smart_buffer() {
|
|
|
|
|
+ CALL_ASSERT(cudaFree(ptr) == cudaSuccess);
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+struct hdr_synthesizer::impl {
|
|
|
|
|
+
|
|
|
|
|
+ static constexpr auto u8_to_f32_coff = 1.0f / 255;
|
|
|
|
|
+ static constexpr float u8_to_f32_coff_arr[] = {u8_to_f32_coff,
|
|
|
|
|
+ u8_to_f32_coff,
|
|
|
|
|
+ u8_to_f32_coff};
|
|
|
|
|
+ static constexpr float gaussian_filter_coff[] = {1 / 16.0f,
|
|
|
|
|
+ 4 / 16.0f,
|
|
|
|
|
+ 6 / 16.0f,
|
|
|
|
|
+ 4 / 16.0f,
|
|
|
|
|
+ 1 / 16.0f};
|
|
|
|
|
+ static constexpr auto gaussian_filter_len = sizeof(gaussian_filter_coff) / sizeof(float);
|
|
|
|
|
+
|
|
|
|
|
+ struct image_buffer {
|
|
|
|
|
+ smart_buffer<Npp32f> *image_pyr, *weight_pyr;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ uint16_t width, height, pyr_height;
|
|
|
|
|
+ uint8_t pyr_level;
|
|
|
|
|
+
|
|
|
|
|
+ NppiSize full_size;
|
|
|
|
|
+ NppiRect full_rect;
|
|
|
|
|
+ NppiPoint origin_point;
|
|
|
|
|
+ size_t *pyr_offset_arr;
|
|
|
|
|
+ NppiSize *pyr_size_arr;
|
|
|
|
|
+ void *gaussian_filter_coff_f32;
|
|
|
|
|
+
|
|
|
|
|
+ NppStreamContext npp_ctx, extra_npp_ctx;
|
|
|
|
|
+ cudaStream_t main_stream, extra_stream;
|
|
|
|
|
+ cudaEvent_t sync_event;
|
|
|
|
|
+
|
|
|
|
|
+ // global temporary memory
|
|
|
|
|
+ smart_buffer<Npp8u> *rgb_u8;
|
|
|
|
|
+ smart_buffer<Npp32f> *rgb_f32[2];
|
|
|
|
|
+
|
|
|
|
|
+ impl(uint16_t _width, uint16_t _height, uint8_t _level, cudaStream_t stream)
|
|
|
|
|
+ : width(_width), height(_height), pyr_level(_level), main_stream(stream) {
|
|
|
|
|
+ rgb_u8 = new smart_buffer<Npp8u>(width, height, 3);
|
|
|
|
|
+ rgb_f32[0] = new smart_buffer<Npp32f>(width, height, 3);
|
|
|
|
|
+ rgb_f32[1] = new smart_buffer<Npp32f>(width, height, 3);
|
|
|
|
|
+ CALL_ASSERT(malloc_dev_mem());
|
|
|
|
|
+
|
|
|
|
|
+ init_npp_ctx(&npp_ctx, main_stream);
|
|
|
|
|
+ init_npp_ctx(&extra_npp_ctx, extra_stream);
|
|
|
|
|
+
|
|
|
|
|
+ pyr_height = height + (height >> 1);
|
|
|
|
|
+ full_size = NppiSize{width, height};
|
|
|
|
|
+ full_rect = NppiRect{0, 0, width, height};
|
|
|
|
|
+ origin_point = NppiPoint{0, 0};
|
|
|
|
|
+
|
|
|
|
|
+ pyr_offset_arr = new size_t[pyr_level];
|
|
|
|
|
+ pyr_size_arr = new NppiSize[pyr_level];
|
|
|
|
|
+ auto cur_width = width, cur_height = height;
|
|
|
|
|
+ for (auto i = 0; i < pyr_level; ++i) {
|
|
|
|
|
+// assert(cur_width % 2 == 0);
|
|
|
|
|
+// assert(cur_height % 2 == 0);
|
|
|
|
|
+ pyr_offset_arr[i] = (i == 0) ? 0 : (pyr_offset_arr[i - 1] + cur_width);
|
|
|
|
|
+ cur_width >>= 1;
|
|
|
|
|
+ cur_height >>= 1;
|
|
|
|
|
+ pyr_size_arr[i] = NppiSize{cur_width, cur_height};
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ~impl() {
|
|
|
|
|
+ delete rgb_u8;
|
|
|
|
|
+ delete rgb_f32[0];
|
|
|
|
|
+ delete rgb_f32[1];
|
|
|
|
|
+ CALL_ASSERT(free_dev_mem());
|
|
|
|
|
+
|
|
|
|
|
+ delete pyr_offset_arr;
|
|
|
|
|
+ delete pyr_size_arr;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool malloc_dev_mem() {
|
|
|
|
|
+ // upload gaussian kernel coefficient
|
|
|
|
|
+ CUDA_API_CHECK(cudaMalloc(&gaussian_filter_coff_f32, sizeof(gaussian_filter_coff)));
|
|
|
|
|
+ CUDA_API_CHECK(cudaMemcpy(gaussian_filter_coff_f32, gaussian_filter_coff,
|
|
|
|
|
+ sizeof(gaussian_filter_coff), cudaMemcpyHostToDevice));
|
|
|
|
|
+
|
|
|
|
|
+ CUDA_API_CHECK(cudaStreamCreate(&extra_stream));
|
|
|
|
|
+ CUDA_API_CHECK(cudaEventCreateWithFlags(&sync_event, cudaEventDisableTiming));
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool free_dev_mem() {
|
|
|
|
|
+ CUDA_API_CHECK(cudaFree(gaussian_filter_coff_f32));
|
|
|
|
|
+ CUDA_API_CHECK(cudaStreamDestroy(extra_stream));
|
|
|
|
|
+ CUDA_API_CHECK(cudaEventDestroy(sync_event));
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static bool init_npp_ctx(NppStreamContext *ctx, cudaStream_t stream) {
|
|
|
|
|
+ ctx->hStream = stream;
|
|
|
|
|
+ CUDA_API_CHECK(cudaGetDevice(&ctx->nCudaDeviceId));
|
|
|
|
|
+ cudaDeviceProp dev_prop = {};
|
|
|
|
|
+ CUDA_API_CHECK(cudaGetDeviceProperties(&dev_prop, ctx->nCudaDeviceId));
|
|
|
|
|
+ ctx->nMultiProcessorCount = dev_prop.multiProcessorCount;
|
|
|
|
|
+ ctx->nMaxThreadsPerMultiProcessor = dev_prop.maxThreadsPerMultiProcessor;
|
|
|
|
|
+ ctx->nMaxThreadsPerBlock = dev_prop.maxThreadsPerBlock;
|
|
|
|
|
+ ctx->nSharedMemPerBlock = dev_prop.sharedMemPerBlock;
|
|
|
|
|
+ CUDA_API_CHECK(cudaDeviceGetAttribute(&ctx->nCudaDevAttrComputeCapabilityMajor,
|
|
|
|
|
+ cudaDevAttrComputeCapabilityMajor, ctx->nCudaDeviceId));
|
|
|
|
|
+ CUDA_API_CHECK(cudaDeviceGetAttribute(&ctx->nCudaDevAttrComputeCapabilityMinor,
|
|
|
|
|
+ cudaDevAttrComputeCapabilityMinor, ctx->nCudaDeviceId));
|
|
|
|
|
+ CUDA_API_CHECK(cudaStreamGetFlags(stream, &ctx->nStreamFlags));
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool malloc_buffer(image_buffer *buf) const {
|
|
|
|
|
+ buf->image_pyr = new smart_buffer<Npp32f>(width, pyr_height, 3);
|
|
|
|
|
+ buf->weight_pyr = new smart_buffer<Npp32f>(width, pyr_height, 1);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool gaussian_pyramid(Npp32f *ptr, size_t pitch, bool is_rgb,
|
|
|
|
|
+ NppStreamContext *ctx) const { // construct gaussian pyramid
|
|
|
|
|
+ auto pyr_ptr = smart_offset(ptr, pitch, 0, height, is_rgb ? 3 : 1);
|
|
|
|
|
+ CUDA_API_CHECK((is_rgb ?
|
|
|
|
|
+ nppiFilterGaussPyramidLayerDownBorder_32f_C3R_Ctx :
|
|
|
|
|
+ nppiFilterGaussPyramidLayerDownBorder_32f_C1R_Ctx)(
|
|
|
|
|
+ ptr, pitch, full_size, origin_point,
|
|
|
|
|
+ pyr_ptr, pitch, pyr_size_arr[0],
|
|
|
|
|
+ 2, gaussian_filter_len, (Npp32f *) gaussian_filter_coff_f32, NPP_BORDER_MIRROR, *ctx));
|
|
|
|
|
+ for (int i = 0; i < pyr_level - 1; ++i) {
|
|
|
|
|
+ auto src_ptr = smart_offset(ptr, pitch, pyr_offset_arr[i], height, is_rgb ? 3 : 1);
|
|
|
|
|
+ auto dst_ptr = smart_offset(ptr, pitch, pyr_offset_arr[i + 1], height, is_rgb ? 3 : 1);
|
|
|
|
|
+ CUDA_API_CHECK((is_rgb ?
|
|
|
|
|
+ nppiFilterGaussPyramidLayerDownBorder_32f_C3R_Ctx :
|
|
|
|
|
+ nppiFilterGaussPyramidLayerDownBorder_32f_C1R_Ctx)(
|
|
|
|
|
+ src_ptr, pitch, pyr_size_arr[i], origin_point,
|
|
|
|
|
+ dst_ptr, pitch, pyr_size_arr[i + 1],
|
|
|
|
|
+ 2, gaussian_filter_len, (Npp32f *) gaussian_filter_coff_f32, NPP_BORDER_MIRROR, *ctx));
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool laplacian_operation(Npp32f *src_ptr, Npp32f *dst_ptr, size_t pitch,
|
|
|
|
|
+ NppiSize src_size, NppiSize dst_size, cudaStream_t stream) const {
|
|
|
|
|
+ call_laplacian_operation(src_ptr, dst_ptr, pitch,
|
|
|
|
|
+ (Npp32f *) gaussian_filter_coff_f32, src_size, dst_size, false, stream);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool laplacian_pyramid(Npp32f *ptr, size_t pitch,
|
|
|
|
|
+ NppStreamContext *ctx, cudaStream_t stream) const { // construct laplacian pyramid
|
|
|
|
|
+ // generate gaussian pyramid first
|
|
|
|
|
+ CALL_CHECK(gaussian_pyramid(ptr, pitch, true, ctx));
|
|
|
|
|
+
|
|
|
|
|
+ // generate laplacian pyramid by up-sampling and subtraction
|
|
|
|
|
+ auto pyr_ptr = smart_offset(ptr, pitch, 0, height, 3);
|
|
|
|
|
+ CALL_CHECK(laplacian_operation(pyr_ptr, ptr, pitch, pyr_size_arr[0], full_size, stream));
|
|
|
|
|
+ for (int i = 0; i < pyr_level - 1; ++i) {
|
|
|
|
|
+ auto src_ptr = smart_offset(ptr, pitch, pyr_offset_arr[i + 1], height, 3);
|
|
|
|
|
+ auto dst_ptr = smart_offset(ptr, pitch, pyr_offset_arr[i], height, 3);
|
|
|
|
|
+ CALL_CHECK(laplacian_operation(src_ptr, dst_ptr, pitch, pyr_size_arr[i + 1], pyr_size_arr[i], stream));
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool reconstruct_operation(Npp32f *src_ptr, Npp32f *dst_ptr, size_t pitch,
|
|
|
|
|
+ NppiSize src_size, NppiSize dst_size, cudaStream_t stream) const {
|
|
|
|
|
+ call_laplacian_operation(src_ptr, dst_ptr, pitch,
|
|
|
|
|
+ (Npp32f *) gaussian_filter_coff_f32, src_size, dst_size, true, stream);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // reconstruct from laplacian pyramid, for rgb image only
|
|
|
|
|
+ bool pyramid_reconstruct(Npp32f *ptr, size_t pitch, cudaStream_t stream) const {
|
|
|
|
|
+ for (int i = pyr_level - 1; i > 0; --i) {
|
|
|
|
|
+ auto src_ptr = smart_offset(ptr, pitch, pyr_offset_arr[i], height, 3);
|
|
|
|
|
+ auto dst_ptr = smart_offset(ptr, pitch, pyr_offset_arr[i - 1], height, 3);
|
|
|
|
|
+ CALL_CHECK(reconstruct_operation(src_ptr, dst_ptr, pitch, pyr_size_arr[i], pyr_size_arr[i - 1], stream));
|
|
|
|
|
+ }
|
|
|
|
|
+ auto pyr_ptr = smart_offset(ptr, pitch, 0, height, 3);
|
|
|
|
|
+ CALL_CHECK(reconstruct_operation(pyr_ptr, ptr, pitch, pyr_size_arr[0], full_size, stream));
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool preprocess_image(image_buffer *buf, void *raw_u8, size_t pitch) {
|
|
|
|
|
+ // debayer image
|
|
|
|
|
+ CUDA_API_CHECK(nppiCFAToRGB_8u_C1C3R_Ctx((Npp8u *) raw_u8, pitch, full_size, full_rect,
|
|
|
|
|
+ rgb_u8->ptr, rgb_u8->pitch,
|
|
|
|
|
+ NPPI_BAYER_BGGR, NPPI_INTER_UNDEFINED, npp_ctx));
|
|
|
|
|
+
|
|
|
|
|
+ // convert to float
|
|
|
|
|
+ CUDA_API_CHECK(nppiConvert_8u32f_C3R_Ctx(rgb_u8->ptr, rgb_u8->pitch,
|
|
|
|
|
+ buf->image_pyr->ptr, buf->image_pyr->pitch,
|
|
|
|
|
+ full_size, npp_ctx));
|
|
|
|
|
+ CUDA_API_CHECK(nppiMulC_32f_C3IR_Ctx(u8_to_f32_coff_arr,
|
|
|
|
|
+ buf->image_pyr->ptr, buf->image_pyr->pitch,
|
|
|
|
|
+ full_size, npp_ctx));
|
|
|
|
|
+ CUDA_API_CHECK(cudaEventRecord(sync_event, main_stream));
|
|
|
|
|
+
|
|
|
|
|
+ // calc weight and construct pyramid
|
|
|
|
|
+ CUDA_API_CHECK(cudaStreamWaitEvent(extra_stream, sync_event));
|
|
|
|
|
+ call_hdr_weight(rgb_u8->ptr, rgb_u8->pitch,
|
|
|
|
|
+ buf->weight_pyr->ptr, buf->weight_pyr->pitch,
|
|
|
|
|
+ width, height, extra_stream); // parallel execution for weight related calculation
|
|
|
|
|
+ CALL_CHECK(gaussian_pyramid(buf->weight_pyr->ptr, buf->weight_pyr->pitch, false, &extra_npp_ctx));
|
|
|
|
|
+ CUDA_API_CHECK(cudaEventRecord(sync_event, extra_stream));
|
|
|
|
|
+
|
|
|
|
|
+ // construct image pyramid
|
|
|
|
|
+ CALL_CHECK(laplacian_pyramid(buf->image_pyr->ptr, buf->image_pyr->pitch, &npp_ctx, main_stream));
|
|
|
|
|
+ CUDA_API_CHECK(cudaStreamWaitEvent(main_stream, sync_event));
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool merge_image(image_buffer *buf_a, image_buffer *buf_b,
|
|
|
|
|
+ uint8_t *out_ptr, size_t out_pitch) {
|
|
|
|
|
+ assert(buf_a->image_pyr->pitch == buf_b->image_pyr->pitch);
|
|
|
|
|
+ assert(buf_a->weight_pyr->pitch == buf_b->weight_pyr->pitch);
|
|
|
|
|
+
|
|
|
|
|
+ // merge
|
|
|
|
|
+ call_hdr_merge(buf_a->image_pyr->ptr, buf_b->image_pyr->ptr, buf_a->image_pyr->pitch,
|
|
|
|
|
+ buf_a->weight_pyr->ptr, buf_b->weight_pyr->ptr, buf_a->weight_pyr->pitch,
|
|
|
|
|
+ width, pyr_height, main_stream);
|
|
|
|
|
+
|
|
|
|
|
+ // reconstruct image from laplacian pyramid
|
|
|
|
|
+ CALL_CHECK(pyramid_reconstruct(buf_a->image_pyr->ptr, buf_a->image_pyr->pitch, main_stream));
|
|
|
|
|
+
|
|
|
|
|
+ // convert to uint8
|
|
|
|
|
+ CUDA_API_CHECK(nppiConvert_32f8u_C3R_Ctx(buf_a->image_pyr->ptr, buf_a->image_pyr->pitch,
|
|
|
|
|
+ out_ptr, out_pitch, full_size, NPP_RND_NEAR, npp_ctx));
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+hdr_synthesizer::hdr_synthesizer(uint16_t width, uint16_t height,
|
|
|
|
|
+ cudaStream_t stream, uint8_t pyramid_level)
|
|
|
|
|
+ : pimpl(std::make_unique<impl>(width, height, pyramid_level, stream)) {
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+hdr_synthesizer::~hdr_synthesizer() = default;
|
|
|
|
|
+
|
|
|
|
|
+bool hdr_synthesizer::malloc_buffer(void **out_buf) {
|
|
|
|
|
+ *out_buf = new impl::image_buffer{};
|
|
|
|
|
+ return pimpl->malloc_buffer((impl::image_buffer *) *out_buf);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool hdr_synthesizer::free_buffer(void *out_buf) {
|
|
|
|
|
+ if (out_buf == nullptr) return true;
|
|
|
|
|
+ auto ptr = (impl::image_buffer *) out_buf;
|
|
|
|
|
+ delete ptr->image_pyr;
|
|
|
|
|
+ delete ptr->weight_pyr;
|
|
|
|
|
+ delete ptr;
|
|
|
|
|
+ return true;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool hdr_synthesizer::preprocess_image(void *img_buf, void *img_ptr, size_t pitch) {
|
|
|
|
|
+ return pimpl->preprocess_image((impl::image_buffer *) img_buf, img_ptr, pitch);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool hdr_synthesizer::merge_image(void *buf_a, void *buf_b,
|
|
|
|
|
+ uint8_t *img_ptr, size_t img_pitch) {
|
|
|
|
|
+ return pimpl->merge_image((impl::image_buffer *) buf_a, (impl::image_buffer *) buf_b,
|
|
|
|
|
+ img_ptr, img_pitch);
|
|
|
|
|
+}
|