mirror of
https://gitee.com/xia-chu/ZLMediaKit.git
synced 2026-05-19 16:27:50 +08:00
适配FFmpeg7.1 (#4102)
ffmpeg中FF_API_OLD_CHANNEL_LAYOUT系列API在2024年3月的提交中已经正式移除了,对应release7.0,使用ffmpeg7.0及以上版本编译zlm会报错。
This commit is contained in:
parent
ac6bb9b332
commit
f3026f5df0
@ -97,7 +97,13 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable {
|
decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable {
|
||||||
if (!swr) {
|
if (!swr) {
|
||||||
|
|
||||||
|
# if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
swr = std::make_shared<FFmpegSwr>(AV_SAMPLE_FMT_S16, &(frame->get()->ch_layout), frame->get()->sample_rate);
|
||||||
|
#else
|
||||||
swr = std::make_shared<FFmpegSwr>(AV_SAMPLE_FMT_S16, frame->get()->channels, frame->get()->channel_layout, frame->get()->sample_rate);
|
swr = std::make_shared<FFmpegSwr>(AV_SAMPLE_FMT_S16, frame->get()->channels, frame->get()->channel_layout, frame->get()->sample_rate);
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
auto pcm = swr->inputFrame(frame);
|
auto pcm = swr->inputFrame(frame);
|
||||||
auto len = pcm->get()->nb_samples * pcm->get()->channels * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format);
|
auto len = pcm->get()->nb_samples * pcm->get()->channels * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format);
|
||||||
|
|||||||
@ -420,9 +420,15 @@ FFmpegDecoder::FFmpegDecoder(const Track::Ptr &track, int thread_num, const std:
|
|||||||
case CodecG711A:
|
case CodecG711A:
|
||||||
case CodecG711U: {
|
case CodecG711U: {
|
||||||
AudioTrack::Ptr audio = static_pointer_cast<AudioTrack>(track);
|
AudioTrack::Ptr audio = static_pointer_cast<AudioTrack>(track);
|
||||||
|
|
||||||
|
#if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
av_channel_layout_default(&_context->ch_layout, audio->getAudioChannel());
|
||||||
|
#else
|
||||||
_context->channels = audio->getAudioChannel();
|
_context->channels = audio->getAudioChannel();
|
||||||
_context->sample_rate = audio->getAudioSampleRate();
|
|
||||||
_context->channel_layout = av_get_default_channel_layout(_context->channels);
|
_context->channel_layout = av_get_default_channel_layout(_context->channels);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
_context->sample_rate = audio->getAudioSampleRate();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@ -582,41 +588,73 @@ void FFmpegDecoder::onDecode(const FFmpegFrame::Ptr &frame) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
FFmpegSwr::FFmpegSwr(AVSampleFormat output, AVChannelLayout *ch_layout, int samplerate) {
|
||||||
|
_target_format = output;
|
||||||
|
av_channel_layout_copy(&_target_ch_layout, ch_layout);
|
||||||
|
_target_samplerate = samplerate;
|
||||||
|
}
|
||||||
|
#else
|
||||||
FFmpegSwr::FFmpegSwr(AVSampleFormat output, int channel, int channel_layout, int samplerate) {
|
FFmpegSwr::FFmpegSwr(AVSampleFormat output, int channel, int channel_layout, int samplerate) {
|
||||||
_target_format = output;
|
_target_format = output;
|
||||||
_target_channels = channel;
|
_target_channels = channel;
|
||||||
_target_channel_layout = channel_layout;
|
_target_channel_layout = channel_layout;
|
||||||
_target_samplerate = samplerate;
|
_target_samplerate = samplerate;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
FFmpegSwr::~FFmpegSwr() {
|
FFmpegSwr::~FFmpegSwr() {
|
||||||
if (_ctx) {
|
if (_ctx) {
|
||||||
swr_free(&_ctx);
|
swr_free(&_ctx);
|
||||||
}
|
}
|
||||||
|
#if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
av_channel_layout_uninit(&_target_ch_layout);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
FFmpegFrame::Ptr FFmpegSwr::inputFrame(const FFmpegFrame::Ptr &frame) {
|
FFmpegFrame::Ptr FFmpegSwr::inputFrame(const FFmpegFrame::Ptr &frame) {
|
||||||
if (frame->get()->format == _target_format &&
|
if (frame->get()->format == _target_format &&
|
||||||
frame->get()->channels == _target_channels &&
|
|
||||||
frame->get()->channel_layout == (uint64_t)_target_channel_layout &&
|
#if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
!av_channel_layout_compare(&(frame->get()->ch_layout), &_target_ch_layout) &&
|
||||||
|
#else
|
||||||
|
frame->get()->channels == _target_channels && frame->get()->channel_layout == (uint64_t)_target_channel_layout &&
|
||||||
|
#endif
|
||||||
|
|
||||||
frame->get()->sample_rate == _target_samplerate) {
|
frame->get()->sample_rate == _target_samplerate) {
|
||||||
// 不转格式 [AUTO-TRANSLATED:31dc6ae1]
|
// 不转格式 [AUTO-TRANSLATED:31dc6ae1]
|
||||||
// Do not convert format
|
// Do not convert format
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
if (!_ctx) {
|
if (!_ctx) {
|
||||||
|
|
||||||
|
#if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
_ctx = swr_alloc();
|
||||||
|
swr_alloc_set_opts2(&_ctx,
|
||||||
|
&_target_ch_layout, _target_format, _target_samplerate,
|
||||||
|
&frame->get()->ch_layout, (AVSampleFormat)frame->get()->format, frame->get()->sample_rate,
|
||||||
|
0, nullptr);
|
||||||
|
#else
|
||||||
_ctx = swr_alloc_set_opts(nullptr, _target_channel_layout, _target_format, _target_samplerate,
|
_ctx = swr_alloc_set_opts(nullptr, _target_channel_layout, _target_format, _target_samplerate,
|
||||||
frame->get()->channel_layout, (AVSampleFormat) frame->get()->format,
|
frame->get()->channel_layout, (AVSampleFormat) frame->get()->format,
|
||||||
frame->get()->sample_rate, 0, nullptr);
|
frame->get()->sample_rate, 0, nullptr);
|
||||||
|
#endif
|
||||||
|
|
||||||
InfoL << "swr_alloc_set_opts:" << av_get_sample_fmt_name((enum AVSampleFormat) frame->get()->format) << " -> "
|
InfoL << "swr_alloc_set_opts:" << av_get_sample_fmt_name((enum AVSampleFormat) frame->get()->format) << " -> "
|
||||||
<< av_get_sample_fmt_name(_target_format);
|
<< av_get_sample_fmt_name(_target_format);
|
||||||
}
|
}
|
||||||
if (_ctx) {
|
if (_ctx) {
|
||||||
auto out = std::make_shared<FFmpegFrame>();
|
auto out = std::make_shared<FFmpegFrame>();
|
||||||
out->get()->format = _target_format;
|
out->get()->format = _target_format;
|
||||||
|
|
||||||
|
#if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
out->get()->ch_layout = _target_ch_layout;
|
||||||
|
av_channel_layout_copy(&(out->get()->ch_layout), &_target_ch_layout);
|
||||||
|
#else
|
||||||
out->get()->channel_layout = _target_channel_layout;
|
out->get()->channel_layout = _target_channel_layout;
|
||||||
out->get()->channels = _target_channels;
|
out->get()->channels = _target_channels;
|
||||||
|
#endif
|
||||||
|
|
||||||
out->get()->sample_rate = _target_samplerate;
|
out->get()->sample_rate = _target_samplerate;
|
||||||
out->get()->pkt_dts = frame->get()->pkt_dts;
|
out->get()->pkt_dts = frame->get()->pkt_dts;
|
||||||
out->get()->pts = frame->get()->pts;
|
out->get()->pts = frame->get()->pts;
|
||||||
|
|||||||
@ -26,10 +26,13 @@ extern "C" {
|
|||||||
#include "libswresample/swresample.h"
|
#include "libswresample/swresample.h"
|
||||||
#include "libavutil/audio_fifo.h"
|
#include "libavutil/audio_fifo.h"
|
||||||
#include "libavutil/imgutils.h"
|
#include "libavutil/imgutils.h"
|
||||||
|
#include "libavutil/frame.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define FF_CODEC_VER_7_1 AV_VERSION_INT(61, 0, 0)
|
||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
class FFmpegFrame {
|
class FFmpegFrame {
|
||||||
@ -51,13 +54,24 @@ class FFmpegSwr {
|
|||||||
public:
|
public:
|
||||||
using Ptr = std::shared_ptr<FFmpegSwr>;
|
using Ptr = std::shared_ptr<FFmpegSwr>;
|
||||||
|
|
||||||
|
# if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
FFmpegSwr(AVSampleFormat output, AVChannelLayout *ch_layout, int samplerate);
|
||||||
|
#else
|
||||||
FFmpegSwr(AVSampleFormat output, int channel, int channel_layout, int samplerate);
|
FFmpegSwr(AVSampleFormat output, int channel, int channel_layout, int samplerate);
|
||||||
|
#endif
|
||||||
|
|
||||||
~FFmpegSwr();
|
~FFmpegSwr();
|
||||||
FFmpegFrame::Ptr inputFrame(const FFmpegFrame::Ptr &frame);
|
FFmpegFrame::Ptr inputFrame(const FFmpegFrame::Ptr &frame);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
# if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1
|
||||||
|
AVChannelLayout _target_ch_layout;
|
||||||
|
#else
|
||||||
int _target_channels;
|
int _target_channels;
|
||||||
int _target_channel_layout;
|
int _target_channel_layout;
|
||||||
|
#endif
|
||||||
|
|
||||||
int _target_samplerate;
|
int _target_samplerate;
|
||||||
AVSampleFormat _target_format;
|
AVSampleFormat _target_format;
|
||||||
SwrContext *_ctx = nullptr;
|
SwrContext *_ctx = nullptr;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user