torchcodec 1.0.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torchcodec/__init__.py +50 -0
- torchcodec/_core/AVIOContextHolder.cpp +85 -0
- torchcodec/_core/AVIOContextHolder.h +44 -0
- torchcodec/_core/AudioCommon.cpp +101 -0
- torchcodec/_core/AudioCommon.h +48 -0
- torchcodec/_core/AudioConverter.cpp +130 -0
- torchcodec/_core/AudioConverter.h +59 -0
- torchcodec/_core/BetaCudaDeviceInterface.cpp +1635 -0
- torchcodec/_core/BetaCudaDeviceInterface.h +364 -0
- torchcodec/_core/CMakeLists.txt +661 -0
- torchcodec/_core/CUDACommon.cpp +129 -0
- torchcodec/_core/CUDACommon.h +115 -0
- torchcodec/_core/Cache.h +126 -0
- torchcodec/_core/CodecParameters.cpp +98 -0
- torchcodec/_core/CodecParameters.h +59 -0
- torchcodec/_core/ColorConverter.cpp +92 -0
- torchcodec/_core/ColorConverter.h +40 -0
- torchcodec/_core/CpuDeviceInterface.cpp +654 -0
- torchcodec/_core/CpuDeviceInterface.h +153 -0
- torchcodec/_core/CudaDeviceInterface.cpp +519 -0
- torchcodec/_core/CudaDeviceInterface.h +92 -0
- torchcodec/_core/DecodeAvif.cpp +284 -0
- torchcodec/_core/DecodeAvif.h +28 -0
- torchcodec/_core/DecodeGif.cpp +488 -0
- torchcodec/_core/DecodeGif.h +32 -0
- torchcodec/_core/DecodeHeic.cpp +306 -0
- torchcodec/_core/DecodeHeic.h +18 -0
- torchcodec/_core/DecodeJpeg.cpp +489 -0
- torchcodec/_core/DecodeJpeg.h +17 -0
- torchcodec/_core/DecodeJpegCuda.cpp +638 -0
- torchcodec/_core/DecodeJpegCuda.h +92 -0
- torchcodec/_core/DecodePng.cpp +569 -0
- torchcodec/_core/DecodePng.h +31 -0
- torchcodec/_core/DecodeWebp.cpp +238 -0
- torchcodec/_core/DecodeWebp.h +17 -0
- torchcodec/_core/Demuxer.cpp +415 -0
- torchcodec/_core/Demuxer.h +158 -0
- torchcodec/_core/DeviceInterface.cpp +152 -0
- torchcodec/_core/DeviceInterface.h +255 -0
- torchcodec/_core/EncodeJpeg.cpp +223 -0
- torchcodec/_core/EncodeJpeg.h +19 -0
- torchcodec/_core/EncodeJpegCuda.cpp +272 -0
- torchcodec/_core/EncodeJpegCuda.h +56 -0
- torchcodec/_core/EncodePng.cpp +207 -0
- torchcodec/_core/EncodePng.h +19 -0
- torchcodec/_core/Encoder.cpp +1023 -0
- torchcodec/_core/Encoder.h +144 -0
- torchcodec/_core/Exif.h +241 -0
- torchcodec/_core/FFMPEGCommon.cpp +1159 -0
- torchcodec/_core/FFMPEGCommon.h +506 -0
- torchcodec/_core/FileIO.cpp +78 -0
- torchcodec/_core/FileIO.h +34 -0
- torchcodec/_core/FileLikeIO.cpp +76 -0
- torchcodec/_core/FileLikeIO.h +68 -0
- torchcodec/_core/FilterGraph.cpp +211 -0
- torchcodec/_core/FilterGraph.h +59 -0
- torchcodec/_core/Frame.cpp +75 -0
- torchcodec/_core/Frame.h +76 -0
- torchcodec/_core/IOInterface.h +44 -0
- torchcodec/_core/ImageCommon.h +113 -0
- torchcodec/_core/Logging.cpp +42 -0
- torchcodec/_core/Logging.h +38 -0
- torchcodec/_core/Metadata.cpp +312 -0
- torchcodec/_core/Metadata.h +115 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.cpp +324 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.h +14 -0
- torchcodec/_core/NVDECCache.cpp +111 -0
- torchcodec/_core/NVDECCache.h +136 -0
- torchcodec/_core/NVDECCacheConfig.cpp +61 -0
- torchcodec/_core/NVDECCacheConfig.h +30 -0
- torchcodec/_core/PacketDecoder.cpp +358 -0
- torchcodec/_core/PacketDecoder.h +140 -0
- torchcodec/_core/SingleStreamDecoder.cpp +1856 -0
- torchcodec/_core/SingleStreamDecoder.h +424 -0
- torchcodec/_core/StableABICompat.h +213 -0
- torchcodec/_core/StreamOptions.h +93 -0
- torchcodec/_core/SwScale.cpp +124 -0
- torchcodec/_core/SwScale.h +67 -0
- torchcodec/_core/TensorIO.cpp +136 -0
- torchcodec/_core/TensorIO.h +53 -0
- torchcodec/_core/Transform.cpp +207 -0
- torchcodec/_core/Transform.h +125 -0
- torchcodec/_core/ValidationUtils.cpp +65 -0
- torchcodec/_core/ValidationUtils.h +29 -0
- torchcodec/_core/WavDecoder.cpp +494 -0
- torchcodec/_core/WavDecoder.h +73 -0
- torchcodec/_core/__init__.py +45 -0
- torchcodec/_core/_decoder_utils.py +258 -0
- torchcodec/_core/_ffmpeg_op_names.py +95 -0
- torchcodec/_core/_ffmpeg_ops.py +699 -0
- torchcodec/_core/_file_like.py +221 -0
- torchcodec/_core/_metadata.py +402 -0
- torchcodec/_core/color_conversion.cpp +316 -0
- torchcodec/_core/color_conversion.cu +391 -0
- torchcodec/_core/color_conversion.h +150 -0
- torchcodec/_core/custom_ops.cpp +2438 -0
- torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake +192 -0
- torchcodec/_core/fetch_avif_from_s3.cmake +81 -0
- torchcodec/_core/giflib/README +28 -0
- torchcodec/_core/giflib/dgif_lib.c +1313 -0
- torchcodec/_core/giflib/gif_hash.c +129 -0
- torchcodec/_core/giflib/gif_hash.h +43 -0
- torchcodec/_core/giflib/gif_lib.h +292 -0
- torchcodec/_core/giflib/gif_lib_private.h +73 -0
- torchcodec/_core/giflib/gifalloc.c +426 -0
- torchcodec/_core/giflib/openbsd-reallocarray.c +74 -0
- torchcodec/_core/heic_custom_ops.cpp +20 -0
- torchcodec/_core/image_custom_ops.cpp +137 -0
- torchcodec/_core/nvcuvid_include/cuviddec.h +1374 -0
- torchcodec/_core/nvcuvid_include/nvcuvid.h +610 -0
- torchcodec/_core/ops.py +103 -0
- torchcodec/_core/sources.bzl +113 -0
- torchcodec/_frame.py +146 -0
- torchcodec/_internally_replaced_utils.py +129 -0
- torchcodec/_logging.py +59 -0
- torchcodec/decoders/__init__.py +50 -0
- torchcodec/decoders/_audio_decoder.py +183 -0
- torchcodec/decoders/_blocks/__init__.py +48 -0
- torchcodec/decoders/_blocks/_audio_converter.py +155 -0
- torchcodec/decoders/_blocks/_codec_parameters.py +229 -0
- torchcodec/decoders/_blocks/_color_converter.py +107 -0
- torchcodec/decoders/_blocks/_demuxer.py +678 -0
- torchcodec/decoders/_blocks/_frame.py +577 -0
- torchcodec/decoders/_blocks/_helpers.py +53 -0
- torchcodec/decoders/_blocks/_packet_decoder.py +357 -0
- torchcodec/decoders/_decoder_utils.py +128 -0
- torchcodec/decoders/_image_decoders.py +1575 -0
- torchcodec/decoders/_video_decoder.py +592 -0
- torchcodec/decoders/_wav_decoder.py +131 -0
- torchcodec/encoders/__init__.py +4 -0
- torchcodec/encoders/_audio_encoder.py +167 -0
- torchcodec/encoders/_image_encoders.py +181 -0
- torchcodec/encoders/_multi_stream_encoder.py +272 -0
- torchcodec/encoders/_video_encoder.py +227 -0
- torchcodec/jpeg8.dll +0 -0
- torchcodec/libavif.dll +0 -0
- torchcodec/libpng16.dll +0 -0
- torchcodec/libsharpyuv.dll +0 -0
- torchcodec/libtorchcodec_core4.dll +0 -0
- torchcodec/libtorchcodec_core5.dll +0 -0
- torchcodec/libtorchcodec_core6.dll +0 -0
- torchcodec/libtorchcodec_core7.dll +0 -0
- torchcodec/libtorchcodec_core8.dll +0 -0
- torchcodec/libtorchcodec_core9.dll +0 -0
- torchcodec/libtorchcodec_custom_ops4.dll +0 -0
- torchcodec/libtorchcodec_custom_ops5.dll +0 -0
- torchcodec/libtorchcodec_custom_ops6.dll +0 -0
- torchcodec/libtorchcodec_custom_ops7.dll +0 -0
- torchcodec/libtorchcodec_custom_ops8.dll +0 -0
- torchcodec/libtorchcodec_custom_ops9.dll +0 -0
- torchcodec/libtorchcodec_heic.dll +0 -0
- torchcodec/libtorchcodec_image.dll +0 -0
- torchcodec/libwebp.dll +0 -0
- torchcodec/libwebpdecoder.dll +0 -0
- torchcodec/libwebpdemux.dll +0 -0
- torchcodec/libwebpmux.dll +0 -0
- torchcodec/libzstd.dll +0 -0
- torchcodec/py.typed +0 -0
- torchcodec/samplers/__init__.py +2 -0
- torchcodec/samplers/_common.py +83 -0
- torchcodec/samplers/_index_based.py +287 -0
- torchcodec/samplers/_time_based.py +360 -0
- torchcodec/share/cmake/TorchCodec/TorchCodecConfig.cmake +76 -0
- torchcodec/share/cmake/TorchCodec/ffmpeg_versions.cmake +128 -0
- torchcodec/transforms/__init__.py +12 -0
- torchcodec/transforms/_decoder_transforms.py +375 -0
- torchcodec/version.py +1 -0
- torchcodec/zlib.dll +0 -0
- torchcodec-1.0.0.dist-info/METADATA +275 -0
- torchcodec-1.0.0.dist-info/RECORD +179 -0
- torchcodec-1.0.0.dist-info/WHEEL +5 -0
- torchcodec-1.0.0.dist-info/licenses/LICENSE +28 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/COPYING.dav1d +23 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libavif +387 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libjpeg-turbo +135 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libpng +134 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libwebp +30 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libyuv +29 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.zlib +22 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#include "color_conversion.h"
|
|
8
|
+
#include "CUDACommon.h"
|
|
9
|
+
#include "StableABICompat.h"
|
|
10
|
+
#include "ValidationUtils.h"
|
|
11
|
+
|
|
12
|
+
namespace facebook::torchcodec {
|
|
13
|
+
|
|
14
|
+
/* clang-format off */
|
|
15
|
+
// Note: [YUV -> RGB Color Conversion]
|
|
16
|
+
//
|
|
17
|
+
// The color conversion matrix is derived from the color space's luma
|
|
18
|
+
// coefficients (kr, kg, kb) and the color range (full or limited).
|
|
19
|
+
//
|
|
20
|
+
// The forward (RGB -> YUV) transform is:
|
|
21
|
+
// Y = kr*R + kg*G + kb*B
|
|
22
|
+
// U = (B - Y) / (2*(1-kb))
|
|
23
|
+
// V = (R - Y) / (2*(1-kr))
|
|
24
|
+
//
|
|
25
|
+
// Inverting gives the YUV -> RGB matrix. The 4th column of each row encodes
|
|
26
|
+
// all additive offsets (UV centering and, for limited range, the Y pedestal)
|
|
27
|
+
// so that the kernel can apply the matrix directly to raw sample values
|
|
28
|
+
// without any pre-processing.
|
|
29
|
+
//
|
|
30
|
+
// For full range:
|
|
31
|
+
// Input Y in [0, maxVal], U/V in [0, maxVal] centered at maxVal/2+1.
|
|
32
|
+
// Output RGB in [0, outScale].
|
|
33
|
+
//
|
|
34
|
+
// For limited (studio) range:
|
|
35
|
+
// Y in [16*s, 235*s], U/V in [16*s, 240*s] where s = 2^(bitDepth-8).
|
|
36
|
+
// Mapped to output RGB in [0, outScale].
|
|
37
|
+
//
|
|
38
|
+
// The same formula works for both NV12 (bitDepth=8, outScale=255) and P016
|
|
39
|
+
// (bitDepth=10 or 12, outScale=65535).
|
|
40
|
+
/* clang-format on */
|
|
41
|
+
|
|
42
|
+
LumaCoefficients get_luma_coefficients(AVColorSpace colorspace) {
|
|
43
|
+
switch (colorspace) {
|
|
44
|
+
case AVCOL_SPC_BT709:
|
|
45
|
+
return {0.2126f, 0.7152f, 0.0722f};
|
|
46
|
+
case AVCOL_SPC_BT2020_NCL:
|
|
47
|
+
case AVCOL_SPC_BT2020_CL:
|
|
48
|
+
return {0.2627f, 0.6780f, 0.0593f};
|
|
49
|
+
default:
|
|
50
|
+
// BT.601: default for unspecified colorspace, matching FFmpeg's
|
|
51
|
+
// swscale behavior (sws_getCoefficients(SWS_CS_DEFAULT) returns
|
|
52
|
+
// BT.601 coefficients).
|
|
53
|
+
return {0.299f, 0.587f, 0.114f};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
void compute_color_conversion_matrix(
|
|
58
|
+
AVColorSpace colorspace,
|
|
59
|
+
AVColorRange color_range,
|
|
60
|
+
int bit_depth,
|
|
61
|
+
float out_scale,
|
|
62
|
+
float out_matrix[3][4]) {
|
|
63
|
+
auto [kr, kg, kb] = get_luma_coefficients(colorspace);
|
|
64
|
+
|
|
65
|
+
float v_scale = 2.0f * (1.0f - kr);
|
|
66
|
+
float u_scale = 2.0f * (1.0f - kb);
|
|
67
|
+
float gu_coeff = -(2.0f * kb * (1.0f - kb)) / kg;
|
|
68
|
+
float gv_coeff = -(2.0f * kr * (1.0f - kr)) / kg;
|
|
69
|
+
|
|
70
|
+
float max_val = static_cast<float>((1 << bit_depth) - 1);
|
|
71
|
+
|
|
72
|
+
bool is_full_range = (color_range == AVCOL_RANGE_JPEG);
|
|
73
|
+
|
|
74
|
+
if (is_full_range) {
|
|
75
|
+
float y_scale = out_scale / max_val;
|
|
76
|
+
float uv_center = static_cast<float>(1 << (bit_depth - 1));
|
|
77
|
+
|
|
78
|
+
out_matrix[0][0] = y_scale;
|
|
79
|
+
out_matrix[0][1] = 0.0f;
|
|
80
|
+
out_matrix[0][2] = v_scale * out_scale / max_val;
|
|
81
|
+
out_matrix[0][3] = -v_scale * uv_center * out_scale / max_val;
|
|
82
|
+
|
|
83
|
+
out_matrix[1][0] = y_scale;
|
|
84
|
+
out_matrix[1][1] = gu_coeff * out_scale / max_val;
|
|
85
|
+
out_matrix[1][2] = gv_coeff * out_scale / max_val;
|
|
86
|
+
out_matrix[1][3] = -(gu_coeff + gv_coeff) * uv_center * out_scale / max_val;
|
|
87
|
+
|
|
88
|
+
out_matrix[2][0] = y_scale;
|
|
89
|
+
out_matrix[2][1] = u_scale * out_scale / max_val;
|
|
90
|
+
out_matrix[2][2] = 0.0f;
|
|
91
|
+
out_matrix[2][3] = -u_scale * uv_center * out_scale / max_val;
|
|
92
|
+
} else {
|
|
93
|
+
float s = static_cast<float>(1 << (bit_depth - 8));
|
|
94
|
+
float y_off = 16.0f * s;
|
|
95
|
+
float y_range = 219.0f * s;
|
|
96
|
+
float uv_off = 128.0f * s;
|
|
97
|
+
float uv_range = 224.0f * s;
|
|
98
|
+
|
|
99
|
+
float y_coeff = out_scale / y_range;
|
|
100
|
+
float uv_coeff_u = out_scale / uv_range;
|
|
101
|
+
float uv_coeff_v = out_scale / uv_range;
|
|
102
|
+
|
|
103
|
+
out_matrix[0][0] = y_coeff;
|
|
104
|
+
out_matrix[0][1] = 0.0f;
|
|
105
|
+
out_matrix[0][2] = v_scale * uv_coeff_v;
|
|
106
|
+
out_matrix[0][3] = -y_coeff * y_off - v_scale * uv_coeff_v * uv_off;
|
|
107
|
+
|
|
108
|
+
out_matrix[1][0] = y_coeff;
|
|
109
|
+
out_matrix[1][1] = gu_coeff * uv_coeff_u;
|
|
110
|
+
out_matrix[1][2] = gv_coeff * uv_coeff_v;
|
|
111
|
+
out_matrix[1][3] = -y_coeff * y_off - gu_coeff * uv_coeff_u * uv_off -
|
|
112
|
+
gv_coeff * uv_coeff_v * uv_off;
|
|
113
|
+
|
|
114
|
+
out_matrix[2][0] = y_coeff;
|
|
115
|
+
out_matrix[2][1] = u_scale * uv_coeff_u;
|
|
116
|
+
out_matrix[2][2] = 0.0f;
|
|
117
|
+
out_matrix[2][3] = -y_coeff * y_off - u_scale * uv_coeff_u * uv_off;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
void maybe_update_color_matrix(
|
|
122
|
+
CachedColorMatrix& cached_color_matrix,
|
|
123
|
+
AVColorSpace colorspace,
|
|
124
|
+
AVColorRange color_range,
|
|
125
|
+
int bit_depth,
|
|
126
|
+
float out_scale) {
|
|
127
|
+
if (cached_color_matrix.valid &&
|
|
128
|
+
cached_color_matrix.colorspace == colorspace &&
|
|
129
|
+
cached_color_matrix.color_range == color_range &&
|
|
130
|
+
cached_color_matrix.bit_depth == bit_depth &&
|
|
131
|
+
cached_color_matrix.out_scale == out_scale) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
compute_color_conversion_matrix(
|
|
136
|
+
colorspace,
|
|
137
|
+
color_range,
|
|
138
|
+
bit_depth,
|
|
139
|
+
out_scale,
|
|
140
|
+
cached_color_matrix.matrix);
|
|
141
|
+
cached_color_matrix.colorspace = colorspace;
|
|
142
|
+
cached_color_matrix.color_range = color_range;
|
|
143
|
+
cached_color_matrix.bit_depth = bit_depth;
|
|
144
|
+
cached_color_matrix.out_scale = out_scale;
|
|
145
|
+
cached_color_matrix.valid = true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
void compute_rgb_to_yuv_matrix(
|
|
149
|
+
AVColorSpace colorspace,
|
|
150
|
+
AVColorRange color_range,
|
|
151
|
+
float out_matrix[3][4]) {
|
|
152
|
+
auto [kr, kg, kb] = get_luma_coefficients(colorspace);
|
|
153
|
+
|
|
154
|
+
float u_scale = 2.0f * (1.0f - kb);
|
|
155
|
+
float v_scale = 2.0f * (1.0f - kr);
|
|
156
|
+
|
|
157
|
+
// Full-range RGB [0,255] -> YUV forward matrix
|
|
158
|
+
// Y = kr*R + kg*G + kb*B
|
|
159
|
+
// U = (-kr*R - kg*G + (1-kb)*B) / uScale
|
|
160
|
+
// V = ((1-kr)*R - kg*G - kb*B) / vScale
|
|
161
|
+
float y_row[3] = {kr, kg, kb};
|
|
162
|
+
float u_row[3] = {-kr / u_scale, -kg / u_scale, (1.0f - kb) / u_scale};
|
|
163
|
+
float v_row[3] = {(1.0f - kr) / v_scale, -kg / v_scale, -kb / v_scale};
|
|
164
|
+
|
|
165
|
+
bool is_full_range = (color_range == AVCOL_RANGE_JPEG);
|
|
166
|
+
|
|
167
|
+
if (is_full_range) {
|
|
168
|
+
for (int i = 0; i < 3; i++) {
|
|
169
|
+
out_matrix[0][i] = y_row[i];
|
|
170
|
+
out_matrix[1][i] = u_row[i];
|
|
171
|
+
out_matrix[2][i] = v_row[i];
|
|
172
|
+
}
|
|
173
|
+
out_matrix[0][3] = 0.0f;
|
|
174
|
+
out_matrix[1][3] = 128.0f;
|
|
175
|
+
out_matrix[2][3] = 128.0f;
|
|
176
|
+
} else {
|
|
177
|
+
// Limited range: Y scaled to [16, 235], UV scaled to [16, 240]
|
|
178
|
+
float y_scale = 219.0f / 255.0f;
|
|
179
|
+
float uv_limited_scale = 224.0f / 255.0f;
|
|
180
|
+
for (int i = 0; i < 3; i++) {
|
|
181
|
+
out_matrix[0][i] = y_row[i] * y_scale;
|
|
182
|
+
out_matrix[1][i] = u_row[i] * uv_limited_scale;
|
|
183
|
+
out_matrix[2][i] = v_row[i] * uv_limited_scale;
|
|
184
|
+
}
|
|
185
|
+
out_matrix[0][3] = 16.0f;
|
|
186
|
+
out_matrix[1][3] = 128.0f;
|
|
187
|
+
out_matrix[2][3] = 128.0f;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
torch::stable::Tensor convert_yuv_frame_to_rgb(
|
|
192
|
+
const AVFrame& av_frame,
|
|
193
|
+
const StableDevice& device,
|
|
194
|
+
cudaStream_t stream,
|
|
195
|
+
std::optional<torch::stable::Tensor> pre_allocated_output_tensor,
|
|
196
|
+
const FrameDims& output_dims,
|
|
197
|
+
AVPixelFormat pix_fmt,
|
|
198
|
+
CachedColorMatrix& cached_color_matrix) {
|
|
199
|
+
const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(pix_fmt);
|
|
200
|
+
STD_TORCH_CHECK(desc != nullptr, "Unknown pixel format on decoded frame");
|
|
201
|
+
int bit_depth = desc->comp[0].depth;
|
|
202
|
+
int bit_shift = desc->comp[0].shift;
|
|
203
|
+
bool is_16bit = bit_depth > 8;
|
|
204
|
+
bool is_444 = desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0;
|
|
205
|
+
|
|
206
|
+
float out_scale = is_16bit ? 65535.0f : 255.0f;
|
|
207
|
+
OutputDtype out_dtype = is_16bit ? OutputDtype::FLOAT32 : OutputDtype::UINT8;
|
|
208
|
+
|
|
209
|
+
// Dimensions may be odd (NVDEC display area for VP9 etc.). The semi-planar
|
|
210
|
+
// kernel works on 2x2 blocks and NVDEC surfaces are allocated even-sized, so
|
|
211
|
+
// we run it over the even-rounded surface and crop to outputDims. The 4:4:4
|
|
212
|
+
// kernel is per-pixel and its frames are allocated at their exact size.
|
|
213
|
+
int kernel_height =
|
|
214
|
+
is_444 ? av_frame.height : round_up_to_even(av_frame.height);
|
|
215
|
+
int kernel_width = is_444 ? av_frame.width : round_up_to_even(av_frame.width);
|
|
216
|
+
|
|
217
|
+
int out_height = output_dims.height;
|
|
218
|
+
int out_width = output_dims.width;
|
|
219
|
+
bool needs_crop =
|
|
220
|
+
(out_height != kernel_height) || (out_width != kernel_width);
|
|
221
|
+
|
|
222
|
+
torch::stable::Tensor dst;
|
|
223
|
+
if (needs_crop) {
|
|
224
|
+
dst = allocate_empty_hwc_tensor(
|
|
225
|
+
FrameDims(kernel_height, kernel_width), device, out_dtype);
|
|
226
|
+
} else if (pre_allocated_output_tensor.has_value()) {
|
|
227
|
+
dst = pre_allocated_output_tensor.value();
|
|
228
|
+
} else {
|
|
229
|
+
dst = allocate_empty_hwc_tensor(
|
|
230
|
+
FrameDims(out_height, out_width), device, out_dtype);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
maybe_update_color_matrix(
|
|
234
|
+
cached_color_matrix,
|
|
235
|
+
av_frame.colorspace,
|
|
236
|
+
av_frame.color_range,
|
|
237
|
+
bit_depth,
|
|
238
|
+
out_scale);
|
|
239
|
+
|
|
240
|
+
int rgb_pitch = validate_int64_to_int(
|
|
241
|
+
dst.stride(0) * (is_16bit ? 2 : 1), "dst RGB row size in bytes");
|
|
242
|
+
|
|
243
|
+
if (is_444 && is_16bit) {
|
|
244
|
+
launch_yuv444_to_rgb16_kernel(
|
|
245
|
+
reinterpret_cast<const uint16_t*>(av_frame.data[0]),
|
|
246
|
+
reinterpret_cast<const uint16_t*>(av_frame.data[1]),
|
|
247
|
+
reinterpret_cast<const uint16_t*>(av_frame.data[2]),
|
|
248
|
+
dst.mutable_data_ptr<uint16_t>(),
|
|
249
|
+
kernel_width,
|
|
250
|
+
kernel_height,
|
|
251
|
+
av_frame.linesize[0],
|
|
252
|
+
av_frame.linesize[1],
|
|
253
|
+
av_frame.linesize[2],
|
|
254
|
+
rgb_pitch,
|
|
255
|
+
bit_shift,
|
|
256
|
+
cached_color_matrix.matrix,
|
|
257
|
+
stream);
|
|
258
|
+
} else if (is_444) {
|
|
259
|
+
launch_yuv444_to_rgb_kernel(
|
|
260
|
+
av_frame.data[0],
|
|
261
|
+
av_frame.data[1],
|
|
262
|
+
av_frame.data[2],
|
|
263
|
+
dst.mutable_data_ptr<uint8_t>(),
|
|
264
|
+
kernel_width,
|
|
265
|
+
kernel_height,
|
|
266
|
+
av_frame.linesize[0],
|
|
267
|
+
av_frame.linesize[1],
|
|
268
|
+
av_frame.linesize[2],
|
|
269
|
+
rgb_pitch,
|
|
270
|
+
cached_color_matrix.matrix,
|
|
271
|
+
stream);
|
|
272
|
+
} else if (is_16bit) {
|
|
273
|
+
launch_p016_to_rgb16_kernel(
|
|
274
|
+
reinterpret_cast<const uint16_t*>(av_frame.data[0]),
|
|
275
|
+
reinterpret_cast<const uint16_t*>(av_frame.data[1]),
|
|
276
|
+
dst.mutable_data_ptr<uint16_t>(),
|
|
277
|
+
kernel_width,
|
|
278
|
+
kernel_height,
|
|
279
|
+
av_frame.linesize[0],
|
|
280
|
+
av_frame.linesize[1],
|
|
281
|
+
rgb_pitch,
|
|
282
|
+
bit_shift,
|
|
283
|
+
cached_color_matrix.matrix,
|
|
284
|
+
stream);
|
|
285
|
+
} else {
|
|
286
|
+
launch_nv12_to_rgb_kernel(
|
|
287
|
+
av_frame.data[0],
|
|
288
|
+
av_frame.data[1],
|
|
289
|
+
dst.mutable_data_ptr<uint8_t>(),
|
|
290
|
+
kernel_width,
|
|
291
|
+
kernel_height,
|
|
292
|
+
av_frame.linesize[0],
|
|
293
|
+
av_frame.linesize[1],
|
|
294
|
+
rgb_pitch,
|
|
295
|
+
cached_color_matrix.matrix,
|
|
296
|
+
stream);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (needs_crop) {
|
|
300
|
+
if (out_height != kernel_height) {
|
|
301
|
+
dst = torch::stable::narrow(dst, /*dim=*/0, /*start=*/0, out_height);
|
|
302
|
+
}
|
|
303
|
+
if (out_width != kernel_width) {
|
|
304
|
+
dst = torch::stable::narrow(dst, /*dim=*/1, /*start=*/0, out_width);
|
|
305
|
+
dst = torch::stable::contiguous(dst);
|
|
306
|
+
}
|
|
307
|
+
if (pre_allocated_output_tensor.has_value()) {
|
|
308
|
+
torch::stable::copy_(pre_allocated_output_tensor.value(), dst);
|
|
309
|
+
return pre_allocated_output_tensor.value();
|
|
310
|
+
}
|
|
311
|
+
return dst;
|
|
312
|
+
}
|
|
313
|
+
return dst;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#include "color_conversion.h"
|
|
8
|
+
|
|
9
|
+
namespace facebook::torchcodec {
|
|
10
|
+
|
|
11
|
+
// NV12 and P016 are semi-planar YUV 4:2:0 formats. Chroma subsampling is
|
|
12
|
+
// 4:2:0 so U,V are subsampled by 2 in both H and W dimensions. This means a
|
|
13
|
+
// single UV pair is responsible for a 2x2 pixel block.
|
|
14
|
+
// The Y plane is YYYYY... where each Y corresponds to one pixel.
|
|
15
|
+
// The UV plane is interleaved as UVUVUVUV...
|
|
16
|
+
// NV12 uses 8-bit samples (uint8_t), i.e. the Y, U and V values are 8 bits
|
|
17
|
+
// each. P016 uses 16-bit samples (uint16_t) with actual data in the most
|
|
18
|
+
// significant bits (right-shifted by 16 - bitDepth).
|
|
19
|
+
|
|
20
|
+
// Wraps the 3x4 color-conversion matrix so it can be passed as a kernel param,
|
|
21
|
+
// to ensure thread-safety (previous implem had it __global__, which wasn't
|
|
22
|
+
// safe)
|
|
23
|
+
struct ColorMatrix {
|
|
24
|
+
float m[3][4];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Applies the color matrix to one pixel's Y, U, V and writes the 3 RGB samples.
|
|
28
|
+
template <typename T>
|
|
29
|
+
__device__ inline void write_rgb_pixel(
|
|
30
|
+
float y,
|
|
31
|
+
float u,
|
|
32
|
+
float v,
|
|
33
|
+
const ColorMatrix& cm,
|
|
34
|
+
T* out) {
|
|
35
|
+
constexpr float clamp_max = sizeof(T) == 1 ? 255.0f : 65535.0f;
|
|
36
|
+
#pragma unroll
|
|
37
|
+
for (int c = 0; c < 3; ++c) {
|
|
38
|
+
float value = cm.m[c][0] * y + cm.m[c][1] * u + cm.m[c][2] * v + cm.m[c][3];
|
|
39
|
+
out[c] = static_cast<T>(fminf(fmaxf(value, 0.0f), clamp_max));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Takes the Y and UV plane as input, applies the color-conversion matrix and
|
|
44
|
+
// fills the RGB plane as output.
|
|
45
|
+
// Each thread, i.e. each invocation of yuvToRgbKernel, processes a 2x2 block
|
|
46
|
+
// of pixels: this optimizes the UV plan reads, since each UV pair is
|
|
47
|
+
// responsible for a 2x2 block.
|
|
48
|
+
//
|
|
49
|
+
// Y plane (one value per pixel): UV plane (one pair per 2x2 block):
|
|
50
|
+
// +------+------+ +----------+
|
|
51
|
+
// | y1 | y2 | row y | u | v |
|
|
52
|
+
// +------+------+ +----------+
|
|
53
|
+
// | y3 | y4 | row y+1
|
|
54
|
+
// +------+------+
|
|
55
|
+
// col x col x+1
|
|
56
|
+
//
|
|
57
|
+
// We use Vec2T vectorized loads to read {y1,y2} and {y3,y4} in two
|
|
58
|
+
// reads, and {U,V} in one read. Then we apply the color matrix to produce
|
|
59
|
+
// 4 RGB pixels.
|
|
60
|
+
template <typename T, typename Vec2T>
|
|
61
|
+
__global__ void yuv_to_rgb_kernel(
|
|
62
|
+
// __restrict__ tells the compiler those pointers never overlap with each
|
|
63
|
+
// other so it can optimize read and writes more aggressively.
|
|
64
|
+
const T* __restrict__ y_plane,
|
|
65
|
+
const T* __restrict__ uv_plane,
|
|
66
|
+
T* __restrict__ rgb_output,
|
|
67
|
+
int width,
|
|
68
|
+
int height,
|
|
69
|
+
int y_pitch_elements,
|
|
70
|
+
int uv_pitch_elements,
|
|
71
|
+
int rgb_pitch_elements,
|
|
72
|
+
int bit_shift,
|
|
73
|
+
const ColorMatrix cm) {
|
|
74
|
+
// The kernel operates on 2x2 blocks, so it's called H / 2 * W / 2 times.
|
|
75
|
+
// We have to multiply back by 2 to retrieve the output pixel coordinates x
|
|
76
|
+
// and y.
|
|
77
|
+
int x = (blockIdx.x * blockDim.x + threadIdx.x) * 2;
|
|
78
|
+
int y = (blockIdx.y * blockDim.y + threadIdx.y) * 2;
|
|
79
|
+
if (x + 1 >= width || y + 1 >= height) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Vec2T stores two values in .x and .y
|
|
84
|
+
// Here, we read the UV pair in one instruction.
|
|
85
|
+
int uv_idx = (y / 2) * uv_pitch_elements + x;
|
|
86
|
+
Vec2T uv = *reinterpret_cast<const Vec2T*>(&uv_plane[uv_idx]);
|
|
87
|
+
float u = static_cast<float>(uv.x >> bit_shift);
|
|
88
|
+
float v = static_cast<float>(uv.y >> bit_shift);
|
|
89
|
+
|
|
90
|
+
// Similarly, we can read 4 Y values in 2 reads: {y1,y2} then {y3,y4}.
|
|
91
|
+
Vec2T y_pairs[2] = {
|
|
92
|
+
*reinterpret_cast<const Vec2T*>(&y_plane[y * y_pitch_elements + x]),
|
|
93
|
+
*reinterpret_cast<const Vec2T*>(&y_plane[(y + 1) * y_pitch_elements + x])};
|
|
94
|
+
|
|
95
|
+
T* rgb_row = rgb_output + y * rgb_pitch_elements + x * 3;
|
|
96
|
+
#pragma unroll
|
|
97
|
+
for (int row = 0; row < 2; ++row) {
|
|
98
|
+
T a[3], b[3]; // a = {ra, ga, ba}, b = {rb, gb, bb}
|
|
99
|
+
write_rgb_pixel<T>(
|
|
100
|
+
static_cast<float>(y_pairs[row].x >> bit_shift), u, v, cm, a);
|
|
101
|
+
write_rgb_pixel<T>(
|
|
102
|
+
static_cast<float>(y_pairs[row].y >> bit_shift), u, v, cm, b);
|
|
103
|
+
|
|
104
|
+
// 3 vectorized stores rather than 6 scalar ones.
|
|
105
|
+
Vec2T raga = {a[0], a[1]};
|
|
106
|
+
*(reinterpret_cast<Vec2T*>(&rgb_row[0])) = raga;
|
|
107
|
+
Vec2T barb = {a[2], b[0]};
|
|
108
|
+
*(reinterpret_cast<Vec2T*>(&rgb_row[2])) = barb;
|
|
109
|
+
Vec2T gbbb = {b[1], b[2]};
|
|
110
|
+
*(reinterpret_cast<Vec2T*>(&rgb_row[4])) = gbbb;
|
|
111
|
+
|
|
112
|
+
rgb_row += rgb_pitch_elements; // go to next line
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void launch_nv12_to_rgb_kernel(
|
|
117
|
+
const uint8_t* y_plane,
|
|
118
|
+
const uint8_t* uv_plane,
|
|
119
|
+
uint8_t* rgb_output,
|
|
120
|
+
int width,
|
|
121
|
+
int height,
|
|
122
|
+
int y_pitch,
|
|
123
|
+
int uv_pitch,
|
|
124
|
+
int rgb_pitch,
|
|
125
|
+
const float color_matrix[3][4],
|
|
126
|
+
cudaStream_t stream) {
|
|
127
|
+
const auto& cm = *reinterpret_cast<const ColorMatrix*>(color_matrix);
|
|
128
|
+
|
|
129
|
+
dim3 block(32, 2);
|
|
130
|
+
dim3 grid(
|
|
131
|
+
(width / 2 + block.x - 1) / block.x,
|
|
132
|
+
(height / 2 + block.y - 1) / block.y);
|
|
133
|
+
|
|
134
|
+
yuv_to_rgb_kernel<uint8_t, uchar2><<<grid, block, 0, stream>>>(
|
|
135
|
+
y_plane,
|
|
136
|
+
uv_plane,
|
|
137
|
+
rgb_output,
|
|
138
|
+
width,
|
|
139
|
+
height,
|
|
140
|
+
y_pitch,
|
|
141
|
+
uv_pitch,
|
|
142
|
+
rgb_pitch,
|
|
143
|
+
0, // bitShift = 0 for NV12
|
|
144
|
+
cm);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
void launch_p016_to_rgb16_kernel(
|
|
148
|
+
const uint16_t* y_plane,
|
|
149
|
+
const uint16_t* uv_plane,
|
|
150
|
+
uint16_t* rgb_output,
|
|
151
|
+
int width,
|
|
152
|
+
int height,
|
|
153
|
+
int y_pitch,
|
|
154
|
+
int uv_pitch,
|
|
155
|
+
int rgb_pitch,
|
|
156
|
+
int bit_shift,
|
|
157
|
+
const float color_matrix[3][4],
|
|
158
|
+
cudaStream_t stream) {
|
|
159
|
+
const auto& cm = *reinterpret_cast<const ColorMatrix*>(color_matrix);
|
|
160
|
+
|
|
161
|
+
int y_pitch_elements = y_pitch / static_cast<int>(sizeof(uint16_t));
|
|
162
|
+
int uv_pitch_elements = uv_pitch / static_cast<int>(sizeof(uint16_t));
|
|
163
|
+
int rgb_pitch_elements = rgb_pitch / static_cast<int>(sizeof(uint16_t));
|
|
164
|
+
|
|
165
|
+
dim3 block(32, 2);
|
|
166
|
+
dim3 grid(
|
|
167
|
+
(width / 2 + block.x - 1) / block.x,
|
|
168
|
+
(height / 2 + block.y - 1) / block.y);
|
|
169
|
+
|
|
170
|
+
yuv_to_rgb_kernel<uint16_t, ushort2><<<grid, block, 0, stream>>>(
|
|
171
|
+
y_plane,
|
|
172
|
+
uv_plane,
|
|
173
|
+
rgb_output,
|
|
174
|
+
width,
|
|
175
|
+
height,
|
|
176
|
+
y_pitch_elements,
|
|
177
|
+
uv_pitch_elements,
|
|
178
|
+
rgb_pitch_elements,
|
|
179
|
+
bit_shift,
|
|
180
|
+
cm);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// YUV 4:4:4 is planar with three full-resolution planes, so there is no 2x2
|
|
184
|
+
// block to exploit: one thread per pixel. Used for the CPU-fallback frames that
|
|
185
|
+
// we upload as yuv444p / yuv444p16le rather than downsample to NV12.
|
|
186
|
+
template <typename T>
|
|
187
|
+
__global__ void yuv444_to_rgb_kernel(
|
|
188
|
+
const T* __restrict__ y_plane,
|
|
189
|
+
const T* __restrict__ u_plane,
|
|
190
|
+
const T* __restrict__ v_plane,
|
|
191
|
+
T* __restrict__ rgb_output,
|
|
192
|
+
int width,
|
|
193
|
+
int height,
|
|
194
|
+
int y_pitch_elements,
|
|
195
|
+
int u_pitch_elements,
|
|
196
|
+
int v_pitch_elements,
|
|
197
|
+
int rgb_pitch_elements,
|
|
198
|
+
int bit_shift,
|
|
199
|
+
const ColorMatrix cm) {
|
|
200
|
+
int x = blockIdx.x * blockDim.x + threadIdx.x;
|
|
201
|
+
int y = blockIdx.y * blockDim.y + threadIdx.y;
|
|
202
|
+
if (x >= width || y >= height) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
write_rgb_pixel<T>(
|
|
207
|
+
static_cast<float>(y_plane[y * y_pitch_elements + x] >> bit_shift),
|
|
208
|
+
static_cast<float>(u_plane[y * u_pitch_elements + x] >> bit_shift),
|
|
209
|
+
static_cast<float>(v_plane[y * v_pitch_elements + x] >> bit_shift),
|
|
210
|
+
cm,
|
|
211
|
+
rgb_output + y * rgb_pitch_elements + x * 3);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
void launch_yuv444_to_rgb_kernel(
|
|
215
|
+
const uint8_t* y_plane,
|
|
216
|
+
const uint8_t* u_plane,
|
|
217
|
+
const uint8_t* v_plane,
|
|
218
|
+
uint8_t* rgb_output,
|
|
219
|
+
int width,
|
|
220
|
+
int height,
|
|
221
|
+
int y_pitch,
|
|
222
|
+
int u_pitch,
|
|
223
|
+
int v_pitch,
|
|
224
|
+
int rgb_pitch,
|
|
225
|
+
const float color_matrix[3][4],
|
|
226
|
+
cudaStream_t stream) {
|
|
227
|
+
const auto& cm = *reinterpret_cast<const ColorMatrix*>(color_matrix);
|
|
228
|
+
|
|
229
|
+
dim3 block(32, 8);
|
|
230
|
+
dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y);
|
|
231
|
+
|
|
232
|
+
yuv444_to_rgb_kernel<uint8_t><<<grid, block, 0, stream>>>(
|
|
233
|
+
y_plane,
|
|
234
|
+
u_plane,
|
|
235
|
+
v_plane,
|
|
236
|
+
rgb_output,
|
|
237
|
+
width,
|
|
238
|
+
height,
|
|
239
|
+
y_pitch,
|
|
240
|
+
u_pitch,
|
|
241
|
+
v_pitch,
|
|
242
|
+
rgb_pitch,
|
|
243
|
+
0, // bit_shift
|
|
244
|
+
cm);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
void launch_yuv444_to_rgb16_kernel(
|
|
248
|
+
const uint16_t* y_plane,
|
|
249
|
+
const uint16_t* u_plane,
|
|
250
|
+
const uint16_t* v_plane,
|
|
251
|
+
uint16_t* rgb_output,
|
|
252
|
+
int width,
|
|
253
|
+
int height,
|
|
254
|
+
int y_pitch,
|
|
255
|
+
int u_pitch,
|
|
256
|
+
int v_pitch,
|
|
257
|
+
int rgb_pitch,
|
|
258
|
+
int bit_shift,
|
|
259
|
+
const float color_matrix[3][4],
|
|
260
|
+
cudaStream_t stream) {
|
|
261
|
+
const auto& cm = *reinterpret_cast<const ColorMatrix*>(color_matrix);
|
|
262
|
+
|
|
263
|
+
constexpr int sample_size = static_cast<int>(sizeof(uint16_t));
|
|
264
|
+
dim3 block(32, 8);
|
|
265
|
+
dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y);
|
|
266
|
+
|
|
267
|
+
yuv444_to_rgb_kernel<uint16_t><<<grid, block, 0, stream>>>(
|
|
268
|
+
y_plane,
|
|
269
|
+
u_plane,
|
|
270
|
+
v_plane,
|
|
271
|
+
rgb_output,
|
|
272
|
+
width,
|
|
273
|
+
height,
|
|
274
|
+
y_pitch / sample_size,
|
|
275
|
+
u_pitch / sample_size,
|
|
276
|
+
v_pitch / sample_size,
|
|
277
|
+
rgb_pitch / sample_size,
|
|
278
|
+
bit_shift,
|
|
279
|
+
cm);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// RGB -> NV12 kernel for encoding.
|
|
283
|
+
// Each thread processes a 2x2 block: computes Y for all 4 pixels,
|
|
284
|
+
// averages U and V across the block, and writes to the NV12 planes.
|
|
285
|
+
__global__ void rgb_to_nv12_kernel(
|
|
286
|
+
const uint8_t* __restrict__ rgb_input,
|
|
287
|
+
uint8_t* __restrict__ y_plane,
|
|
288
|
+
uint8_t* __restrict__ uv_plane,
|
|
289
|
+
int width,
|
|
290
|
+
int height,
|
|
291
|
+
int rgb_pitch_elements,
|
|
292
|
+
int y_pitch_elements,
|
|
293
|
+
int uv_pitch_elements,
|
|
294
|
+
const ColorMatrix cm) {
|
|
295
|
+
int x = (blockIdx.x * blockDim.x + threadIdx.x) * 2;
|
|
296
|
+
int y = (blockIdx.y * blockDim.y + threadIdx.y) * 2;
|
|
297
|
+
if (x + 1 >= width || y + 1 >= height) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Read 4 RGB pixels from 2x2 block
|
|
302
|
+
const uint8_t* row0 = rgb_input + y * rgb_pitch_elements + x * 3;
|
|
303
|
+
const uint8_t* row1 = rgb_input + (y + 1) * rgb_pitch_elements + x * 3;
|
|
304
|
+
|
|
305
|
+
float r00 = row0[0], g00 = row0[1], b00 = row0[2];
|
|
306
|
+
float r01 = row0[3], g01 = row0[4], b01 = row0[5];
|
|
307
|
+
float r10 = row1[0], g10 = row1[1], b10 = row1[2];
|
|
308
|
+
float r11 = row1[3], g11 = row1[4], b11 = row1[5];
|
|
309
|
+
|
|
310
|
+
// Compute Y for all 4 pixels
|
|
311
|
+
float y00 =
|
|
312
|
+
cm.m[0][0] * r00 + cm.m[0][1] * g00 + cm.m[0][2] * b00 + cm.m[0][3];
|
|
313
|
+
float y01 =
|
|
314
|
+
cm.m[0][0] * r01 + cm.m[0][1] * g01 + cm.m[0][2] * b01 + cm.m[0][3];
|
|
315
|
+
float y10 =
|
|
316
|
+
cm.m[0][0] * r10 + cm.m[0][1] * g10 + cm.m[0][2] * b10 + cm.m[0][3];
|
|
317
|
+
float y11 =
|
|
318
|
+
cm.m[0][0] * r11 + cm.m[0][1] * g11 + cm.m[0][2] * b11 + cm.m[0][3];
|
|
319
|
+
|
|
320
|
+
// Write Y plane
|
|
321
|
+
uchar2 y_row0 = {
|
|
322
|
+
static_cast<uint8_t>(fminf(fmaxf(roundf(y00), 0.0f), 255.0f)),
|
|
323
|
+
static_cast<uint8_t>(fminf(fmaxf(roundf(y01), 0.0f), 255.0f))};
|
|
324
|
+
*(reinterpret_cast<uchar2*>(&y_plane[y * y_pitch_elements + x])) = y_row0;
|
|
325
|
+
|
|
326
|
+
uchar2 y_row1 = {
|
|
327
|
+
static_cast<uint8_t>(fminf(fmaxf(roundf(y10), 0.0f), 255.0f)),
|
|
328
|
+
static_cast<uint8_t>(fminf(fmaxf(roundf(y11), 0.0f), 255.0f))};
|
|
329
|
+
*(reinterpret_cast<uchar2*>(&y_plane[(y + 1) * y_pitch_elements + x])) =
|
|
330
|
+
y_row1;
|
|
331
|
+
|
|
332
|
+
// Compute U,V for all 4 pixels and average for 4:2:0 subsampling
|
|
333
|
+
float u00 =
|
|
334
|
+
cm.m[1][0] * r00 + cm.m[1][1] * g00 + cm.m[1][2] * b00 + cm.m[1][3];
|
|
335
|
+
float u01 =
|
|
336
|
+
cm.m[1][0] * r01 + cm.m[1][1] * g01 + cm.m[1][2] * b01 + cm.m[1][3];
|
|
337
|
+
float u10 =
|
|
338
|
+
cm.m[1][0] * r10 + cm.m[1][1] * g10 + cm.m[1][2] * b10 + cm.m[1][3];
|
|
339
|
+
float u11 =
|
|
340
|
+
cm.m[1][0] * r11 + cm.m[1][1] * g11 + cm.m[1][2] * b11 + cm.m[1][3];
|
|
341
|
+
|
|
342
|
+
float v00 =
|
|
343
|
+
cm.m[2][0] * r00 + cm.m[2][1] * g00 + cm.m[2][2] * b00 + cm.m[2][3];
|
|
344
|
+
float v01 =
|
|
345
|
+
cm.m[2][0] * r01 + cm.m[2][1] * g01 + cm.m[2][2] * b01 + cm.m[2][3];
|
|
346
|
+
float v10 =
|
|
347
|
+
cm.m[2][0] * r10 + cm.m[2][1] * g10 + cm.m[2][2] * b10 + cm.m[2][3];
|
|
348
|
+
float v11 =
|
|
349
|
+
cm.m[2][0] * r11 + cm.m[2][1] * g11 + cm.m[2][2] * b11 + cm.m[2][3];
|
|
350
|
+
|
|
351
|
+
float u_avg = (u00 + u01 + u10 + u11) * 0.25f;
|
|
352
|
+
float v_avg = (v00 + v01 + v10 + v11) * 0.25f;
|
|
353
|
+
|
|
354
|
+
int uv_idx = (y / 2) * uv_pitch_elements + x;
|
|
355
|
+
uchar2 uv_pair = {
|
|
356
|
+
static_cast<uint8_t>(fminf(fmaxf(roundf(u_avg), 0.0f), 255.0f)),
|
|
357
|
+
static_cast<uint8_t>(fminf(fmaxf(roundf(v_avg), 0.0f), 255.0f))};
|
|
358
|
+
*(reinterpret_cast<uchar2*>(&uv_plane[uv_idx])) = uv_pair;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
void launch_rgb_to_nv12_kernel(
|
|
362
|
+
const uint8_t* rgb_input,
|
|
363
|
+
uint8_t* y_plane,
|
|
364
|
+
uint8_t* uv_plane,
|
|
365
|
+
int width,
|
|
366
|
+
int height,
|
|
367
|
+
int rgb_pitch,
|
|
368
|
+
int y_pitch,
|
|
369
|
+
int uv_pitch,
|
|
370
|
+
const float color_matrix[3][4],
|
|
371
|
+
cudaStream_t stream) {
|
|
372
|
+
const auto& cm = *reinterpret_cast<const ColorMatrix*>(color_matrix);
|
|
373
|
+
|
|
374
|
+
dim3 block(32, 2);
|
|
375
|
+
dim3 grid(
|
|
376
|
+
(width / 2 + block.x - 1) / block.x,
|
|
377
|
+
(height / 2 + block.y - 1) / block.y);
|
|
378
|
+
|
|
379
|
+
rgb_to_nv12_kernel<<<grid, block, 0, stream>>>(
|
|
380
|
+
rgb_input,
|
|
381
|
+
y_plane,
|
|
382
|
+
uv_plane,
|
|
383
|
+
width,
|
|
384
|
+
height,
|
|
385
|
+
rgb_pitch,
|
|
386
|
+
y_pitch,
|
|
387
|
+
uv_pitch,
|
|
388
|
+
cm);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
} // namespace facebook::torchcodec
|