Skip to content

Commit 7e6667c

Browse files
committed
Address comments
1 parent edbb5e7 commit 7e6667c

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def run(self):
6868
super().run()
6969

7070
def build_extension(self, ext):
71-
"""Call our CMake build system to build libtorchcodec?.so"""
71+
"""Call our CMake build system to build libtorchcodec*.so"""
7272
# Setuptools was designed to build one extension (.so file) at a time,
7373
# calling this method for each Extension object. We're using a
7474
# CMake-based build where all our extensions are built together at once.

src/torchcodec/decoders/_core/AVIOFileLikeContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ AVIOFileLikeContext::AVIOFileLikeContext(py::object fileLike)
2929
int AVIOFileLikeContext::read(void* opaque, uint8_t* buf, int buf_size) {
3030
auto fileLike = static_cast<UniquePyObject*>(opaque);
3131

32+
// Note that we acquire the GIL outside of the loop. This is likely more
33+
// efficient than releasing and acquiring it each loop iteration.
34+
py::gil_scoped_acquire gil;
3235
int num_read = 0;
3336
while (num_read < buf_size) {
3437
int request = buf_size - num_read;
35-
// TODO: It is maybe more efficient to grab the lock once in the
36-
// surrounding scope?
37-
py::gil_scoped_acquire gil;
3838
auto chunk = static_cast<std::string>(
3939
static_cast<py::bytes>((*fileLike)->attr("read")(request)));
4040
int chunk_len = static_cast<int>(chunk.length());

src/torchcodec/decoders/_core/AVIOFileLikeContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class AVIOFileLikeContext : public AVIOContextHolder {
3333
// and that's, at least, hard. For all of the common pitfalls, see:
3434
//
3535
// https://pybind11.readthedocs.io/en/stable/advanced/misc.html#common-sources-of-global-interpreter-lock-errors
36+
//
37+
// We maintain a reference to the file-like object because the file-like
38+
// object that was created on the Python side must live as long as our
39+
// potential use. That is, even if there are no more references to the object
40+
// on the Python side, we require that the object is still live.
3641
struct PyObjectDeleter {
3742
inline void operator()(py::object* obj) const {
3843
if (obj) {

src/torchcodec/decoders/_core/CMakeLists.txt

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ find_package(Python3 ${PYTHON_VERSION} EXACT COMPONENTS Development)
1010
function(make_torchcodec_sublibrary
1111
library_name
1212
sources
13-
dependent_libraries
14-
ffmpeg_include_dirs)
13+
library_dependencies)
1514

1615
add_library(${library_name} SHARED ${sources})
1716
set_target_properties(${library_name} PROPERTIES CXX_STANDARD 17)
@@ -20,7 +19,6 @@ function(make_torchcodec_sublibrary
2019
./../../../../
2120
"${TORCH_INSTALL_PREFIX}/include"
2221
${Python3_INCLUDE_DIRS}
23-
${ffmpeg_include_dirs}
2422
)
2523

2624
# Avoid adding the "lib" prefix which we already add explicitly.
@@ -29,14 +27,15 @@ function(make_torchcodec_sublibrary
2927
target_link_libraries(
3028
${library_name}
3129
PUBLIC
32-
${dependent_libraries}
30+
${library_dependencies}
3331
)
3432
endfunction()
3533

3634
function(make_torchcodec_libraries
3735
ffmpeg_major_version
38-
ffmpeg_target
39-
ffmpeg_include_dirs)
36+
ffmpeg_target)
37+
38+
# TODO: List each library and its purpose.
4039

4140
# Create libtorchcodec_decoderN.so
4241
set(decoder_library_name "libtorchcodec_decoder${ffmpeg_major_version}")
@@ -52,14 +51,14 @@ function(make_torchcodec_libraries
5251
list(APPEND decoder_sources CPUOnlyDevice.cpp)
5352
endif()
5453

55-
set(decoder_dependent_libraries
54+
set(decoder_library_dependencies
5655
${ffmpeg_target}
5756
${TORCH_LIBRARIES}
5857
${Python3_LIBRARIES}
5958
)
6059

6160
if(ENABLE_CUDA)
62-
list(APPEND decoder_dependent_libraries
61+
list(APPEND decoder_library_dependencies
6362
${CUDA_nppi_LIBRARY}
6463
${CUDA_nppicc_LIBRARY}
6564
)
@@ -68,8 +67,7 @@ function(make_torchcodec_libraries
6867
make_torchcodec_sublibrary(
6968
"${decoder_library_name}"
7069
"${decoder_sources}"
71-
"${decoder_dependent_libraries}"
72-
"${ffmpeg_include_dirs}"
70+
"${decoder_library_dependencies}"
7371
)
7472

7573
# Create libtorchcodec_custom_opsN.so
@@ -82,7 +80,6 @@ function(make_torchcodec_libraries
8280
"${custom_ops_library_name}"
8381
"${custom_ops_sources}"
8482
"${decoder_library_name}"
85-
"${ffmpeg_include_dirs}"
8683
)
8784

8885
# Create libtorchcodec_pybind_opsN.so
@@ -95,7 +92,6 @@ function(make_torchcodec_libraries
9592
"${pybind_ops_library_name}"
9693
"${pybind_ops_sources}"
9794
"${decoder_library_name}"
98-
"${ffmpeg_include_dirs}"
9995
)
10096
# pybind11 quirk, see:
10197
# https://pybind11.readthedocs.io/en/stable/faq.html#someclass-declared-with-greater-visibility-than-the-type-of-its-field-someclass-member-wattributes
@@ -137,10 +133,10 @@ if(DEFINED ENV{BUILD_AGAINST_ALL_FFMPEG_FROM_S3})
137133
${CMAKE_CURRENT_SOURCE_DIR}/fetch_and_expose_non_gpl_ffmpeg_libs.cmake
138134
)
139135

140-
make_torchcodec_libraries(7 ffmpeg7 $ffmpeg7_INCLUDE_DIRs)
141-
make_torchcodec_libraries(6 ffmpeg6 $ffmpeg6_INCLUDE_DIRS)
142-
make_torchcodec_libraries(4 ffmpeg4 $ffmpeg4_INCLUDE_DIRS)
143-
make_torchcodec_libraries(5 ffmpeg5 $ffmpeg5_INCLUDE_DIRS)
136+
make_torchcodec_libraries(7 ffmpeg7)
137+
make_torchcodec_libraries(6 ffmpeg6)
138+
make_torchcodec_libraries(4 ffmpeg4)
139+
make_torchcodec_libraries(5 ffmpeg5)
144140
else()
145141
message(
146142
STATUS
@@ -180,7 +176,7 @@ else()
180176
)
181177
endif()
182178

183-
make_torchcodec_libraries(${ffmpeg_major_version} PkgConfig::LIBAV ${LIBAV_INCLUDE_DIRS})
179+
make_torchcodec_libraries(${ffmpeg_major_version} PkgConfig::LIBAV)
184180

185181
# Expose these values updwards so that the test compilation does not need
186182
# to re-figure it out. FIXME: it's not great that we just copy-paste the

src/torchcodec/decoders/_core/PyBindOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ int64_t create_from_file_like(
3131
realSeek = seekModeFromString(seek_mode.value());
3232
}
3333

34-
auto contextHolder = std::make_unique<AVIOFileLikeContext>(file_like);
34+
auto avioContextHolder = std::make_unique<AVIOFileLikeContext>(file_like);
3535

36-
VideoDecoder* decoder = new VideoDecoder(std::move(contextHolder), realSeek);
36+
VideoDecoder* decoder = new VideoDecoder(std::move(avioContextHolder), realSeek);
3737
return reinterpret_cast<int64_t>(decoder);
3838
}
3939

src/torchcodec/decoders/_core/VideoDecoderOps.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ at::Tensor create_from_tensor(
129129
at::Tensor video_tensor,
130130
std::optional<std::string_view> seek_mode) {
131131
TORCH_CHECK(video_tensor.is_contiguous(), "video_tensor must be contiguous");
132+
TORCH_CHECK(video_tensor.scalar_type() == torch::kUInt8, "video_tensor must be kUInt8");
132133
void* data = video_tensor.mutable_data_ptr();
133134
size_t length = video_tensor.numel();
134135

src/torchcodec/decoders/_core/ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
_pybind_ops: Optional[ModuleType] = None
2222

2323

24-
def load_torchcodec_extension():
24+
def load_torchcodec_shared_libraries():
2525
# Successively try to load libtorchcodec7.so, libtorchcodec6.so,
2626
# libtorchcodec5.so, and libtorchcodec4.so. Each of these correspond to an
2727
# ffmpeg major version. This should cover all potential ffmpeg versions
@@ -79,7 +79,7 @@ def load_torchcodec_extension():
7979
)
8080

8181

82-
load_torchcodec_extension()
82+
load_torchcodec_shared_libraries()
8383

8484

8585
# Note: We use disallow_in_graph because PyTorch does constant propagation of

test/decoders/test_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def test_create_decoder(self, create_from, device):
363363
decoder = create_from_file_like(open(path, mode="rb", buffering=0), "exact")
364364
elif create_from == "file_like_bufferedio":
365365
decoder = create_from_file_like(
366-
open(path, mode="rb", buffering=-4096), "exact"
366+
open(path, mode="rb", buffering=4096), "exact"
367367
)
368368
else:
369369
raise ValueError("Oops, double check the parametrization of this test!")

0 commit comments

Comments
 (0)