1010namespace facebook ::torchcodec {
1111
1212AVIOFileLikeContext::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
4929int 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-
10669int64_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
0 commit comments