Skip to content

Commit 6ab07d2

Browse files
authored
Merge pull request #756 from eclipseo/fix_for_ffmpeg7
Add compatibility with FFMPEG 7.0
2 parents bee4233 + 20f5cf2 commit 6ab07d2

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

src/media.cpp

+45-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ typedef AVCodec FeAVCodec;
6161
#define FORMAT_CTX_URL m_imp->m_format_ctx->filename
6262
#endif
6363

64+
#define HAVE_CH_LAYOUT (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
65+
#define HAVE_DURATION (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(58, 2, 100))
66+
6467
void try_hw_accel( AVCodecContext *&codec_ctx, FeAVCodec *&dec );
6568

6669
std::string g_decoder;
@@ -321,9 +324,15 @@ FeAudioImp::~FeAudioImp()
321324

322325
bool FeAudioImp::process_frame( AVFrame *frame, sf::SoundStream::Chunk &data, int &offset )
323326
{
327+
#if HAVE_CH_LAYOUT
328+
int nb_channels = codec_ctx->ch_layout.nb_channels;
329+
#else
330+
int nb_channels = codec_ctx->channels;
331+
#endif
332+
324333
int data_size = av_samples_get_buffer_size(
325334
NULL,
326-
codec_ctx->channels,
335+
nb_channels,
327336
frame->nb_samples,
328337
codec_ctx->sample_fmt, 1);
329338

@@ -349,17 +358,27 @@ bool FeAudioImp::process_frame( AVFrame *frame, sf::SoundStream::Chunk &data, in
349358
return false;
350359
}
351360

361+
#if HAVE_CH_LAYOUT
362+
AVChannelLayout layout;
363+
av_channel_layout_copy(&layout, &frame->ch_layout);
364+
if (!av_channel_layout_check(&layout)) {
365+
av_channel_layout_default(&layout, codec_ctx->ch_layout.nb_channels);
366+
}
367+
av_opt_set_chlayout(resample_ctx, "in_chlayout", &layout, 0);
368+
av_opt_set_chlayout(resample_ctx, "out_chlayout", &layout, 0);
369+
#else
352370
int64_t channel_layout = frame->channel_layout;
353371
if ( !channel_layout )
354372
{
355373
channel_layout = av_get_default_channel_layout(
356374
codec_ctx->channels );
357375
}
358-
359376
av_opt_set_int( resample_ctx, "in_channel_layout", channel_layout, 0 );
377+
av_opt_set_int( resample_ctx, "out_channel_layout", channel_layout, 0 );
378+
#endif
379+
360380
av_opt_set_int( resample_ctx, "in_sample_fmt", frame->format, 0 );
361381
av_opt_set_int( resample_ctx, "in_sample_rate", frame->sample_rate, 0 );
362-
av_opt_set_int( resample_ctx, "out_channel_layout", channel_layout, 0 );
363382
av_opt_set_int( resample_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0 );
364383
av_opt_set_int( resample_ctx, "out_sample_rate", frame->sample_rate, 0 );
365384

@@ -382,9 +401,15 @@ bool FeAudioImp::process_frame( AVFrame *frame, sf::SoundStream::Chunk &data, in
382401
if ( resample_ctx )
383402
{
384403
int out_linesize;
404+
#if HAVE_CH_LAYOUT
405+
int nb_channels = codec_ctx->ch_layout.nb_channels;
406+
#else
407+
int nb_channels = codec_ctx->channels;
408+
#endif
409+
385410
av_samples_get_buffer_size(
386411
&out_linesize,
387-
codec_ctx->channels,
412+
nb_channels,
388413
frame->nb_samples,
389414
AV_SAMPLE_FMT_S16, 0 );
390415

@@ -402,8 +427,9 @@ bool FeAudioImp::process_frame( AVFrame *frame, sf::SoundStream::Chunk &data, in
402427
FeLog() << "Error performing audio conversion." << std::endl;
403428
return false;
404429
}
405-
offset += out_samples * codec_ctx->channels;
406-
data.sampleCount += out_samples * codec_ctx->channels;
430+
431+
offset += out_samples * nb_channels;
432+
data.sampleCount += out_samples * nb_channels;
407433
data.samples = audio_buff;
408434
}
409435
}
@@ -752,15 +778,20 @@ void FeVideoImp::video_thread()
752778
if ( raw_frame->pts == AV_NOPTS_VALUE )
753779
raw_frame->pts = packet->dts;
754780

755-
// This only works on FFmpeg, exclude libav (it doesn't have pkt_duration
781+
756782
#if (LIBAVUTIL_VERSION_MICRO >= 100 )
783+
// This only works on FFmpeg, exclude libav (it doesn't have pkt_duration
757784
// Correct for out of bounds pts
758785
if ( raw_frame->pts < prev_pts )
759786
raw_frame->pts = prev_pts + prev_duration;
760787

761788
// Track pts and duration if we need to correct next frame
762789
prev_pts = raw_frame->pts;
790+
#if HAVE_DURATION
791+
prev_duration = raw_frame->duration;
792+
#else
763793
prev_duration = raw_frame->pkt_duration;
794+
#endif
764795
#endif
765796

766797
detached_frame = raw_frame;
@@ -1065,8 +1096,14 @@ bool FeMedia::open( const std::string &archive,
10651096
+ AV_INPUT_BUFFER_PADDING_SIZE
10661097
+ codec_ctx->sample_rate );
10671098

1099+
#if HAVE_CH_LAYOUT
1100+
int nb_channels = codec_ctx->ch_layout.nb_channels;
1101+
#else
1102+
int nb_channels = codec_ctx->channels;
1103+
#endif
1104+
10681105
sf::SoundStream::initialize(
1069-
codec_ctx->channels,
1106+
nb_channels,
10701107
codec_ctx->sample_rate );
10711108

10721109
sf::SoundStream::setLoop( false );

0 commit comments

Comments
 (0)