Skip to content

Commit 6b42199

Browse files
committed
Fix usage of libswscale. Avoid intermediate quantization in ycbcr->rgb video conversion.
Signed-off-by: Michal Zientkiewicz <[email protected]>
1 parent 8ccf26a commit 6b42199

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

dali/operators/video/color_space.cu

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,24 @@ __global__ static void VideoColorSpaceConversionKernel(
4747

4848
#pragma unroll
4949
for (int i = 0; i < 2; i++) {
50-
float cy = halfy + i * 0.5f + 0.25f;
50+
float cy = halfy + i * 0.5f + 0.5f;
5151
#pragma unroll
5252
for (int j = 0; j < 2; j++) {
53-
float cx = halfx + j * 0.5f + 0.25f;
54-
u8vec3 yuv_val;
53+
float cx = halfx + j * 0.5f + 0.5f;
54+
vec3 yuv_val;
5555
yuv_val[0] = Y.at(ivec2{x + j, y + i}, 0, kernels::BorderClamp());
5656

5757
UV(&yuv_val[1], vec2(cx, cy), kernels::BorderClamp());
5858

59-
u8vec3 out_val;
59+
yuv_val *= 1.0f / 255.0f;
60+
61+
vec3 out_val;
6062
switch (conversion_type) {
6163
case VIDEO_COLOR_SPACE_CONVERSION_TYPE_YUV_TO_RGB_FULL_RANGE:
62-
out_val = dali::kernels::color::jpeg::ycbcr_to_rgb<uint8_t>(yuv_val);
64+
out_val = dali::kernels::color::jpeg::ycbcr_to_rgb<float>(yuv_val);
6365
break;
6466
case VIDEO_COLOR_SPACE_CONVERSION_TYPE_YUV_TO_RGB:
65-
out_val = dali::kernels::color::itu_r_bt_601::ycbcr_to_rgb<uint8_t>(yuv_val);
67+
out_val = dali::kernels::color::itu_r_bt_601::ycbcr_to_rgb<float>(yuv_val);
6668
break;
6769
case VIDEO_COLOR_SPACE_CONVERSION_TYPE_YUV_UPSAMPLE:
6870
out_val = yuv_val;
@@ -71,10 +73,11 @@ __global__ static void VideoColorSpaceConversionKernel(
7173
assert(false);
7274
}
7375
if (normalized_range) {
74-
output({x + j, y + i, 0}) = ConvertNorm<Out>(out_val.x);
75-
output({x + j, y + i, 1}) = ConvertNorm<Out>(out_val.y);
76-
output({x + j, y + i, 2}) = ConvertNorm<Out>(out_val.z);
76+
output({x + j, y + i, 0}) = ConvertSatNorm<Out>(out_val.x);
77+
output({x + j, y + i, 1}) = ConvertSatNorm<Out>(out_val.y);
78+
output({x + j, y + i, 2}) = ConvertSatNorm<Out>(out_val.z);
7779
} else {
80+
out_val *= 255.0f;
7881
output({x + j, y + i, 0}) = ConvertSat<Out>(out_val.x);
7982
output({x + j, y + i, 1}) = ConvertSat<Out>(out_val.y);
8083
output({x + j, y + i, 2}) = ConvertSat<Out>(out_val.z);

dali/operators/video/frames_decoder_cpu.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void FramesDecoderCpu::CopyToOutput(uint8_t *data) {
9393
Width(),
9494
Height(),
9595
sws_output_format,
96-
SWS_BILINEAR,
96+
SWS_BILINEAR|SWS_FULL_CHR_H_INT|SWS_ACCURATE_RND,
9797
nullptr,
9898
nullptr,
9999
nullptr),

dali/test/python/test_video_reader.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,8 @@ def video_reader_pipeline():
139139
), f"Number of frames mismatch: {num_frames} != {sample_experimental.shape[0]}"
140140
if i == 0:
141141
for k in range(num_frames):
142-
if device == "cpu":
143-
additional_args = {"threshold": 0.06}
144-
else:
145-
additional_args = {}
146142
compare_frames(
147-
sample_experimental[k], sample_legacy[k], i, j, k, **additional_args
143+
sample_experimental[k], sample_legacy[k], i, j, k
148144
)
149145
else:
150146
np.testing.assert_array_equal(sample_legacy, sample_experimental)

0 commit comments

Comments
 (0)