Fix FFmpeg deprecation warnings #127203

Merged
Sebastian Parborg merged 2 commits from ZedDB/blender:ffmpeg_fixes into main 2024-11-18 17:46:20 +01:00
4 changed files with 53 additions and 6 deletions

View File

@ -27,9 +27,14 @@ extern "C" {
AUD_NAMESPACE_BEGIN
/* FFmpeg < 4.0 */
#if LIBAVCODEC_VERSION_MAJOR < 58
#define FFMPEG_OLD_CODE
#endif
/* FFmpeg < 5.0 */
#if LIBAVCODEC_VERSION_MAJOR < 59
#define FFMPEG_OLD_CH_LAYOUT
#endif
SampleFormat FFMPEGReader::convertSampleFormat(AVSampleFormat format)
{
@ -112,7 +117,13 @@ int FFMPEGReader::decode(AVPacket& packet, Buffer& buffer)
if(ret != 0)
break;
int data_size = av_samples_get_buffer_size(nullptr, m_codecCtx->channels, m_frame->nb_samples, m_codecCtx->sample_fmt, 1);
#ifdef FFMPEG_OLD_CH_LAYOUT
int channels = m_codecCtx->channels;
#else
int channels = m_codecCtx->ch_layout.nb_channels;
#endif
int data_size = av_samples_get_buffer_size(nullptr, channels, m_frame->nb_samples, m_codecCtx->sample_fmt, 1);
if(buf_size - buf_pos < data_size)
{
@ -122,12 +133,12 @@ int FFMPEGReader::decode(AVPacket& packet, Buffer& buffer)
if(m_tointerleave)
{
int single_size = data_size / m_codecCtx->channels / m_frame->nb_samples;
for(int channel = 0; channel < m_codecCtx->channels; channel++)
int single_size = data_size / channels / m_frame->nb_samples;
for(int channel = 0; channel < channels; channel++)
{
for(int i = 0; i < m_frame->nb_samples; i++)
{
std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((m_codecCtx->channels * i) + channel) * single_size,
std::memcpy(((data_t*)buffer.getBuffer()) + buf_pos + ((channels * i) + channel) * single_size,
m_frame->data[channel] + i * single_size, single_size);
}
}
@ -207,7 +218,12 @@ void FFMPEGReader::init(int stream)
if(avcodec_open2(m_codecCtx, aCodec, nullptr) < 0)
AUD_THROW(FileException, "File couldn't be read, ffmpeg codec couldn't be opened.");
m_specs.channels = (Channels) m_codecCtx->channels;
#ifdef FFMPEG_OLD_CH_LAYOUT
int channels = m_codecCtx->channels;
#else
int channels = m_codecCtx->ch_layout.nb_channels;
#endif
m_specs.channels = (Channels) channels;
m_tointerleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
switch(av_get_packed_sample_fmt(m_codecCtx->sample_fmt))
@ -345,7 +361,12 @@ std::vector<StreamInfo> FFMPEGReader::queryStreams()
info.specs.rate = m_formatCtx->streams[i]->codec->sample_rate;
info.specs.format = convertSampleFormat(m_formatCtx->streams[i]->codec->sample_fmt);
#else
info.specs.channels = Channels(m_formatCtx->streams[i]->codecpar->channels);
#ifdef FFMPEG_OLD_CH_LAYOUT
int channels = m_formatCtx->streams[i]->codecpar->channels;
#else
int channels = m_formatCtx->streams[i]->codecpar->ch_layout.nb_channels;
#endif
info.specs.channels = Channels(channels);
info.specs.rate = m_formatCtx->streams[i]->codecpar->sample_rate;
info.specs.format = convertSampleFormat(AVSampleFormat(m_formatCtx->streams[i]->codecpar->format));
#endif

View File

@ -30,9 +30,14 @@ extern "C" {
AUD_NAMESPACE_BEGIN
/* FFmpeg < 4.0 */
#if LIBAVCODEC_VERSION_MAJOR < 58
#define FFMPEG_OLD_CODE
#endif
/* FFmpeg < 5.0 */
#if LIBAVCODEC_VERSION_MAJOR < 59
#define FFMPEG_OLD_CH_LAYOUT
#endif
void FFMPEGWriter::encode()
{
@ -77,8 +82,13 @@ void FFMPEGWriter::encode()
m_frame->nb_samples = m_input_samples;
m_frame->format = m_codecCtx->sample_fmt;
#ifdef FFMPEG_OLD_CH_LAYOUT
m_frame->channel_layout = m_codecCtx->channel_layout;
m_frame->channels = m_specs.channels;
#else
if(av_channel_layout_copy(&m_frame->ch_layout, &m_codecCtx->ch_layout) < 0)
AUD_THROW(FileException, "File couldn't be written, couldn't copy audio channel layout.");
#endif
if(avcodec_fill_audio_frame(m_frame, m_specs.channels, m_codecCtx->sample_fmt, reinterpret_cast<data_t*>(data), m_input_buffer.getSize(), 0) < 0)
AUD_THROW(FileException, "File couldn't be written, filling the audio frame failed with ffmpeg.");
@ -405,8 +415,13 @@ FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Conta
m_codecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
m_codecCtx->bit_rate = bitrate;
#ifdef FFMPEG_OLD_CH_LAYOUT
m_codecCtx->channel_layout = channel_layout;
m_codecCtx->channels = m_specs.channels;
#else
av_channel_layout_uninit(&m_codecCtx->ch_layout);
av_channel_layout_from_mask(&m_codecCtx->ch_layout, channel_layout);
#endif
m_stream->time_base.num = m_codecCtx->time_base.num = 1;
m_stream->time_base.den = m_codecCtx->time_base.den = m_codecCtx->sample_rate;

View File

@ -40,6 +40,13 @@
# define FFMPEG_INLINE static inline
#endif
#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(58, 29, 100)
/* In ffmpeg 6.1 usage of the "key_frame" variable from "AVFrame" has been deprecated.
* used the new method to query for the "AV_FRAME_FLAG_KEY" flag instead.
*/
# define FFMPEG_OLD_KEY_FRAME_QUERY_METHOD
#endif
#if (LIBAVFORMAT_VERSION_MAJOR < 59)
/* For versions older than ffmpeg 5.0, use the old channel layout variables.
* We intend to only keep this workaround for around two releases (3.5, 3.6).

View File

@ -700,7 +700,11 @@ static void ffmpeg_decode_store_frame_pts(ImBufAnim *anim)
{
anim->cur_pts = av_get_pts_from_frame(anim->pFrame);
# ifdef FFMPEG_OLD_KEY_FRAME_QUERY_METHOD
if (anim->pFrame->key_frame) {
# else
if (anim->pFrame->flags & AV_FRAME_FLAG_KEY) {
# endif
anim->cur_key_frame_pts = anim->cur_pts;
}