Skip to content

Commit 2485377

Browse files
committed
ffmpeg: Restore compat with <5.1
1 parent ca57f7b commit 2485377

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

Source/Util/AVWrapper.cpp

+58-10
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ size_t AVWrapperFrame::getBufferSize() const {
2424
return av_image_get_buffer_size(format, frame->width, frame->height, 1);
2525
} else if (AVWrapperType::Audio) {
2626
AVSampleFormat format = static_cast<AVSampleFormat>(frame->format);
27-
//return av_samples_get_buffer_size(nullptr, frame->channels, frame->nb_samples, format, 1);
28-
int planes = av_sample_fmt_is_planar(format) ? frame->ch_layout.nb_channels : 1;
29-
return frame->linesize[0] * planes;
27+
int channels = 1;
28+
if (av_sample_fmt_is_planar(format)) {
29+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
30+
channels = frame->ch_layout.nb_channels;
31+
#else
32+
channels = frame->channels;
33+
#endif
34+
};
35+
return frame->linesize[0] * channels;
3036
} else {
3137
xassert(0);
3238
return 0;
@@ -49,17 +55,24 @@ size_t AVWrapperFrame::copyBuffer(uint8_t** buffer) const {
4955
return buffer_size;
5056
} else if (AVWrapperType::Audio) {
5157
AVSampleFormat format = static_cast<AVSampleFormat>(frame->format);
52-
if (av_sample_fmt_is_planar(format) && 1 < frame->ch_layout.nb_channels) {
58+
int channels = 1;
59+
if (av_sample_fmt_is_planar(format)) {
60+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
61+
channels = frame->ch_layout.nb_channels;
62+
#else
63+
channels = frame->channels;
64+
#endif
65+
};
66+
if (1 < channels) {
5367
//We need to pack planar audio
54-
int planes = frame->ch_layout.nb_channels;
5568
size_t bytes_sample = av_get_bytes_per_sample(format);
56-
xassert(buffer_size == frame->linesize[0] * planes);
69+
xassert(buffer_size == frame->linesize[0] * channels);
5770
//Store each plane sample as interleaved
5871
for (int si = 0; si < frame->nb_samples; ++si) {
5972
size_t srcoff = si * bytes_sample;
6073
xassert(srcoff + bytes_sample <= frame->linesize[0]);
61-
for (int pi = 0; pi < planes; pi++) {
62-
size_t dstoff = (si * planes + pi) * bytes_sample;
74+
for (int pi = 0; pi < channels; pi++) {
75+
size_t dstoff = (si * channels + pi) * bytes_sample;
6376
xassert(dstoff + bytes_sample <= buffer_size);
6477
memcpy(*buffer + dstoff, frame->data[pi] + srcoff, bytes_sample);
6578
}
@@ -120,7 +133,12 @@ int AVWrapper::close() {
120133
if (filterGraph) {
121134
avfilter_graph_free(&filterGraph);
122135
}
136+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
123137
swrChannelLayout = {};
138+
#else
139+
swrChannelLayout = 0;
140+
swrChannels = 0;
141+
#endif
124142
swrSampleRate = 0;
125143
swrFormat = AV_SAMPLE_FMT_NONE;
126144
#endif
@@ -162,8 +180,13 @@ void AVWrapper::pullFilterAudio() {
162180
break;
163181
}
164182

183+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
184+
int nb_channels = swrChannelLayout.nb_channels;
185+
#else
186+
int nb_channels = av_get_channel_layout_nb_channels(swrChannelLayout);
187+
#endif
165188
int bytes_per_sample = av_get_bytes_per_sample(swrFormat);
166-
int data_size = converted->frame->nb_samples * swrChannelLayout.nb_channels * bytes_per_sample;
189+
int data_size = converted->frame->nb_samples * nb_channels * bytes_per_sample;
167190
converted->frame->linesize[0] = data_size;
168191
converted->type = AVWrapperType::Audio;
169192
converted->time_base = av_buffersink_get_time_base(buffersinkCtx);
@@ -337,8 +360,13 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
337360
}
338361
#ifdef PERIMETER_FFMPEG_MOVIE
339362
swrFormat = dst_format != 0 ? dst_format : audioCodecCtx->sample_fmt;
363+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
340364
int num_channel = dst_channels != 0 ? dst_channels : audioCodecCtx->ch_layout.nb_channels;
341365
av_channel_layout_default(&swrChannelLayout, num_channel);
366+
#else
367+
swrChannels = dst_channels != 0 ? dst_channels : audioCodecCtx->channels;
368+
swrChannelLayout = av_get_default_channel_layout(swrChannels);
369+
#endif
342370
swrSampleRate = dst_frequency != 0 ? dst_frequency : audioCodecCtx->sample_rate;
343371
if (swrFormat == AV_SAMPLE_FMT_NONE) {
344372
fprintf(stderr, "setupAudioConverter unsupported device output format %d at %s\n", dst_format, file_path.c_str());
@@ -356,12 +384,24 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
356384
AVRational time_base = audioCodecCtx->time_base;
357385

358386
char channel_str[128];
387+
#if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100))
388+
const int64_t out_channel_layouts[] = { swrChannelLayout, -1 };
389+
if (!audioCodecCtx->channel_layout) {
390+
audioCodecCtx->channel_layout = av_get_default_channel_layout(audioCodecCtx->channels);
391+
}
392+
snprintf(channel_str, sizeof(channel_str), "0x%" PRIx64, audioCodecCtx->channel_layout);
393+
#else
359394
av_channel_layout_describe(&swrChannelLayout, channel_str, sizeof(channel_str));
395+
#endif
396+
360397
std::string args = "time_base=" + std::to_string(time_base.num) + "/" + std::to_string(time_base.den) +
361398
":sample_rate=" + std::to_string(audioCodecCtx->sample_rate) +
362399
":sample_fmt=" + av_get_sample_fmt_name(audioCodecCtx->sample_fmt) +
363400
":channel_layout=" + channel_str;
364401

402+
#if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100))
403+
av_get_channel_layout_string(channel_str, sizeof(channel_str), swrChannels, swrChannelLayout);
404+
#endif
365405
std::string filters_descr;
366406

367407
//Add aresample filter if sample rates mismatch
@@ -411,15 +451,19 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
411451
goto end;
412452
}
413453

454+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
414455
ret = av_opt_set(buffersinkCtx, "ch_layouts", channel_str, AV_OPT_SEARCH_CHILDREN);
456+
#else
457+
ret = av_opt_set_int_list(buffersinkCtx, "channel_layouts", out_channel_layouts, -1, AV_OPT_SEARCH_CHILDREN);
458+
#endif
415459
if (ret < 0) {
416460
fprintf(stderr, "setupAudioConverter Cannot set output channel layout\n");
417461
goto end;
418462
}
419463

420464
ret = av_opt_set_int_list(buffersinkCtx, "sample_rates", out_sample_rates, -1, AV_OPT_SEARCH_CHILDREN);
421465
if (ret < 0) {
422-
fprintf(stderr, "setupAudioConverter Cannot set output sample rate\n");
466+
fprintf(stderr, "Cannot set output sample rate\n");
423467
goto end;
424468
}
425469

@@ -462,7 +506,11 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
462506

463507
/* Print summary of the sink buffer */
464508
outlink = buffersinkCtx->inputs[0];
509+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
465510
av_channel_layout_describe(&outlink->ch_layout, channel_str, sizeof(channel_str));
511+
#else
512+
av_get_channel_layout_string(channel_str, sizeof(channel_str), -1, outlink->channel_layout);
513+
#endif
466514
fprintf(
467515
stdout, "AVWrapper audio conv: %s\n -> %s\n -> %dHz %s %s\n",
468516
args.c_str(), filters_descr.c_str(),

Source/Util/AVWrapper.h

+6
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,13 @@ class AVWrapper
112112
///Software scaler output frame format
113113
AVPixelFormat swsFormat = AV_PIX_FMT_NONE;
114114
///Audio converter output channel layout
115+
#if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
115116
AVChannelLayout swrChannelLayout = {};
117+
#else
118+
int64_t swrChannelLayout = 0;
119+
///Audio converter output channels
120+
int swrChannels = 0;
121+
#endif
116122
///Audio converter output rate
117123
int swrSampleRate = 0;
118124
///Audio converter output format

0 commit comments

Comments
 (0)