Skip to content

Commit 0906fb3

Browse files
committed
Properly free AVFormatContext and streams
1 parent b110dac commit 0906fb3

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/torchcodec/decoders/_core/FFMPEGCommon.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ struct Deleter {
4949
};
5050

5151
// Unique pointers for FFMPEG structures.
52-
using UniqueAVFormatContext = std::unique_ptr<
52+
using UniqueAVFormatContextForDecoding = std::unique_ptr<
5353
AVFormatContext,
5454
Deleterp<AVFormatContext, void, avformat_close_input>>;
55+
using UniqueAVFormatContextForEncoding = std::unique_ptr<
56+
AVFormatContext,
57+
Deleter<AVFormatContext, void, avformat_free_context>>;
5558
using UniqueAVCodecContext = std::unique_ptr<
5659
AVCodecContext,
5760
Deleterp<AVCodecContext, void, avcodec_free_context>>;

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int64_t secondsToClosestPts(double seconds, const AVRational& timeBase) {
4141
}
4242

4343
struct AVInput {
44-
UniqueAVFormatContext formatContext;
44+
UniqueAVFormatContextForDecoding formatContext;
4545
std::unique_ptr<AVIOBytesContext> ioBytesContext;
4646
};
4747

@@ -1695,10 +1695,7 @@ FrameDims getHeightAndWidthFromOptionsOrAVFrame(
16951695
videoStreamOptions.width.value_or(avFrame.width));
16961696
}
16971697

1698-
Encoder::~Encoder() {
1699-
// TODO NEED TO CALL THIS
1700-
// avformat_free_context(avFormatContext_.get());
1701-
}
1698+
Encoder::~Encoder() {}
17021699

17031700
Encoder::Encoder(int sampleRate, std::string_view fileName)
17041701
: sampleRate_(sampleRate) {
@@ -1756,6 +1753,9 @@ Encoder::Encoder(int sampleRate, std::string_view fileName)
17561753
avCodecContext_->frame_size,
17571754
". Cannot encode. This should probably never happen?");
17581755

1756+
// We're allocating the stream here. Streams are meant to be freed by
1757+
// avformat_free_context(avFormatContext), which we call in the
1758+
// avFormatContext_'s destructor.
17591759
avStream_ = avformat_new_stream(avFormatContext_.get(), NULL);
17601760
TORCH_CHECK(avStream_ != nullptr, "Couldn't create new stream.");
17611761
avcodec_parameters_from_context(avStream_->codecpar, avCodecContext_.get());
@@ -1829,10 +1829,10 @@ torch::Tensor Encoder::encode(const torch::Tensor& wf) {
18291829
avFrame->pts += avFrame->nb_samples;
18301830
numEncodedSamples += numSamplesToEncode;
18311831
}
1832-
encode_inner_loop(autoAVPacket, nullptr); // flush
1833-
18341832
TORCH_CHECK(numEncodedSamples == numSamples, "Hmmmmmm something went wrong.");
18351833

1834+
encode_inner_loop(autoAVPacket, nullptr); // flush
1835+
18361836
ffmpegRet = av_write_trailer(avFormatContext_.get());
18371837
TORCH_CHECK(
18381838
ffmpegRet == AVSUCCESS,

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ class VideoDecoder {
453453

454454
SeekMode seekMode_;
455455
ContainerMetadata containerMetadata_;
456-
UniqueAVFormatContext formatContext_;
456+
UniqueAVFormatContextForDecoding formatContext_;
457457
std::map<int, StreamInfo> streamInfos_;
458458
const int NO_ACTIVE_STREAM = -2;
459459
int activeStreamIndex_ = NO_ACTIVE_STREAM;
@@ -559,7 +559,7 @@ class Encoder {
559559
private:
560560
void encode_inner_loop(AutoAVPacket& autoAVPacket, AVFrame* avFrame);
561561

562-
UniqueAVFormatContext avFormatContext_;
562+
UniqueAVFormatContextForEncoding avFormatContext_;
563563
UniqueAVCodecContext avCodecContext_;
564564
AVStream* avStream_;
565565

0 commit comments

Comments
 (0)