Files
ports/graphics/openscenegraph/patches/patch-src_osgPlugins_ffmpeg_FFmpegDecoderAudio_cpp
2025-10-21 12:55:33 +00:00

145 lines
5.0 KiB
Plaintext

Fix building with newer FFmpeg
Index: src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp
--- src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp.orig
+++ src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp
@@ -13,15 +13,6 @@
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
#endif
-#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
-#define av_frame_alloc avcodec_alloc_frame
-#define av_frame_free avcodec_free_frame
-#endif
-
-#if LIBAVCODEC_VERSION_MAJOR < 56
- #define AV_CODEC_ID_NONE CODEC_ID_NONE
-#endif
-
namespace osgFFmpeg {
static int decode_audio(AVCodecContext *avctx, int16_t *samples,
@@ -32,26 +23,28 @@ static int decode_audio(AVCodecContext *avctx, int16_t
int out_nb_channels,
AVSampleFormat out_sample_format)
{
-#if LIBAVCODEC_VERSION_MAJOR >= 53 || (LIBAVCODEC_VERSION_MAJOR==52 && LIBAVCODEC_VERSION_MINOR>=32)
-
AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = const_cast<uint8_t *>(buf);
avpkt.size = buf_size;
AVFrame *frame = av_frame_alloc();
- int ret, got_frame = 0;
+ int ret;
if (!frame)
return AVERROR(ENOMEM);
- ret = avcodec_decode_audio4(avctx, frame, &got_frame, &avpkt);
+ ret = avcodec_receive_frame(avctx, frame);
#ifdef USE_AVRESAMPLE // libav's AVFrame structure does not contain a 'channels' field
- if (ret >= 0 && got_frame) {
+ if (ret >= 0) {
#else
- if (ret >= 0 && got_frame && av_frame_get_channels(frame)>0) {
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
+ if (ret >= 0 && frame->channels>0) {
+#else
+ if (ret >= 0 && frame->ch_layout.nb_channels>0) {
#endif
+#endif
int ch, plane_size;
int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
@@ -92,9 +85,15 @@ static int decode_audio(AVCodecContext *avctx, int16_t
memcpy(samples, frame->extended_data[0], plane_size);
- if (planar && avctx->channels > 1) {
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
+ int _channels = avctx->channels;
+#else
+ int _channels = avctx->ch_layout.nb_channels;
+#endif
+
+ if (planar && _channels > 1) {
uint8_t *out = ((uint8_t *)samples) + plane_size;
- for (ch = 1; ch < avctx->channels; ch++) {
+ for (ch = 1; ch < _channels; ch++) {
memcpy(out, frame->extended_data[ch], plane_size);
out += plane_size;
}
@@ -108,11 +107,6 @@ static int decode_audio(AVCodecContext *avctx, int16_t
}
av_frame_free(&frame);
return ret;
-
-#else
- // fallback for older versions of ffmpeg that don't have avcodec_decode_audio3.
- return avcodec_decode_audio2(avctx, samples, frame_size_ptr, buf, buf_size);
-#endif
}
@@ -151,10 +145,16 @@ void FFmpegDecoderAudio::open(AVStream * const stream,
return;
m_stream = stream;
- m_context = stream->codec;
+ m_codecpar = stream->codecpar;
+ const AVCodec* p_codec = avcodec_find_decoder(m_codecpar->codec_id);
+ m_context = avcodec_alloc_context3(p_codec);
m_in_sample_rate = m_context->sample_rate;
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
m_in_nb_channels = m_context->channels;
+#else
+ m_in_nb_channels = m_context->ch_layout.nb_channels;
+#endif
m_in_sample_format = m_context->sample_fmt;
AVDictionaryEntry *opt_out_sample_rate = av_dict_get( *parameters->getOptions(), "out_sample_rate", NULL, 0 );
@@ -189,16 +189,30 @@ printf("### CONVERTING from sample format %s TO %s\n\t
m_in_sample_rate,
m_out_sample_rate);
#endif
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
m_swr_context = swr_alloc_set_opts(NULL,
av_get_default_channel_layout(m_out_nb_channels),
+#else
+ int err = swr_alloc_set_opts2(&m_swr_context,
+ &m_context->ch_layout,
+#endif
m_out_sample_format,
m_out_sample_rate,
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
av_get_default_channel_layout(m_in_nb_channels),
+#else
+ &m_context->ch_layout,
+#endif
m_in_sample_format,
m_in_sample_rate,
0, NULL );
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
int err = swr_init(m_swr_context);
+#else
+ if ( !err )
+ err = swr_init(m_swr_context);
+#endif
if ( err ) {
char error_string[512];
@@ -214,7 +228,7 @@ printf("### CONVERTING from sample format %s TO %s\n\t
throw std::runtime_error("invalid audio codec");;
// Find the decoder for the audio stream
- AVCodec * const p_codec = avcodec_find_decoder(m_context->codec_id);
+ p_codec = avcodec_find_decoder(m_context->codec_id);
if (p_codec == 0)
throw std::runtime_error("avcodec_find_decoder() failed");