Skip to content

Commit 5285193

Browse files
authored
PR #14060 from OhadMeir: Cherry Pick PR #14015 from OhadMeir: Add UYVY to YUYV conversion for D457
2 parents 1f166d6 + f3f8591 commit 5285193

File tree

7 files changed

+72
-7
lines changed

7 files changed

+72
-7
lines changed

src/cuda/cuda-conversion.cu

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,34 @@ __global__ void kernel_unpack_yuy2_bgra8_cuda(const uint8_t * src, uint8_t *dst,
223223
}
224224
}
225225

226+
__global__ void kernel_uyvy_to_yuyv_cuda(uint16_t* src, uint16_t* dst, int count)
227+
{
228+
int i = blockDim.x * blockIdx.x + threadIdx.x;
229+
230+
if (i >= count)
231+
return;
232+
233+
dst[i] = ( ( src[i] >> 8 ) & 0x00FF ) | ( ( src[i] << 8 ) & 0xFF00 );
234+
}
235+
236+
void rscuda::uyvy_to_yuyv_cuda_helper(const uint16_t* src, uint16_t* dst, int count)
237+
{
238+
int numBlocks = count / RS2_CUDA_THREADS_PER_BLOCK;
239+
auto d_src = alloc_dev<uint16_t>(count);
240+
auto d_dst = alloc_dev<uint16_t>(count);
241+
242+
auto result = cudaMemcpy(d_src.get(), src, count * sizeof(uint16_t), cudaMemcpyHostToDevice);
243+
assert(result == cudaSuccess);
244+
245+
kernel_uyvy_to_yuyv_cuda <<<numBlocks, RS2_CUDA_THREADS_PER_BLOCK >>>(d_src.get(), d_dst.get(), count);
246+
cudaStreamSynchronize(0);
247+
248+
result = cudaGetLastError();
249+
assert(result == cudaSuccess);
250+
251+
result = cudaMemcpy(dst, d_dst.get(), count * sizeof(uint16_t), cudaMemcpyDeviceToHost);
252+
assert(result == cudaSuccess);
253+
}
226254

