@@ -203,6 +203,18 @@ VideoDecoder::BatchDecodedOutput::BatchDecodedOutput(
203203 frames = allocateEmptyHWCTensor (height, width, options.device , numFrames);
204204}
205205
206+ bool VideoDecoder::SwsContextKey::operator ==(
207+ const VideoDecoder::SwsContextKey& other) {
208+ return decodedWidth == other.decodedWidth && decodedHeight == decodedHeight &&
209+ decodedFormat == other.decodedFormat &&
210+ outputWidth == other.outputWidth && outputHeight == other.outputHeight ;
211+ }
212+
213+ bool VideoDecoder::SwsContextKey::operator !=(
214+ const VideoDecoder::SwsContextKey& other) {
215+ return !(*this == other);
216+ }
217+
206218VideoDecoder::VideoDecoder () {}
207219
208220void VideoDecoder::initializeDecoder () {
@@ -1339,7 +1351,14 @@ int VideoDecoder::convertFrameToBufferUsingSwsScale(
13391351
13401352 int expectedOutputHeight = outputTensor.sizes ()[0 ];
13411353 int expectedOutputWidth = outputTensor.sizes ()[1 ];
1342- if (activeStream.swsContext .get () == nullptr ) {
1354+ auto curFrameSwsContextKey = SwsContextKey{
1355+ frame->width ,
1356+ frame->height ,
1357+ frameFormat,
1358+ expectedOutputWidth,
1359+ expectedOutputHeight};
1360+ if (activeStream.swsContext .get () == nullptr ||
1361+ activeStream.swsContextKey != curFrameSwsContextKey) {
13431362 SwsContext* swsContext = sws_getContext (
13441363 frame->width ,
13451364 frame->height ,
@@ -1373,6 +1392,7 @@ int VideoDecoder::convertFrameToBufferUsingSwsScale(
13731392 brightness,
13741393 contrast,
13751394 saturation);
1395+ activeStream.swsContextKey = curFrameSwsContextKey;
13761396 activeStream.swsContext .reset (swsContext);
13771397 }
13781398 SwsContext* swsContext = activeStream.swsContext .get ();
0 commit comments