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
6 changes: 5 additions & 1 deletion src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,10 @@ int VideoDecoder::convertFrameToBufferUsingSwsScale(

int expectedOutputHeight = outputTensor.sizes()[0];
int expectedOutputWidth = outputTensor.sizes()[1];
if (activeStream.swsContext.get() == nullptr) {
auto curFrameSwsContextKey = SwsContextKey{
frame->width, frame->height, frameFormat, expectedOutputWidth, expectedOutputHeight};
if (activeStream.swsContext.get() == nullptr ||
activeStream.swsContextKey != curFrameSwsContextKey) {
SwsContext* swsContext = sws_getContext(
frame->width,
frame->height,
Expand Down Expand Up @@ -1373,6 +1376,7 @@ int VideoDecoder::convertFrameToBufferUsingSwsScale(
brightness,
contrast,
saturation);
activeStream.swsContextKey = curFrameSwsContextKey;
activeStream.swsContext.reset(swsContext);
}
SwsContext* swsContext = activeStream.swsContext.get();
Expand Down
10 changes: 10 additions & 0 deletions src/torchcodec/decoders/_core/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ class VideoDecoder {
AVFilterContext* sourceContext = nullptr;
AVFilterContext* sinkContext = nullptr;
};
struct SwsContextKey {
int decodedWidth;
int decodedHeight;
AVPixelFormat decodedFormat;
int outputWidth;
int outputHeight;
bool operator==(const SwsContextKey&) const = default;
bool operator!=(const SwsContextKey&) const = default;
};
// Stores information for each stream.
struct StreamInfo {
int streamIndex = -1;
Expand All @@ -337,6 +346,7 @@ class VideoDecoder {
ColorConversionLibrary colorConversionLibrary = FILTERGRAPH;
std::vector<FrameInfo> keyFrames;
std::vector<FrameInfo> allFrames;
SwsContextKey swsContextKey;
UniqueSwsContext swsContext;
};
VideoDecoder();
Expand Down