Skip to content

Commit 46e614c

Browse files
Wes Castrofacebook-github-bot
authored andcommitted
[Torchvision] Fix FFmpeg 7.1+ support (#9231)
Summary: * Fix `SwrContext` variable shadowing leading to null pointer dereference * Fix hang when using AVI inputs with `MemoryBuffer` reader (`SyncDecoder::TestMemoryBuffer` test) Differential Revision: D82494990
1 parent 58eb039 commit 46e614c

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

torchvision/csrc/io/decoder/audio_sampler.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ bool AudioSampler::init(const SamplerParameters& params) {
4949
}
5050

5151
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
52-
SwrContext* swrContext_ = NULL;
5352
AVChannelLayout channel_out;
5453
AVChannelLayout channel_in;
5554
av_channel_layout_default(&channel_out, params.out.audio.channels);
5655
av_channel_layout_default(&channel_in, params.in.audio.channels);
57-
swr_alloc_set_opts2(
56+
int ret = swr_alloc_set_opts2(
5857
&swrContext_,
5958
&channel_out,
6059
(AVSampleFormat)params.out.audio.format,
@@ -64,6 +63,10 @@ bool AudioSampler::init(const SamplerParameters& params) {
6463
params.in.audio.samples,
6564
0,
6665
logCtx_);
66+
if (ret < 0 || swrContext_ == nullptr) {
67+
LOG(ERROR) << "Cannot allocate SwrContext";
68+
return false;
69+
}
6770
#else
6871
swrContext_ = swr_alloc_set_opts(
6972
nullptr,
@@ -75,11 +78,11 @@ bool AudioSampler::init(const SamplerParameters& params) {
7578
params.in.audio.samples,
7679
0,
7780
logCtx_);
78-
#endif
7981
if (swrContext_ == nullptr) {
8082
LOG(ERROR) << "Cannot allocate SwrContext";
8183
return false;
8284
}
85+
#endif
8386

8487
int result;
8588
if ((result = swr_init(swrContext_)) < 0) {

torchvision/csrc/io/decoder/memory_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int MemoryBuffer::read(uint8_t* buf, int size) {
1414
return available;
1515
}
1616

17-
return 0;
17+
return AVERROR_EOF;
1818
}
1919

2020
int64_t MemoryBuffer::seek(int64_t offset, int whence) {

0 commit comments

Comments
 (0)