-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Enable rotate frames for RGB camera #13733
Open
noacoohen
wants to merge
2
commits into
IntelRealSense:development
Choose a base branch
from
noacoohen:rotation_filter_for_color_frames
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−64
Open
Changes from all commits
Commits
Show all changes
2 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 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 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 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 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 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#include "stream.h" | ||
#include "core/video.h" | ||
#include "proc/rotation-filter.h" | ||
#include <rsutils/easylogging/easyloggingpp.h> | ||
|
||
namespace librealsense { | ||
|
||
|
@@ -16,8 +17,13 @@ namespace librealsense { | |
|
||
|
||
rotation_filter::rotation_filter() | ||
: rotation_filter( std::vector< rs2_stream >{ RS2_STREAM_DEPTH } ) | ||
{ | ||
} | ||
|
||
rotation_filter::rotation_filter( std::vector< rs2_stream > streams_to_rotate ) | ||
: stream_filter_processing_block( "Rotation Filter" ) | ||
, _streams_to_rotate() | ||
, _streams_to_rotate( streams_to_rotate ) | ||
, _control_val( rotation_default_val ) | ||
, _real_width( 0 ) | ||
, _real_height( 0 ) | ||
|
@@ -40,44 +46,25 @@ namespace librealsense { | |
if( ! strong_rotation_control ) | ||
return; | ||
|
||
std::lock_guard< std::mutex > lock( _mutex ); | ||
|
||
if( ! strong_rotation_control->is_valid( val ) ) | ||
throw invalid_value_exception( rsutils::string::from() | ||
<< "Unsupported rotation scale " << val << " is out of range." ); | ||
|
||
std::lock_guard< std::mutex > lock( _mutex ); | ||
_value = val; | ||
} ); | ||
|
||
register_option( RS2_OPTION_ROTATION, rotation_control ); | ||
register_option( RS2_OPTION_ROTATION, rotation_control ); | ||
} | ||
|
||
rs2::frame rotation_filter::process_frame(const rs2::frame_source& source, const rs2::frame& f) | ||
{ | ||
if( _value == rotation_default_val ) | ||
if( _value == rotation_default_val || _streams_to_rotate.empty() ) | ||
return f; | ||
|
||
rs2::stream_profile profile = f.get_profile(); | ||
rs2_stream type = profile.stream_type(); | ||
rs2_extension tgt_type; | ||
if( type == RS2_STREAM_INFRARED ) | ||
{ | ||
tgt_type = RS2_EXTENSION_VIDEO_FRAME; | ||
} | ||
else if( f.is< rs2::disparity_frame >() ) | ||
{ | ||
tgt_type = RS2_EXTENSION_DISPARITY_FRAME; | ||
} | ||
else if( f.is< rs2::depth_frame >() ) | ||
{ | ||
tgt_type = RS2_EXTENSION_DEPTH_FRAME; | ||
} | ||
else | ||
{ | ||
return f; | ||
} | ||
|
||
auto src = f.as< rs2::video_frame >(); | ||
rs2::stream_profile profile = f.get_profile(); | ||
auto format = profile.format(); | ||
_target_stream_profile = profile; | ||
|
||
if( _value == 90 || _value == -90 ) | ||
|
@@ -92,30 +79,35 @@ namespace librealsense { | |
} | ||
auto bpp = src.get_bytes_per_pixel(); | ||
update_output_profile( f ); | ||
rs2_stream type = profile.stream_type(); | ||
rs2_extension tgt_type; | ||
if( type == RS2_STREAM_COLOR || type == RS2_STREAM_INFRARED ) | ||
tgt_type = RS2_EXTENSION_VIDEO_FRAME; | ||
else | ||
tgt_type = f.is< rs2::disparity_frame >() ? RS2_EXTENSION_DISPARITY_FRAME : RS2_EXTENSION_DEPTH_FRAME; | ||
|
||
if (auto tgt = prepare_target_frame(f, source, tgt_type)) | ||
{ | ||
switch( bpp ) | ||
if( format == RS2_FORMAT_YUYV && ( _value == 90 || _value == -90 ) ) | ||
{ | ||
case 1: { | ||
rotate_frame< 1 >( static_cast< uint8_t * >( const_cast< void * >( tgt.get_data() ) ), | ||
static_cast< const uint8_t * >( src.get_data() ), | ||
src.get_width(), | ||
src.get_height() ); | ||
break; | ||
LOG_ERROR( "Rotating YUYV format is disabled for 90 or -90 degrees" ); | ||
return f; | ||
} | ||
|
||
case 2: { | ||
rotate_frame< 2 >( static_cast< uint8_t * >( const_cast< void * >( tgt.get_data() ) ), | ||
static_cast< const uint8_t * >( src.get_data() ) , | ||
if( format == RS2_FORMAT_YUYV ) | ||
{ | ||
rotate_YUYV_frame( static_cast< uint8_t * >( const_cast< void * >( tgt.get_data() ) ), | ||
static_cast< const uint8_t * >( src.get_data() ), | ||
src.get_width(), | ||
src.get_height()); | ||
break; | ||
src.get_height() ); | ||
} | ||
|
||
default: | ||
LOG_ERROR( "Rotation transform does not support format: " | ||
+ std::string( rs2_format_to_string( tgt.get_profile().format() ) ) ); | ||
else | ||
{ | ||
rotate_frame( static_cast< uint8_t * >( const_cast< void * >( tgt.get_data() ) ), | ||
static_cast< const uint8_t * >( src.get_data() ), | ||
src.get_width(), | ||
src.get_height(), | ||
bpp ); | ||
} | ||
return tgt; | ||
} | ||
|
@@ -180,8 +172,7 @@ namespace librealsense { | |
return ret; | ||
} | ||
|
||
template< size_t SIZE > | ||
void rotation_filter::rotate_frame( uint8_t * const out, const uint8_t * source, int width, int height ) | ||
void rotation_filter::rotate_frame( uint8_t * const out, const uint8_t * source, int width, int height, int bpp ) | ||
{ | ||
if( _value != 90 && _value != -90 && _value != 180 ) | ||
{ | ||
|
@@ -193,33 +184,64 @@ namespace librealsense { | |
int height_out = ( _value == 90 || _value == -90 ) ? width : height; // rotate by 180 will keep the values as is | ||
|
||
// Perform rotation | ||
for( int i = 0; i < height; ++i ) | ||
for( int i = 0; i < height; ++i ) | ||
{ | ||
for( int j = 0; j < width; ++j ) | ||
{ | ||
// Calculate source index | ||
size_t src_index = ( i * width + j ) * SIZE; | ||
size_t src_index = ( i * width + j ) * bpp; | ||
|
||
// Determine output index based on rotation angle | ||
size_t out_index; | ||
if( _value == 90 ) | ||
{ | ||
out_index = ( j * height + ( height - i - 1 ) ) * SIZE; | ||
out_index = ( j * height + ( height - i - 1 ) ) * bpp; | ||
} | ||
else if( _value == -90 ) | ||
{ | ||
out_index = ( ( width - j - 1 ) * height + i ) * SIZE; | ||
out_index = ( ( width - j - 1 ) * height + i ) * bpp; | ||
} | ||
else // 180 degrees | ||
{ | ||
out_index = ( ( height - i - 1 ) * width + ( width - j - 1 ) ) * SIZE; | ||
out_index = ( ( height - i - 1 ) * width + ( width - j - 1 ) ) * bpp; | ||
} | ||
|
||
// Copy pixel data from source to destination | ||
std::memcpy( &out[out_index], &source[src_index], SIZE ); | ||
std::memcpy( &out[out_index], &source[src_index], bpp ); | ||
} | ||
} | ||
} | ||
|
||
|
||
void rotation_filter::rotate_YUYV_frame( uint8_t * const out, const uint8_t * source, int width, int height ) | ||
{ | ||
int frame_size = width * height * 2; // YUYV has 2 bytes per pixel | ||
for( int i = 0; i < frame_size; i += 4 ) | ||
{ | ||
// Find the corresponding flipped position in the output | ||
int flipped_index = frame_size - 4 - i; | ||
|
||
// Copy YUYV pixels in reverse order to rotate 180 degrees | ||
out[flipped_index] = source[i]; // Y1 | ||
out[flipped_index + 1] = source[i + 1]; // U | ||
out[flipped_index + 2] = source[i + 2]; // Y2 | ||
out[flipped_index + 3] = source[i + 3]; // V | ||
} | ||
} | ||
|
||
|
||
bool rotation_filter::should_process( const rs2::frame & frame ) | ||
{ | ||
if( ! frame || frame.is< rs2::frameset >() ) | ||
return false; | ||
auto type = frame.get_profile().stream_type(); | ||
|
||
for( auto & stream_type : _streams_to_rotate ){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
if( stream_type == type ) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} | ||
|
This file contains 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code duplication. Is default constructor needed? If so have it call the other constructor with an empty vector
{}
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed