-
Notifications
You must be signed in to change notification settings - Fork 64
Transforms bridge between Python and C++ #948
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,9 +43,9 @@ TORCH_LIBRARY(torchcodec_ns, m) { | |
m.def( | ||
"_create_from_file_like(int file_like_context, str? seek_mode=None) -> Tensor"); | ||
m.def( | ||
"_add_video_stream(Tensor(a!) decoder, *, int? width=None, int? height=None, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"default\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None, str? color_conversion_library=None) -> ()"); | ||
"_add_video_stream(Tensor(a!) decoder, *, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"default\", str transform_specs=\"\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None, str? color_conversion_library=None) -> ()"); | ||
m.def( | ||
"add_video_stream(Tensor(a!) decoder, *, int? width=None, int? height=None, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"default\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None) -> ()"); | ||
"add_video_stream(Tensor(a!) decoder, *, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"default\", str transform_specs=\"\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None) -> ()"); | ||
m.def( | ||
"add_audio_stream(Tensor(a!) decoder, *, int? stream_index=None, int? sample_rate=None, int? num_channels=None) -> ()"); | ||
m.def("seek_to_pts(Tensor(a!) decoder, float seconds) -> ()"); | ||
|
@@ -183,6 +183,69 @@ SingleStreamDecoder::SeekMode seekModeFromString(std::string_view seekMode) { | |
} | ||
} | ||
|
||
int checkedToPositiveInt(const std::string& str) { | ||
int ret = 0; | ||
try { | ||
ret = std::stoi(str); | ||
} catch (const std::invalid_argument&) { | ||
TORCH_CHECK(false, "String cannot be converted to an int:" + str); | ||
} catch (const std::out_of_range&) { | ||
TORCH_CHECK(false, "String would become integer out of range:" + str); | ||
} | ||
TORCH_CHECK(ret > 0, "String must be a positive integer:" + str); | ||
return ret; | ||
} | ||
|
||
// Resize transform specs take the form: | ||
// | ||
// "resize, <height>, <width>" | ||
// | ||
// Where "resize" is the string literal and <height> and <width> are positive | ||
// integers. | ||
Transform* makeResizeTransform( | ||
const std::vector<std::string>& resizeTransformSpec) { | ||
TORCH_CHECK( | ||
resizeTransformSpec.size() == 3, | ||
"resizeTransformSpec must have 3 elements including its name"); | ||
int height = checkedToPositiveInt(resizeTransformSpec[1]); | ||
int width = checkedToPositiveInt(resizeTransformSpec[2]); | ||
return new ResizeTransform(FrameDims(height, width)); | ||
} | ||
|
||
std::vector<std::string> split(const std::string& str, char delimiter) { | ||
std::vector<std::string> tokens; | ||
std::string token; | ||
std::istringstream tokenStream(str); | ||
while (std::getline(tokenStream, token, delimiter)) { | ||
tokens.push_back(token); | ||
} | ||
return tokens; | ||
} | ||
|
||
// The transformSpecsRaw string is always in the format: | ||
// | ||
// "name1, param1, param2, ...; name2, param1, param2, ...; ..." | ||
// | ||
// Where "nameX" is the name of the transform, and "paramX" are the parameters. | ||
std::vector<Transform*> makeTransforms(const std::string& transformSpecsRaw) { | ||
std::vector<Transform*> transforms; | ||
std::vector<std::string> transformSpecs = split(transformSpecsRaw, ';'); | ||
for (const std::string& transformSpecRaw : transformSpecs) { | ||
std::vector<std::string> transformSpec = split(transformSpecRaw, ','); | ||
TORCH_CHECK( | ||
transformSpec.size() >= 1, | ||
"Invalid transform spec: " + transformSpecRaw); | ||
|
||
auto name = transformSpec[0]; | ||
if (name == "resize") { | ||
transforms.push_back(makeResizeTransform(transformSpec)); | ||
} else { | ||
TORCH_CHECK(false, "Invalid transform name: " + name); | ||
} | ||
} | ||
return transforms; | ||
} | ||
|
||
} // namespace | ||
|
||
// ============================== | ||
|
@@ -252,36 +315,18 @@ at::Tensor _create_from_file_like( | |
|
||
void _add_video_stream( | ||
at::Tensor& decoder, | ||
std::optional<int64_t> width = std::nullopt, | ||
std::optional<int64_t> height = std::nullopt, | ||
std::optional<int64_t> num_threads = std::nullopt, | ||
std::optional<std::string_view> dimension_order = std::nullopt, | ||
std::optional<int64_t> stream_index = std::nullopt, | ||
std::string_view device = "cpu", | ||
std::string_view device_variant = "default", | ||
std::string_view transform_specs = "", | ||
std::optional<std::tuple<at::Tensor, at::Tensor, at::Tensor>> | ||
custom_frame_mappings = std::nullopt, | ||
std::optional<std::string_view> color_conversion_library = std::nullopt) { | ||
VideoStreamOptions videoStreamOptions; | ||
videoStreamOptions.ffmpegThreadCount = num_threads; | ||
|
||
// TODO: Eliminate this temporary bridge code. This exists because we have | ||
// not yet exposed the transforms API on the Python side. We also want | ||
// to remove the `width` and `height` arguments from the Python API. | ||
// | ||
// TEMPORARY BRIDGE CODE START | ||
TORCH_CHECK( | ||
width.has_value() == height.has_value(), | ||
"width and height must both be set or unset."); | ||
std::vector<Transform*> transforms; | ||
if (width.has_value()) { | ||
transforms.push_back( | ||
new ResizeTransform(FrameDims(height.value(), width.value()))); | ||
width.reset(); | ||
height.reset(); | ||
} | ||
// TEMPORARY BRIDGE CODE END | ||
|
||
if (dimension_order.has_value()) { | ||
std::string stdDimensionOrder{dimension_order.value()}; | ||
TORCH_CHECK(stdDimensionOrder == "NHWC" || stdDimensionOrder == "NCHW"); | ||
|
@@ -309,6 +354,9 @@ void _add_video_stream( | |
videoStreamOptions.device = torch::Device(std::string(device)); | ||
videoStreamOptions.deviceVariant = device_variant; | ||
|
||
std::vector<Transform*> transforms = | ||
makeTransforms(std::string(transform_specs)); | ||
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. An example of how using a default empty spec make things simpler than using an optional: we always call this function. If we have an empty spec, we just get back an empty vector. |
||
|
||
std::optional<SingleStreamDecoder::FrameMappings> converted_mappings = | ||
custom_frame_mappings.has_value() | ||
? std::make_optional(makeFrameMappings(custom_frame_mappings.value())) | ||
|
@@ -324,24 +372,22 @@ void _add_video_stream( | |
// Add a new video stream at `stream_index` using the provided options. | ||
void add_video_stream( | ||
at::Tensor& decoder, | ||
std::optional<int64_t> width = std::nullopt, | ||
std::optional<int64_t> height = std::nullopt, | ||
std::optional<int64_t> num_threads = std::nullopt, | ||
std::optional<std::string_view> dimension_order = std::nullopt, | ||
std::optional<int64_t> stream_index = std::nullopt, | ||
std::string_view device = "cpu", | ||
std::string_view device_variant = "default", | ||
std::string_view transform_specs = "", | ||
const std::optional<std::tuple<at::Tensor, at::Tensor, at::Tensor>>& | ||
custom_frame_mappings = std::nullopt) { | ||
_add_video_stream( | ||
decoder, | ||
width, | ||
height, | ||
num_threads, | ||
dimension_order, | ||
stream_index, | ||
device, | ||
device_variant, | ||
transform_specs, | ||
custom_frame_mappings); | ||
} | ||
|
||
|
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.
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.
Note that we're using an empty spec,
""
, as the default rather than making it optional. I find this makes the code simpler and easier to reason about.