Skip to content

Commit c6e07ae

Browse files
committed
Fix for Windows
1 parent fa32ece commit c6e07ae

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/torchcodec/_core/AVIOFileLikeContext.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,27 @@
1111

1212
#include "src/torchcodec/_core/AVIOContextHolder.h"
1313

14+
// pybind11 has specific visibility rules; see:
15+
// https://pybind11.readthedocs.io/en/stable/faq.html#someclass-declared-with-greater-visibility-than-the-type-of-its-field-someclass-member-wattributes
16+
// This file is included in both pybind_ops.cpp and custom_ops.cpp. We compile
17+
// pybind_ops.cpp with the global visibility flag as required. On the
18+
// custom_ops.cpp side we don't need to do that. However, we do need to ensure
19+
// that this class is also seen as the same visibility.
20+
//
21+
// This only matters on Linux and Mac; on Windows we don't need to do anything.
22+
#ifdef _WIN32
23+
#define VISIBILITY_HIDDEN
24+
#else
25+
#define VISIBILITY_HIDDEN __attribute__((visibility("hidden")))
26+
#endif
27+
1428
namespace py = pybind11;
1529

1630
namespace facebook::torchcodec {
1731

1832
// Enables uers to pass in a Python file-like object. We then forward all read
1933
// and seek calls back up to the methods on the Python object.
20-
class __attribute__((visibility("hidden"))) AVIOFileLikeContext
21-
: public AVIOContextHolder {
34+
class VISIBILITY_HIDDEN AVIOFileLikeContext : public AVIOContextHolder {
2235
public:
2336
explicit AVIOFileLikeContext(const py::object& fileLike, bool isForWriting);
2437

src/torchcodec/_core/custom_ops.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ at::Tensor create_from_tensor(
207207
realSeek = seekModeFromString(seek_mode.value());
208208
}
209209

210-
auto contextHolder = std::make_unique<AVIOFromTensorContext>(video_tensor);
210+
auto avioContextHolder =
211+
std::make_unique<AVIOFromTensorContext>(video_tensor);
211212

212213
std::unique_ptr<SingleStreamDecoder> uniqueDecoder =
213-
std::make_unique<SingleStreamDecoder>(std::move(contextHolder), realSeek);
214+
std::make_unique<SingleStreamDecoder>(
215+
std::move(avioContextHolder), realSeek);
214216
return wrapDecoderPointerToTensor(std::move(uniqueDecoder));
215217
}
216218

@@ -219,16 +221,18 @@ at::Tensor _create_from_file_like(
219221
std::optional<std::string_view> seek_mode) {
220222
auto fileLikeContext =
221223
reinterpret_cast<AVIOFileLikeContext*>(file_like_context);
222-
TORCH_CHECK(fileLikeContext != nullptr, "file_like must be a valid pointer");
223-
std::unique_ptr<AVIOFileLikeContext> contextHolder(fileLikeContext);
224+
TORCH_CHECK(
225+
fileLikeContext != nullptr, "file_like_context must be a valid pointer");
226+
std::unique_ptr<AVIOFileLikeContext> avioContextHolder(fileLikeContext);
224227

225228
SingleStreamDecoder::SeekMode realSeek = SingleStreamDecoder::SeekMode::exact;
226229
if (seek_mode.has_value()) {
227230
realSeek = seekModeFromString(seek_mode.value());
228231
}
229232

230233
std::unique_ptr<SingleStreamDecoder> uniqueDecoder =
231-
std::make_unique<SingleStreamDecoder>(std::move(contextHolder), realSeek);
234+
std::make_unique<SingleStreamDecoder>(
235+
std::move(avioContextHolder), realSeek);
232236
return wrapDecoderPointerToTensor(std::move(uniqueDecoder));
233237
}
234238

0 commit comments

Comments
 (0)