From 1340adb8207382440ddd85a9e01f7a65fb9c03ea Mon Sep 17 00:00:00 2001 From: Stephen Saunders Date: Thu, 22 Dec 2022 21:24:50 -0500 Subject: [PATCH] Restore support for FFMPEG legacy channel_layout & channels fields, remove self-assignment in idSoundDecoder_Vorbis::Open() (cherry picked from commit 22f9e5cc0e42a8d101f628df461ee7649acb3e01) --- neo/renderer/Cinematic.cpp | 23 +++++++++++++++++++---- neo/sound/OpenAL/AL_CinematicAudio.cpp | 12 ++++++++++++ neo/sound/XAudio2/XA2_CinematicAudio.cpp | 4 ++++ neo/sound/snd_decoder.cpp | 4 ++-- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/neo/renderer/Cinematic.cpp b/neo/renderer/Cinematic.cpp index a5b3a166..51717654 100644 --- a/neo/renderer/Cinematic.cpp +++ b/neo/renderer/Cinematic.cpp @@ -738,11 +738,15 @@ bool idCinematicLocal::InitFromFFMPEGFile( const char* qpath, bool amilooping, n if( dec_ctx2->sample_fmt >= AV_SAMPLE_FMT_U8P ) // SRS - Planar formats start at AV_SAMPLE_FMT_U8P { dst_smp = static_cast( dec_ctx2->sample_fmt - AV_SAMPLE_FMT_U8P ); // SRS - Setup context to convert from planar to packed +#if LIBSWRESAMPLE_VERSION_INT >= AV_VERSION_INT(4,7,100) if( ( ret2 = swr_alloc_set_opts2( &swr_ctx, &dec_ctx2->ch_layout, dst_smp, dec_ctx2->sample_rate, &dec_ctx2->ch_layout, dec_ctx2->sample_fmt, dec_ctx2->sample_rate, 0, NULL ) ) < 0 ) { av_strerror( ret2, error, sizeof( error ) ); common->Warning( "idCinematic: Failed to create audio resample context with error: %s\n", error ); } +#else + swr_ctx = swr_alloc_set_opts( NULL, dec_ctx2->channel_layout, dst_smp, dec_ctx2->sample_rate, dec_ctx2->channel_layout, dec_ctx2->sample_fmt, dec_ctx2->sample_rate, 0, NULL ); +#endif ret2 = swr_init( swr_ctx ); hasplanar = true; } @@ -751,12 +755,19 @@ bool idCinematicLocal::InitFromFFMPEGFile( const char* qpath, bool amilooping, n dst_smp = dec_ctx2->sample_fmt; // SRS - Must always define the destination format hasplanar = false; } - common->Printf( "Cinematic audio stream found: Sample Rate=%d Hz, Channels=%d, Format=%s, Planar=%d\n", dec_ctx2->sample_rate, dec_ctx2->ch_layout.nb_channels, GetSampleFormat( dec_ctx2->sample_fmt ), hasplanar ); - #if defined(_MSC_VER) && !defined(USE_OPENAL) + common->Printf( "Cinematic audio stream found: Sample Rate=%d Hz, Channels=%d, Format=%s, Planar=%d\n", dec_ctx2->sample_rate, +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,37,100) + dec_ctx2->ch_layout.nb_channels, +#else + dec_ctx2->channels, +#endif + GetSampleFormat( dec_ctx2->sample_fmt ), hasplanar ); + +#if defined(_MSC_VER) && !defined(USE_OPENAL) cinematicAudio = new( TAG_AUDIO ) CinematicAudio_XAudio2; - #else +#else cinematicAudio = new( TAG_AUDIO ) CinematicAudio_OpenAL; - #endif +#endif cinematicAudio->InitAudio( dec_ctx2 ); } //GK:End @@ -1484,7 +1495,11 @@ cinData_t idCinematicLocal::ImageForTimeFFMPEG( int thisTime, nvrhi::ICommandLis else if( framePos + 1 == desiredFrame ) { // SRS - Since destination sample format is packed (non-planar), returned bufflinesize equals num_bytes +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,37,100) res = av_samples_alloc( &audioBuffer, &num_bytes, frame3->ch_layout.nb_channels, frame3->nb_samples, dst_smp, 0 ); +#else + res = av_samples_alloc( &audioBuffer, &num_bytes, frame3->channels, frame3->nb_samples, dst_smp, 0 ); +#endif if( res < 0 || res != num_bytes ) { common->Warning( "idCinematic: Failed to allocate audio buffer with result: %d\n", res ); diff --git a/neo/sound/OpenAL/AL_CinematicAudio.cpp b/neo/sound/OpenAL/AL_CinematicAudio.cpp index b1cb829c..04adf508 100644 --- a/neo/sound/OpenAL/AL_CinematicAudio.cpp +++ b/neo/sound/OpenAL/AL_CinematicAudio.cpp @@ -66,19 +66,31 @@ void CinematicAudio_OpenAL::InitAudio( void* audioContext ) case AV_SAMPLE_FMT_U8: case AV_SAMPLE_FMT_U8P: { +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,37,100) av_sample_cin = dec_ctx2->ch_layout.nb_channels == 2 ? AL_FORMAT_STEREO8 : AL_FORMAT_MONO8; +#else + av_sample_cin = dec_ctx2->channels == 2 ? AL_FORMAT_STEREO8 : AL_FORMAT_MONO8; +#endif break; } case AV_SAMPLE_FMT_S16: case AV_SAMPLE_FMT_S16P: { +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,37,100) av_sample_cin = dec_ctx2->ch_layout.nb_channels == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; +#else + av_sample_cin = dec_ctx2->channels == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; +#endif break; } case AV_SAMPLE_FMT_FLT: case AV_SAMPLE_FMT_FLTP: { +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,37,100) av_sample_cin = dec_ctx2->ch_layout.nb_channels == 2 ? AL_FORMAT_STEREO_FLOAT32 : AL_FORMAT_MONO_FLOAT32; +#else + av_sample_cin = dec_ctx2->channels == 2 ? AL_FORMAT_STEREO_FLOAT32 : AL_FORMAT_MONO_FLOAT32; +#endif break; } default: diff --git a/neo/sound/XAudio2/XA2_CinematicAudio.cpp b/neo/sound/XAudio2/XA2_CinematicAudio.cpp index bfa05cd2..121168b1 100644 --- a/neo/sound/XAudio2/XA2_CinematicAudio.cpp +++ b/neo/sound/XAudio2/XA2_CinematicAudio.cpp @@ -108,7 +108,11 @@ void CinematicAudio_XAudio2::InitAudio( void* audioContext ) return; } } +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,37,100) voiceFormatcine.nChannels = dec_ctx2->ch_layout.nb_channels; //fixed +#else + voiceFormatcine.nChannels = dec_ctx2->channels; //fixed +#endif voiceFormatcine.nSamplesPerSec = dec_ctx2->sample_rate; //fixed #elif defined(USE_BINKDEC) AudioInfo* binkInfo = ( AudioInfo* )audioContext; diff --git a/neo/sound/snd_decoder.cpp b/neo/sound/snd_decoder.cpp index 00940d28..37fdcf51 100644 --- a/neo/sound/snd_decoder.cpp +++ b/neo/sound/snd_decoder.cpp @@ -330,7 +330,7 @@ bool idSoundDecoder_Vorbis::Open( const char* fileName ) return false; } - this->sample = sample; + //this->sample = sample; // SRS - self assignment not needed here return true; -} \ No newline at end of file +}