Skip to content

Commit dfa9fcc

Browse files
committed
Merge branch 'main' of https://github.com/pytorch/torchcodec into doc1
2 parents f29b05c + 170caad commit dfa9fcc

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

benchmarks/decoders/benchmark_decoders_library.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ def retrieve_videos(urls_and_dest_paths):
393393

394394

395395
def plot_data(df_data, plot_path):
396+
plt.rcParams["font.size"] = 18
397+
396398
# Creating the DataFrame
397399
df = pd.DataFrame(df_data)
398400

@@ -440,9 +442,7 @@ def plot_data(df_data, plot_path):
440442

441443
# Set the title for the subplot
442444
base_video = Path(video).name.removesuffix(".mp4")
443-
ax.set_title(
444-
f"video={base_video}\ndecode_pattern={vcount} x {vtype}", fontsize=10
445-
)
445+
ax.set_title(f"{base_video}\n{vcount} x {vtype}", fontsize=11)
446446

447447
# Plot bars with error bars
448448
ax.barh(
13.7 KB
Loading

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
206218
VideoDecoder::VideoDecoder() {}
207219

208220
void 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();

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&);
327+
bool operator!=(const SwsContextKey&);
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)