Skip to content

Commit 497f68b

Browse files
committed
Better comment; move around seek to string
1 parent c6e07ae commit 497f68b

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,16 +1704,4 @@ FrameDims getHeightAndWidthFromOptionsOrAVFrame(
17041704
videoStreamOptions.width.value_or(avFrame->width));
17051705
}
17061706

1707-
SingleStreamDecoder::SeekMode seekModeFromString(std::string_view seekMode) {
1708-
if (seekMode == "exact") {
1709-
return SingleStreamDecoder::SeekMode::exact;
1710-
} else if (seekMode == "approximate") {
1711-
return SingleStreamDecoder::SeekMode::approximate;
1712-
} else if (seekMode == "custom_frame_mappings") {
1713-
return SingleStreamDecoder::SeekMode::custom_frame_mappings;
1714-
} else {
1715-
TORCH_CHECK(false, "Invalid seek mode: " + std::string(seekMode));
1716-
}
1717-
}
1718-
17191707
} // namespace facebook::torchcodec

src/torchcodec/_core/SingleStreamDecoder.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,4 @@ std::ostream& operator<<(
375375
std::ostream& os,
376376
const SingleStreamDecoder::DecodeStats& stats);
377377

378-
SingleStreamDecoder::SeekMode seekModeFromString(std::string_view seekMode);
379-
380378
} // namespace facebook::torchcodec

src/torchcodec/_core/custom_ops.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ std::string mapToJson(const std::map<std::string, std::string>& metadataMap) {
169169
return ss.str();
170170
}
171171

172+
SingleStreamDecoder::SeekMode seekModeFromString(std::string_view seekMode) {
173+
if (seekMode == "exact") {
174+
return SingleStreamDecoder::SeekMode::exact;
175+
} else if (seekMode == "approximate") {
176+
return SingleStreamDecoder::SeekMode::approximate;
177+
} else if (seekMode == "custom_frame_mappings") {
178+
return SingleStreamDecoder::SeekMode::custom_frame_mappings;
179+
} else {
180+
TORCH_CHECK(false, "Invalid seek mode: " + std::string(seekMode));
181+
}
182+
}
183+
172184
} // namespace
173185

174186
// ==============================

src/torchcodec/_core/pybind_ops.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ namespace py = pybind11;
1414

1515
namespace facebook::torchcodec {
1616

17-
// In principle, this should be able to return a tensor. But when we try that,
18-
// we run into the bug reported here:
17+
// Note: It's not immediately obvous why we need both custom_ops.cpp and
18+
// pybind_ops.cpp. We do all other Python to C++ bridging in
19+
// custom_ops.cpp, and that even depends on pybind11, so why have an
20+
// explicit pybind-only file?
1921
//
20-
// https://github.com/pytorch/pytorch/issues/136664
22+
// The reason is that we want to accept OWNERSHIP of a file-like object
23+
// from the Python side. In order to do that, we need a proper
24+
// py::object. For raw bytes, we can launder that through a tensor on the
25+
// custom_ops.cpp side, but we can't launder a proper Python object
26+
// through a tensor. Custom ops can't accept a proper Python object
27+
// through py::object, so we have to do direct pybind11 here.
2128
//
22-
// So we instead launder the pointer through an int, and then use a conversion
23-
// function on the custom ops side to launder that int into a tensor.
29+
// TODO: Investigate if we can do something better here. See:
30+
// https://github.com/pytorch/torchcodec/issues/896
31+
// Short version is that we're laundering a pointer through an int, the
32+
// Python side forwards that to decoder creation functions in
33+
// custom_ops.cpp and we do another cast on that side to get a pointer
34+
// again. We want to investigate if we can do something cleaner by
35+
// defining proper pybind objects.
2436
int64_t create_file_like_context(py::object file_like, bool is_for_writing) {
2537
AVIOFileLikeContext* context =
2638
new AVIOFileLikeContext(file_like, is_for_writing);

0 commit comments

Comments
 (0)