Skip to content

Commit 375cfdf

Browse files
eclipseoNicolasHug
andauthored
Add compatibility with FFMPEG 7.0 (#8408)
Co-authored-by: Nicolas Hug <[email protected]> Co-authored-by: Nicolas Hug <[email protected]>
1 parent a839642 commit 375cfdf

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

torchvision/csrc/io/decoder/audio_sampler.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ bool AudioSampler::init(const SamplerParameters& params) {
4848
return false;
4949
}
5050

51+
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
52+
SwrContext* swrContext_ = NULL;
53+
AVChannelLayout channel_out;
54+
AVChannelLayout channel_in;
55+
av_channel_layout_default(&channel_out, params.out.audio.channels);
56+
av_channel_layout_default(&channel_in, params.in.audio.channels);
57+
int ret = swr_alloc_set_opts2(
58+
&swrContext_,
59+
&channel_out,
60+
(AVSampleFormat)params.out.audio.format,
61+
params.out.audio.samples,
62+
&channel_in,
63+
(AVSampleFormat)params.in.audio.format,
64+
params.in.audio.samples,
65+
0,
66+
logCtx_);
67+
#else
5168
swrContext_ = swr_alloc_set_opts(
5269
nullptr,
5370
av_get_default_channel_layout(params.out.audio.channels),
@@ -58,6 +75,7 @@ bool AudioSampler::init(const SamplerParameters& params) {
5875
params.in.audio.samples,
5976
0,
6077
logCtx_);
78+
#endif
6179
if (swrContext_ == nullptr) {
6280
LOG(ERROR) << "Cannot allocate SwrContext";
6381
return false;

torchvision/csrc/io/decoder/audio_stream.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,36 @@
66
namespace ffmpeg {
77

88
namespace {
9+
static int get_nb_channels(const AVFrame* frame, const AVCodecContext* codec) {
10+
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
11+
return frame ? frame->ch_layout.nb_channels : codec->ch_layout.nb_channels;
12+
#else
13+
return frame ? frame->channels : codec->channels;
14+
#endif
15+
}
16+
917
bool operator==(const AudioFormat& x, const AVFrame& y) {
1018
return x.samples == static_cast<size_t>(y.sample_rate) &&
11-
x.channels == static_cast<size_t>(y.channels) && x.format == y.format;
19+
x.channels == static_cast<size_t>(get_nb_channels(&y, nullptr)) &&
20+
x.format == y.format;
1221
}
1322

1423
bool operator==(const AudioFormat& x, const AVCodecContext& y) {
1524
return x.samples == static_cast<size_t>(y.sample_rate) &&
16-
x.channels == static_cast<size_t>(y.channels) && x.format == y.sample_fmt;
25+
x.channels == static_cast<size_t>(get_nb_channels(nullptr, &y)) &&
26+
x.format == y.sample_fmt;
1727
}
1828

1929
AudioFormat& toAudioFormat(AudioFormat& x, const AVFrame& y) {
2030
x.samples = y.sample_rate;
21-
x.channels = y.channels;
31+
x.channels = get_nb_channels(&y, nullptr);
2232
x.format = y.format;
2333
return x;
2434
}
2535

2636
AudioFormat& toAudioFormat(AudioFormat& x, const AVCodecContext& y) {
2737
x.samples = y.sample_rate;
28-
x.channels = y.channels;
38+
x.channels = get_nb_channels(nullptr, &y);
2939
x.format = y.sample_fmt;
3040
return x;
3141
}
@@ -54,9 +64,15 @@ int AudioStream::initFormat() {
5464
if (format_.format.audio.samples == 0) {
5565
format_.format.audio.samples = codecCtx_->sample_rate;
5666
}
67+
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
68+
if (format_.format.audio.channels == 0) {
69+
format_.format.audio.channels = codecCtx_->ch_layout.nb_channels;
70+
}
71+
#else
5772
if (format_.format.audio.channels == 0) {
5873
format_.format.audio.channels = codecCtx_->channels;
5974
}
75+
#endif
6076
if (format_.format.audio.format == AV_SAMPLE_FMT_NONE) {
6177
format_.format.audio.format = codecCtx_->sample_fmt;
6278
}

0 commit comments

Comments
 (0)