Skip to content

Commit 7a13ad0

Browse files
authored
[Torchvision] Fix FFmpeg 7.1+ support
Differential Revision: D82494990 Pull Request resolved: #9231
1 parent 58eb039 commit 7a13ad0

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-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/decoder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ int Decoder::readFunction(void* opaque, uint8_t* buf, int size) {
154154
if (decoder == nullptr) {
155155
return 0;
156156
}
157-
return decoder->readCallback(buf, size);
157+
int bytesRead = decoder->readCallback(buf, size);
158+
return bytesRead == 0 ? AVERROR_EOF : bytesRead;
158159
}
159160

160161
/* static */

0 commit comments

Comments
 (0)