Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions src/torchcodec/_core/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,30 @@ AVSampleFormat findBestOutputSampleFormat(const AVCodec& avCodec) {
return supportedSampleFormats[0];
}

} // namespace

AudioEncoder::~AudioEncoder() {
close_avio();
}
void closeAVIOContext(
AVFormatContext* avFormatContext,
AVIOContextHolder* avioContextHolder) {
if (!avFormatContext || !avFormatContext->pb) {
return;
}

void AudioEncoder::close_avio() {
if (avFormatContext_ && avFormatContext_->pb) {
if (avFormatContext_->pb->error == 0) {
avio_flush(avFormatContext_->pb);
}
if (avFormatContext->pb->error == 0) {
avio_flush(avFormatContext->pb);
}

if (!avioContextHolder_) {
if (avFormatContext_->pb->error == 0) {
avio_close(avFormatContext_->pb);
}
// avoids closing again in destructor, which would segfault.
avFormatContext_->pb = nullptr;
if (!avioContextHolder) {
if (avFormatContext->pb->error == 0) {
avio_close(avFormatContext->pb);
}
}

avFormatContext->pb = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check my understanding that the main logic difference is that we now set avFormatContext->pb = nullptr; after the if (!avioContextHolder) { ...} block, and not inside it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly, after and outside of the condition.

}

} // namespace

AudioEncoder::~AudioEncoder() {
closeAVIOContext(avFormatContext_.get(), avioContextHolder_.get());
}

AudioEncoder::AudioEncoder(
Expand Down Expand Up @@ -336,7 +340,7 @@ void AudioEncoder::encode() {
"Error in: av_write_trailer",
getFFMPEGErrorStringFromErrorCode(status));

close_avio();
closeAVIOContext(avFormatContext_.get(), avioContextHolder_.get());
}

UniqueAVFrame AudioEncoder::maybeConvertAVFrame(const UniqueAVFrame& avFrame) {
Expand Down Expand Up @@ -646,18 +650,7 @@ void sortCodecOptions(
} // namespace

VideoEncoder::~VideoEncoder() {
// TODO-VideoEncoder: Unify destructor with ~AudioEncoder()
if (avFormatContext_ && avFormatContext_->pb) {
if (avFormatContext_->pb->error == 0) {
avio_flush(avFormatContext_->pb);
}
if (!avioContextHolder_) {
if (avFormatContext_->pb->error == 0) {
avio_close(avFormatContext_->pb);
}
avFormatContext_->pb = nullptr;
}
}
closeAVIOContext(avFormatContext_.get(), avioContextHolder_.get());
}

VideoEncoder::VideoEncoder(
Expand Down
1 change: 0 additions & 1 deletion src/torchcodec/_core/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class AudioEncoder {
void encodeFrame(AutoAVPacket& autoAVPacket, const UniqueAVFrame& avFrame);
void maybeFlushSwrBuffers(AutoAVPacket& autoAVPacket);
void flushBuffers();
void close_avio();

UniqueEncodingAVFormatContext avFormatContext_;
UniqueAVCodecContext avCodecContext_;
Expand Down
3 changes: 0 additions & 3 deletions test/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,6 @@ def encode_to_tensor(samples):
encoded_from_contiguous, encoded_from_non_contiguous, rtol=0, atol=0
)

@pytest.mark.skip(
reason="Flaky test, see https://github.com/pytorch/torchcodec/issues/724"
)
@pytest.mark.parametrize("num_channels_input", (1, 2))
@pytest.mark.parametrize("num_channels_output", (1, 2, None))
@pytest.mark.parametrize("method", ("to_file", "to_tensor", "to_file_like"))
Expand Down
Loading