Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with FFMPEG 7.0 #8408

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions torchvision/csrc/io/decoder/audio_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ bool AudioSampler::init(const SamplerParameters& params) {
return false;
}

#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
SwrContext* swrContext_ = NULL;
AVChannelLayout channel_out;
AVChannelLayout channel_in;
av_channel_layout_default(&channel_out, params.out.audio.channels);
av_channel_layout_default(&channel_in, params.in.audio.channels);
int ret = swr_alloc_set_opts2(
&swrContext_,
&channel_out,
(AVSampleFormat)params.out.audio.format,
params.out.audio.samples,
&channel_in,
(AVSampleFormat)params.in.audio.format,
params.in.audio.samples,
0,
logCtx_);
#else
swrContext_ = swr_alloc_set_opts(
nullptr,
av_get_default_channel_layout(params.out.audio.channels),
Expand All @@ -58,6 +75,7 @@ bool AudioSampler::init(const SamplerParameters& params) {
params.in.audio.samples,
0,
logCtx_);
#endif
if (swrContext_ == nullptr) {
LOG(ERROR) << "Cannot allocate SwrContext";
return false;
Expand Down
24 changes: 20 additions & 4 deletions torchvision/csrc/io/decoder/audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@
namespace ffmpeg {

namespace {
static int get_nb_channels(const AVFrame* frame, const AVCodecContext* codec) {
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
return frame ? frame->ch_layout.nb_channels : codec->ch_layout.nb_channels;
#else
return frame ? frame->channels : codec->channels;
#endif
}

bool operator==(const AudioFormat& x, const AVFrame& y) {
return x.samples == static_cast<size_t>(y.sample_rate) &&
x.channels == static_cast<size_t>(y.channels) && x.format == y.format;
x.channels == static_cast<size_t>(get_nb_channels(&y, nullptr)) &&
x.format == y.format;
}

bool operator==(const AudioFormat& x, const AVCodecContext& y) {
return x.samples == static_cast<size_t>(y.sample_rate) &&
x.channels == static_cast<size_t>(y.channels) && x.format == y.sample_fmt;
x.channels == static_cast<size_t>(get_nb_channels(nullptr, &y)) &&
x.format == y.sample_fmt;
}

AudioFormat& toAudioFormat(AudioFormat& x, const AVFrame& y) {
x.samples = y.sample_rate;
x.channels = y.channels;
x.channels = get_nb_channels(&y, nullptr);
x.format = y.format;
return x;
}

AudioFormat& toAudioFormat(AudioFormat& x, const AVCodecContext& y) {
x.samples = y.sample_rate;
x.channels = y.channels;
x.channels = get_nb_channels(nullptr, &y);
x.format = y.sample_fmt;
return x;
}
Expand Down Expand Up @@ -54,9 +64,15 @@ int AudioStream::initFormat() {
if (format_.format.audio.samples == 0) {
format_.format.audio.samples = codecCtx_->sample_rate;
}
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
if (format_.format.audio.channels == 0) {
format_.format.audio.channels = codecCtx_->ch_layout.nb_channels;
}
#else
if (format_.format.audio.channels == 0) {
format_.format.audio.channels = codecCtx_->channels;
}
#endif
if (format_.format.audio.format == AV_SAMPLE_FMT_NONE) {
format_.format.audio.format = codecCtx_->sample_fmt;
}
Expand Down
Loading