Skip to content

Commit e4f05ce

Browse files
committed
Enforce that encode() cannot be called twice
1 parent 99d21db commit e4f05ce

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,12 @@ torch::Tensor AudioEncoder::encodeToTensor() {
208208
}
209209

210210
void AudioEncoder::encode() {
211-
// TODO-ENCODING: Need to check, but consecutive calls to encode() are
212-
// probably invalid. We can address this once we (re)design the public and
213-
// private encoding APIs.
211+
// To be on the safe side we enforce that encode() can only be called once on
212+
// an encoder object. Whether this is actually necessary is unknown, so this
213+
// may be relaxed if needed.
214+
TORCH_CHECK(!encodeWasCalled_, "Cannot call encode() twice.");
215+
encodeWasCalled_ = true;
216+
214217
UniqueAVFrame avFrame(av_frame_alloc());
215218
TORCH_CHECK(avFrame != nullptr, "Couldn't allocate AVFrame.");
216219
// Default to 256 like in torchaudio

src/torchcodec/_core/Encoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@ class AudioEncoder {
4949

5050
// Stores the AVIOContext for the output tensor buffer.
5151
std::unique_ptr<AVIOToTensorContext> avioContextHolder_;
52+
53+
bool encodeWasCalled_ = false;
5254
};
5355
} // namespace facebook::torchcodec

0 commit comments

Comments
 (0)