Skip to content

Commit 1c6fad8

Browse files
committed
WIP
1 parent 6e88ee6 commit 1c6fad8

File tree

4 files changed

+21
-59
lines changed

4 files changed

+21
-59
lines changed

src/torchcodec/_core/AVIOContextHolder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ void AVIOContextHolder::createAVIOContext(
2323
buffer != nullptr,
2424
"Failed to allocate buffer of size " + std::to_string(bufferSize));
2525

26-
TORCH_CHECK(
27-
(seek != nullptr) && ((write != nullptr) ^ (read != nullptr)),
28-
"seek method must be defined, and either write or read must be defined. "
29-
"But not both!")
26+
// TORCH_CHECK(
27+
// (seek != nullptr) && ((write != nullptr) ^ (read != nullptr)),
28+
// "seek method must be defined, and either write or read must be
29+
// defined. " "But not both!")
3030
avioContext_.reset(avioAllocContext(
3131
buffer,
3232
bufferSize,

src/torchcodec/_core/AVIOFileLikeContext.cpp

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,20 @@
1010
namespace facebook::torchcodec {
1111

1212
AVIOFileLikeContext::AVIOFileLikeContext(py::object fileLike)
13-
: AVIOFileLikeContext(fileLike, false) {}
14-
15-
std::unique_ptr<AVIOFileLikeContext> AVIOFileLikeContext::createForWriting(
16-
py::object fileLike) {
17-
return std::unique_ptr<AVIOFileLikeContext>(
18-
new AVIOFileLikeContext(fileLike, true));
19-
}
20-
21-
AVIOFileLikeContext::AVIOFileLikeContext(py::object fileLike, bool isWriteMode)
2213
: fileLike_{UniquePyObject(new py::object(fileLike))} {
2314
{
2415
// TODO: Is it necessary to acquire the GIL here? Is it maybe even
2516
// harmful? At the moment, this is only called from within a pybind
2617
// function, and pybind guarantees we have the GIL.
2718
py::gil_scoped_acquire gil;
28-
if (isWriteMode) {
29-
TORCH_CHECK(
30-
py::hasattr(fileLike, "write"),
31-
"File like object must implement a write method.");
32-
} else {
33-
TORCH_CHECK(
34-
py::hasattr(fileLike, "read"),
35-
"File like object must implement a read method.");
36-
}
19+
TORCH_CHECK(
20+
py::hasattr(fileLike, "read"),
21+
"File like object must implement a read method.");
3722
TORCH_CHECK(
3823
py::hasattr(fileLike, "seek"),
3924
"File like object must implement a seek method.");
4025
}
41-
42-
if (isWriteMode) {
43-
createAVIOContext(nullptr, &write, &seek, &fileLike_);
44-
} else {
45-
createAVIOContext(&read, nullptr, &seek, &fileLike_);
46-
}
26+
createAVIOContext(&read, &write, &seek, &fileLike_);
4727
}
4828

4929
int AVIOFileLikeContext::read(void* opaque, uint8_t* buf, int buf_size) {
@@ -86,23 +66,6 @@ int AVIOFileLikeContext::read(void* opaque, uint8_t* buf, int buf_size) {
8666
return totalNumRead == 0 ? AVERROR_EOF : totalNumRead;
8767
}
8868

89-
int AVIOFileLikeContext::write(void* opaque, const uint8_t* buf, int buf_size) {
90-
auto fileLike = static_cast<UniquePyObject*>(opaque);
91-
92-
// Note that we acquire the GIL outside of the loop. This is likely more
93-
// efficient than releasing and acquiring it each loop iteration.
94-
py::gil_scoped_acquire gil;
95-
96-
// Create a bytes object from the buffer
97-
py::bytes data_bytes(reinterpret_cast<const char*>(buf), buf_size);
98-
99-
// Call the Python write method
100-
auto bytes_written = (*fileLike)->attr("write")(data_bytes);
101-
102-
// Python write() should return the number of bytes written
103-
return py::cast<int>(bytes_written);
104-
}
105-
10669
int64_t AVIOFileLikeContext::seek(void* opaque, int64_t offset, int whence) {
10770
// We do not know the file size.
10871
if (whence == AVSEEK_SIZE) {
@@ -114,4 +77,12 @@ int64_t AVIOFileLikeContext::seek(void* opaque, int64_t offset, int whence) {
11477
return py::cast<int64_t>((*fileLike)->attr("seek")(offset, whence));
11578
}
11679

80+
int AVIOFileLikeContext::write(void* opaque, const uint8_t* buf, int buf_size) {
81+
auto fileLike = static_cast<UniquePyObject*>(opaque);
82+
py::gil_scoped_acquire gil;
83+
py::bytes bytes_obj(reinterpret_cast<const char*>(buf), buf_size);
84+
85+
return py::cast<int64_t>((*fileLike)->attr("write")(bytes_obj));
86+
}
87+
11788
} // namespace facebook::torchcodec

src/torchcodec/_core/AVIOFileLikeContext.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,16 @@ namespace py = pybind11;
1515

1616
namespace facebook::torchcodec {
1717

18-
// Enables users to pass in a Python file-like object. We then forward all read,
19-
// write and seek calls back up to the methods on the Python object.
20-
class __attribute__((visibility("default"))) AVIOFileLikeContext
21-
: public AVIOContextHolder {
18+
// Enables uers to pass in a Python file-like object. We then forward all read
19+
// and seek calls back up to the methods on the Python object.
20+
class AVIOFileLikeContext : public AVIOContextHolder {
2221
public:
23-
// Constructor for reading from a file-like object
2422
explicit AVIOFileLikeContext(py::object fileLike);
2523

26-
// Constructor for writing to a file-like object
27-
static std::unique_ptr<AVIOFileLikeContext> createForWriting(
28-
py::object fileLike);
29-
3024
private:
31-
// Private constructor for write mode
32-
AVIOFileLikeContext(py::object fileLike, bool isWriteMode);
33-
3425
static int read(void* opaque, uint8_t* buf, int buf_size);
35-
static int write(void* opaque, const uint8_t* buf, int buf_size);
3626
static int64_t seek(void* opaque, int64_t offset, int whence);
27+
static int write(void* opaque, const uint8_t* buf, int buf_size);
3728

3829
// Note that we dynamically allocate the Python object because we need to
3930
// strictly control when its destructor is called. We must hold the GIL

src/torchcodec/_core/pybind_ops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void encode_audio_to_file_like(
6060
audioStreamOptions.bitRate = bit_rate;
6161
audioStreamOptions.numChannels = num_channels;
6262

63-
auto avioContextHolder = AVIOFileLikeContext::createForWriting(file_like);
63+
auto avioContextHolder = std::make_unique<AVIOFileLikeContext>(file_like);
6464

6565
AudioEncoder encoder(
6666
samples,

0 commit comments

Comments
 (0)