Skip to content

Commit d301f53

Browse files
committed
Better comment for AVIOContextHolder.
1 parent 43d6dde commit d301f53

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/torchcodec/decoders/_core/FFMPEGCommon.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,28 @@ bool canSwsScaleHandleUnalignedData();
148148
using AVIOReadFunction = int (*)(void*, uint8_t*, int);
149149
using AVIOSeekFunction = int64_t (*)(void*, int64_t, int);
150150

151-
// TODO: explain purpose of context holder
151+
// The AVIOContextHolder serves several purposes:
152+
//
153+
// 1. It is a smart pointer for the AVIOContext. It has the logic to create
154+
// a new AVIOContext and will appropriately free the AVIOContext when it
155+
// goes out of scope. Note that this requires more than just the having a
156+
// UniqueAVIOContext, as the AVIOContext points to a buffer which must be
157+
// freed.
158+
// 2. It is a base class for AVIOContext specializations. When specializing a
159+
// AVIOContext, we need to provide four things:
160+
// 1. A read callback function.
161+
// 2. A seek callback function.
162+
// 3. A write callback function. (Not supported yet; it's for encoding.)
163+
// 4. A pointer to some context object that has the same lifetime as the
164+
// AVIOContext itself. This context object holds the custom state that
165+
// tracks the custom behavior of reading, seeking and writing. It is
166+
// provided upon AVIOContext creation and to the read, seek and
167+
// write callback functions.
168+
// While it's not required, it is natural for the derived classes to make
169+
// all of the above members. Base classes need to call
170+
// createAVIOContext(), ideally in there constructor.
171+
// 3. A generic handle for those that just need to manage having access to an
172+
// AVIOContext, but aren't necessarily concerned with how it was customized.
152173
class AVIOContextHolder {
153174
public:
154175
virtual ~AVIOContextHolder();
@@ -165,7 +186,7 @@ class AVIOContextHolder {
165186
UniqueAVIOContext avioContext_;
166187

167188
// Defaults to 64 KB
168-
static const int defaultBufferSize = 64 * 1014;
189+
static const int defaultBufferSize = 64 * 1024;
169190
};
170191

171192
} // namespace facebook::torchcodec

src/torchcodec/decoders/_core/PyBindOps.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class AVIOFileLikeContext : public AVIOContextHolder {
5353
int num_read = 0;
5454
while (num_read < buf_size) {
5555
int request = buf_size - num_read;
56+
// TODO: It is maybe more efficient to grab the lock once in the
57+
// surrounding scope?
5658
py::gil_scoped_acquire gil;
5759
auto chunk = static_cast<std::string>(
5860
static_cast<py::bytes>((*fileLike)->attr("read")(request)));
@@ -85,11 +87,11 @@ class AVIOFileLikeContext : public AVIOContextHolder {
8587
}
8688

8789
private:
88-
// Note that we keep a pointer to the Python object because we need to
90+
// Note that we dynamically allocate the Python object because we need to
8991
// strictly control when its destructor is called. We must hold the GIL
9092
// when its destructor gets called, as it needs to update the reference
91-
// count. It's easiest to control that when it's a pointer. Otherwise, we'd
92-
// have to ensure whatever enclosing scope holds the object has the GIL,
93+
// count. It's easiest to control that when it's dynamic memory. Otherwise,
94+
// we'd have to ensure whatever enclosing scope holds the object has the GIL,
9395
// and that's, at least, hard. For all of the common pitfalls, see:
9496
//
9597
// https://pybind11.readthedocs.io/en/stable/advanced/misc.html#common-sources-of-global-interpreter-lock-errors

test/decoders/VideoDecoderTest.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ class VideoDecoderTest : public testing::TestWithParam<bool> {
5151

5252
void* buffer = content_.data();
5353
size_t length = content_.length();
54-
constexpr int bufferSize = 64 * 1024;
55-
auto contextHolder =
56-
std::make_unique<AVIOBytesContext>(buffer, length, bufferSize);
57-
54+
auto contextHolder = std::make_unique<AVIOBytesContext>(buffer, length);
5855
return std::make_unique<VideoDecoder>(
5956
std::move(contextHolder), VideoDecoder::SeekMode::approximate);
6057
} else {

0 commit comments

Comments
 (0)