Skip to content

Commit

Permalink
Add compatibility with FFMPEG 7.0 (#8408)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Hug <nh.nicolas.hug@gmail.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
  • Loading branch information
3 people authored Jun 6, 2024
1 parent a839642 commit 375cfdf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
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

0 comments on commit 375cfdf

Please sign in to comment.