-
Notifications
You must be signed in to change notification settings - Fork 10
Add custom bt601 full range cuda kernel and fix color conversion #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
examples/camera_streamer/operators/nv_stream_decoder/nv12_to_rgb.cu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include "nv12_to_rgb.cuh" | ||
|
|
||
| namespace isaac_teleop::cam_streamer | ||
| { | ||
|
|
||
| // Full-range BT.601 NV12 -> RGB (ITU-T T.871 / JFIF). | ||
| // Coefficients derived from BT.601 luma weights (Kr=0.299, Kb=0.114). | ||
| // See: https://www.itu.int/rec/T-REC-T.871 | ||
| __global__ void nv12_to_rgb_fullrange_kernel(const uint8_t* __restrict__ y_plane, | ||
| const uint8_t* __restrict__ uv_plane, | ||
| int y_pitch, | ||
| uint8_t* __restrict__ dst, | ||
| int dst_pitch, | ||
| int width, | ||
| int height) | ||
| { | ||
| const int x = blockIdx.x * blockDim.x + threadIdx.x; | ||
| const int y = blockIdx.y * blockDim.y + threadIdx.y; | ||
|
|
||
| if (x >= width || y >= height) | ||
| return; | ||
|
|
||
| const float Y = static_cast<float>(y_plane[y * y_pitch + x]); | ||
| const int uv_x = (x & ~1); | ||
| const int uv_y = y >> 1; | ||
| const int uv_offset = uv_y * y_pitch + uv_x; | ||
|
|
||
| const float Cb = static_cast<float>(uv_plane[uv_offset]) - 128.0f; | ||
| const float Cr = static_cast<float>(uv_plane[uv_offset + 1]) - 128.0f; | ||
|
|
||
| const float R = Y + 1.402f * Cr; | ||
| const float G = Y - 0.34414f * Cb - 0.71414f * Cr; | ||
| const float B = Y + 1.772f * Cb; | ||
|
|
||
| const int dst_offset = y * dst_pitch + x * 3; | ||
| dst[dst_offset] = static_cast<uint8_t>(fminf(fmaxf(R, 0.0f), 255.0f)); | ||
| dst[dst_offset + 1] = static_cast<uint8_t>(fminf(fmaxf(G, 0.0f), 255.0f)); | ||
| dst[dst_offset + 2] = static_cast<uint8_t>(fminf(fmaxf(B, 0.0f), 255.0f)); | ||
| } | ||
|
|
||
| void nv12_to_rgb_fullrange_bt601(const uint8_t* y_plane, | ||
| const uint8_t* uv_plane, | ||
| int y_pitch, | ||
| uint8_t* dst, | ||
| int dst_pitch, | ||
| int width, | ||
| int height, | ||
| cudaStream_t stream) | ||
| { | ||
| dim3 block(16, 16); | ||
| dim3 grid((width + block.x - 1) / block.x, | ||
| (height + block.y - 1) / block.y); | ||
|
|
||
| nv12_to_rgb_fullrange_kernel<<<grid, block, 0, stream>>>( | ||
| y_plane, uv_plane, y_pitch, dst, dst_pitch, width, height); | ||
| } | ||
|
|
||
| } // namespace isaac_teleop::cam_streamer | ||
36 changes: 36 additions & 0 deletions
36
examples/camera_streamer/operators/nv_stream_decoder/nv12_to_rgb.cuh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef NV12_TO_RGB_CUH | ||
| #define NV12_TO_RGB_CUH | ||
|
|
||
| #include <cuda_runtime.h> | ||
| #include <cstdint> | ||
|
|
||
| namespace isaac_teleop::cam_streamer | ||
| { | ||
|
|
||
| /** | ||
| * Full-range BT.601 NV12 -> packed RGB conversion. | ||
| * Coefficients from ITU-T T.871 (https://www.itu.int/rec/T-REC-T.871). | ||
| * | ||
| * NPP's NV12-to-RGB functions don't cover this combination: 709CSC is | ||
| * BT.709 limited-range, 709HDTV is BT.709 full-range, and the plain | ||
| * nppiNV12ToRGB_8u_P2C3R uses BT.601 but its range is unspecified in | ||
| * the docs. This single-pass kernel fills the gap for cameras like | ||
| * OAK-D whose VPU encoder outputs full-range BT.601 NV12. | ||
| */ | ||
| void nv12_to_rgb_fullrange_bt601(const uint8_t* y_plane, | ||
| const uint8_t* uv_plane, | ||
| int y_pitch, | ||
| uint8_t* dst, | ||
| int dst_pitch, | ||
| int width, | ||
| int height, | ||
| cudaStream_t stream = 0); | ||
|
|
||
| } // namespace isaac_teleop::cam_streamer | ||
|
|
||
| #endif /* NV12_TO_RGB_CUH */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.