-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathresize.cpp
More file actions
119 lines (106 loc) · 3.26 KB
/
Copy pathresize.cpp
File metadata and controls
119 lines (106 loc) · 3.26 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "libspdl/cuda/npp/detail/resize.h"
#include "libspdl/core/detail/logging.h"
#include "libspdl/core/detail/tracing.h"
#include "libspdl/cuda/detail/utils.h"
#include "libspdl/cuda/npp/detail/utils.h"
#include "libspdl/cuda/nvjpeg/detail/utils.h"
#include <fmt/core.h>
#include <fmt/format.h>
#include <nppi.h>
#define RESIZE_FUNC \
NppStatus (*resize_func)( \
const Npp8u*, \
int, \
NppiSize, \
NppiRect, \
Npp8u*, \
int, \
NppiSize, \
NppiRect, \
int, \
NppStreamContext)
namespace spdl::cuda::detail {
namespace {
template <RESIZE_FUNC>
void resize(
const nvjpegImage_t& src,
const NppiSize& src_size,
const NppiRect& src_roi,
nvjpegImage_t& dst,
const NppiSize& dst_size,
const NppiRect&, // TODO: Support ROI
NppStreamContext& stream,
int index = 0) {
TRACE_EVENT("decoding", "nppiResize");
CHECK_NPP(
resize_func(
src.channel[index],
(int)src.pitch[index],
src_size,
src_roi,
dst.channel[index],
(int)dst.pitch[index],
dst_size,
src_roi,
NPPI_INTER_LANCZOS,
stream),
"Failed to resize the image.");
}
} // namespace
void resize_npp(
nvjpegOutputFormat_t fmt,
nvjpegImage_t src,
int src_width,
int src_height,
nvjpegImage_t dst,
int dst_width,
int dst_height,
uintptr_t stream_handle,
bool sync) {
NppStreamContext stream;
stream.hStream = (cudaStream_t)stream_handle;
NppiSize src_size{.width = src_width, .height = src_height};
NppiSize dst_size{.width = dst_width, .height = dst_height};
// TODO: support ROI
NppiRect src_roi{.x = 0, .y = 0, .width = src_width, .height = src_height};
NppiRect dst_roi{.x = 0, .y = 0, .width = dst_width, .height = dst_height};
switch (fmt) {
case NVJPEG_OUTPUT_RGBI:
[[fallthrough]];
case NVJPEG_OUTPUT_BGRI:
resize<nppiResize_8u_C3R_Ctx>(
src, src_size, src_roi, dst, dst_size, dst_roi, stream);
break;
case NVJPEG_OUTPUT_Y:
resize<nppiResize_8u_C1R_Ctx>(
src, src_size, src_roi, dst, dst_size, dst_roi, stream);
break;
case NVJPEG_OUTPUT_RGB:
[[fallthrough]];
case NVJPEG_OUTPUT_BGR:
resize<nppiResize_8u_C1R_Ctx>(
src, src_size, src_roi, dst, dst_size, dst_roi, stream, 0);
resize<nppiResize_8u_C1R_Ctx>(
src, src_size, src_roi, dst, dst_size, dst_roi, stream, 1);
resize<nppiResize_8u_C1R_Ctx>(
src, src_size, src_roi, dst, dst_size, dst_roi, stream, 2);
break;
default:
// It should be already handled by `get_nvjpeg_output_format`
SPDL_FAIL_INTERNAL(
fmt::format("Unexpected output format: {}", to_string(fmt)));
}
if (sync) {
CHECK_CUDA(
cudaStreamSynchronize((cudaStream_t)stream_handle),
"Failed to synchronize stream after NPP resize.");
}
}
} // namespace spdl::cuda::detail