Skip to content

Commit 7953714

Browse files
committed
Merge branch 'main' of github.com:pytorch/torchcodec
2 parents 4bae5aa + 4560f0e commit 7953714

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,10 @@ int VideoDecoder::convertFrameToBufferUsingSwsScale(
13391339

13401340
int expectedOutputHeight = outputTensor.sizes()[0];
13411341
int expectedOutputWidth = outputTensor.sizes()[1];
1342-
if (activeStream.swsContext.get() == nullptr) {
1342+
auto curFrameSwsContextKey = SwsContextKey{
1343+
frame->width, frame->height, frameFormat, expectedOutputWidth, expectedOutputHeight};
1344+
if (activeStream.swsContext.get() == nullptr ||
1345+
activeStream.swsContextKey != curFrameSwsContextKey) {
13431346
SwsContext* swsContext = sws_getContext(
13441347
frame->width,
13451348
frame->height,
@@ -1373,6 +1376,7 @@ int VideoDecoder::convertFrameToBufferUsingSwsScale(
13731376
brightness,
13741377
contrast,
13751378
saturation);
1379+
activeStream.swsContextKey = curFrameSwsContextKey;
13761380
activeStream.swsContext.reset(swsContext);
13771381
}
13781382
SwsContext* swsContext = activeStream.swsContext.get();

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ class VideoDecoder {
317317
AVFilterContext* sourceContext = nullptr;
318318
AVFilterContext* sinkContext = nullptr;
319319
};
320+
struct SwsContextKey {
321+
int decodedWidth;
322+
int decodedHeight;
323+
AVPixelFormat decodedFormat;
324+
int outputWidth;
325+
int outputHeight;
326+
bool operator==(const SwsContextKey&) const = default;
327+
bool operator!=(const SwsContextKey&) const = default;
328+
};
320329
// Stores information for each stream.
321330
struct StreamInfo {
322331
int streamIndex = -1;
@@ -337,6 +346,7 @@ class VideoDecoder {
337346
ColorConversionLibrary colorConversionLibrary = FILTERGRAPH;
338347
std::vector<FrameInfo> keyFrames;
339348
std::vector<FrameInfo> allFrames;
349+
SwsContextKey swsContextKey;
340350
UniqueSwsContext swsContext;
341351
};
342352
VideoDecoder();

0 commit comments

Comments
 (0)