227255
void rscuda::unpack_yuy2_cuda_helper(const uint8_t* h_src, uint8_t* h_dst, int n, rs2_format format)
228256
{

src/cuda/cuda-conversion.cuh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace rscuda
3333
template<class SOURCE>
3434
void y16_y16_from_y12i_10_cuda_helper(uint8_t* const dest[], int count, const SOURCE * source);
3535
void unpack_yuy2_cuda_helper(const uint8_t* src, uint8_t* dst, int n, rs2_format format);
36+
void uyvy_to_yuyv_cuda_helper(const uint16_t* src, uint16_t* dst, int n);
3637

3738
template<rs2_format FORMAT> void unpack_yuy2_cuda(uint8_t * const d[], const uint8_t * s, int n)
3839
{

src/device.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,25 @@ void device::register_stream_to_extrinsic_group(const stream_interface& stream,
194194
}
195195
}
196196

197-
std::vector<rs2_format> device::map_supported_color_formats(rs2_format source_format)
197+
std::vector< rs2_format > device::map_supported_color_formats( rs2_format source_format, bool should_map_source_format )
198198
{
199199
// Mapping from source color format to all of the compatible target color formats.
200200

201201
std::vector<rs2_format> target_formats = { RS2_FORMAT_RGB8, RS2_FORMAT_RGBA8, RS2_FORMAT_BGR8, RS2_FORMAT_BGRA8 };
202202
switch (source_format)
203203
{
204204
case RS2_FORMAT_YUYV:
205-
target_formats.push_back(RS2_FORMAT_YUYV);
206205
target_formats.push_back(RS2_FORMAT_Y8);
207206
break;
208207
case RS2_FORMAT_UYVY:
209-
target_formats.push_back(RS2_FORMAT_UYVY);
210208
break;
211209
default:
212210
LOG_ERROR("Format is not supported for mapping");
213211
}
212+
213+
if( should_map_source_format )
214+
target_formats.push_back( source_format );
215+
214216
return target_formats;
215217
}
216218

src/device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class device
9494
int add_sensor(const std::shared_ptr<sensor_interface>& sensor_base);
9595
int assign_sensor(const std::shared_ptr<sensor_interface>& sensor_base, uint8_t idx);
9696
void register_stream_to_extrinsic_group(const stream_interface& stream, uint32_t groupd_index);
97-
std::vector<rs2_format> map_supported_color_formats(rs2_format source_format);
97+
std::vector<rs2_format> map_supported_color_formats(rs2_format source_format, bool should_map_source_format = true);
9898

9999
explicit device( std::shared_ptr< const device_info > const &, bool device_changed_notifications = true );
100100

src/ds/d400/d400-color.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,14 @@ namespace librealsense
255255
auto uvc_dev = raw_color_ep->get_uvc_device();
256256
if (uvc_dev->is_platform_jetson())
257257
{
258-
// Work-around for discrepancy between the RGB YUYV descriptor and the parser . Use UYUV parser instead
259-
color_ep.register_processing_block(processing_block_factory::create_pbf_vector<uyvy_converter>(RS2_FORMAT_YUYV, map_supported_color_formats(RS2_FORMAT_YUYV), RS2_STREAM_COLOR));
258+
// Work-around for discrepancy between the RGB YUYV descriptor and the parser. Use UYUV parser instead.
259+
// Bytes are reveiced swapped, so YUYV format is received as UYVY.
260+
color_ep.register_processing_block( processing_block_factory::create_pbf_vector< uyvy_converter >( RS2_FORMAT_YUYV,
261+
map_supported_color_formats( RS2_FORMAT_YUYV, false ),
262+
RS2_STREAM_COLOR ) );
263+
color_ep.register_processing_block( { { RS2_FORMAT_YUYV } },
264+
{ { RS2_FORMAT_YUYV, RS2_STREAM_COLOR } },
265+
[]() { return std::make_shared< uyvy_to_yuyv >(); } );
260266
}
261267
else
262268
{

src/proc/color-formats-converter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,4 +1140,19 @@ namespace librealsense
11401140
{
11411141
unpack_m420(_target_format, _target_stream, dest, source, width, height, actual_size);
11421142
}
1143+
1144+
void uyvy_to_yuyv::process_function( uint8_t * const dest[], const uint8_t * source, int width, int height, int actual_size, int input_size)
1145+
{
1146+
auto in = reinterpret_cast< const uint16_t * >( source );
1147+
auto out = reinterpret_cast< uint16_t * >( dest[0] );
1148+
1149+
// Time measurments on Jetson yielded better results for the non-CUDA version
1150+
//#ifdef RS2_USE_CUDA
1151+
//if( rsutils::rs2_is_gpu_available() )
1152+
// rscuda::uyvy_to_yuyv_cuda_helper( in, out, width * height );
1153+
//#else
1154+
for( size_t i = 0; i < width * height; ++i )
1155+
out[i] = ( ( in[i] >> 8 ) & 0x00FF ) | ( ( in[i] << 8 ) & 0xFF00 );
1156+
//#endif
1157+
}
11431158
}

src/proc/color-formats-converter.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,17 @@ namespace librealsense
7373
color_converter(name, target_format) {};
7474
void process_function( uint8_t * const dest[], const uint8_t * source, int width, int height, int actual_size, int input_size) override;
7575
};
76-
}
76+
77+
class LRS_EXTENSION_API uyvy_to_yuyv : public color_converter
78+
{
79+
public:
80+
uyvy_to_yuyv() : uyvy_to_yuyv( "UYVY to YUYV Converter" ) {};
81+
82+
protected:
83+
uyvy_to_yuyv( const char * name ) : color_converter( name, RS2_FORMAT_YUYV, RS2_STREAM_COLOR )
84+
{
85+
};
86+
87+
void process_function( uint8_t * const dest[], const uint8_t * source, int width, int height, int actual_size, int input_size ) override;
88+
};
89+
}

0 commit comments

Comments
 (0)