1 From 8ccad6937177b1b92e40ab8f4447ea27bac009a7 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= <lalinsky@gmail.com>
3 Date: Fri, 4 Nov 2022 21:47:38 +0100
4 Subject: [PATCH] Use FFmpeg 5.x (#120)
6 * Use FFmpeg 5.1.2 for CI builds
8 * Build on Ubuntu 20.04
10 * Upgrade code to FFmpeg 5.x APIs
12 * Only set FFmpeg include dirs if building tools
18 .github/workflows/build.yml | 6 +-
19 CMakeLists.txt | 16 --
20 package/build.sh | 4 +-
21 src/audio/ffmpeg_audio_processor.h | 2 -
22 src/audio/ffmpeg_audio_processor_avresample.h | 72 -------
23 src/audio/ffmpeg_audio_processor_swresample.h | 18 +-
24 src/audio/ffmpeg_audio_reader.h | 197 +++++++++---------
25 tests/CMakeLists.txt | 6 +
26 8 files changed, 122 insertions(+), 199 deletions(-)
27 delete mode 100644 src/audio/ffmpeg_audio_processor_avresample.h
29 diff --git a/CMakeLists.txt b/CMakeLists.txt
30 index f8d6a32..4da2405 100644
33 @@ -84,9 +84,6 @@ find_package(FFmpeg)
35 cmake_push_check_state(RESET)
36 set(CMAKE_REQUIRED_LIBRARIES ${FFMPEG_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lm)
37 - check_function_exists(av_packet_unref HAVE_AV_PACKET_UNREF)
38 - check_function_exists(av_frame_alloc HAVE_AV_FRAME_ALLOC)
39 - check_function_exists(av_frame_free HAVE_AV_FRAME_FREE)
40 cmake_pop_check_state()
43 @@ -163,14 +160,11 @@ message(STATUS "Using ${FFT_LIB} for FFT calculations")
44 if(NOT AUDIO_PROCESSOR_LIB)
45 if(FFMPEG_LIBSWRESAMPLE_FOUND)
46 set(AUDIO_PROCESSOR_LIB "swresample")
47 - elseif(FFMPEG_LIBAVRESAMPLE_FOUND)
48 - set(AUDIO_PROCESSOR_LIB "avresample")
52 if(AUDIO_PROCESSOR_LIB STREQUAL "swresample")
53 if(FFMPEG_LIBSWRESAMPLE_FOUND)
54 - set(USE_AVRESAMPLE OFF)
55 set(USE_SWRESAMPLE ON)
56 set(AUDIO_PROCESSOR_LIBRARIES ${FFMPEG_LIBSWRESAMPLE_LIBRARIES})
57 set(AUDIO_PROCESSOR_INCLUDE_DIRS ${FFMPEG_LIBSWRESAMPLE_INCLUDE_DIRS})
58 @@ -178,16 +172,6 @@ if(AUDIO_PROCESSOR_LIB STREQUAL "swresample")
59 message(FATAL_ERROR "Selected ${AUDIO_PROCESSOR_LIB} for audio processing, but the library is not found")
61 message(STATUS "Using ${AUDIO_PROCESSOR_LIB} for audio conversion")
62 -elseif(AUDIO_PROCESSOR_LIB STREQUAL "avresample")
63 - if(FFMPEG_LIBAVRESAMPLE_FOUND)
64 - set(USE_AVRESAMPLE ON)
65 - set(USE_SWRESAMPLE OFF)
66 - set(AUDIO_PROCESSOR_LIBRARIES ${FFMPEG_LIBAVRESAMPLE_LIBRARIES})
67 - set(AUDIO_PROCESSOR_INCLUDE_DIRS ${FFMPEG_LIBAVRESAMPLE_INCLUDE_DIRS})
69 - message(FATAL_ERROR "Selected ${AUDIO_PROCESSOR_LIB} for audio processing, but the library is not found")
71 - message(STATUS "Using ${AUDIO_PROCESSOR_LIB} for audio conversion")
73 message(STATUS "Building without audio conversion support, please install FFmpeg with libswresample")
75 diff --git a/src/audio/ffmpeg_audio_processor.h b/src/audio/ffmpeg_audio_processor.h
76 index 7628fc7..39f4f6d 100644
77 --- a/src/audio/ffmpeg_audio_processor.h
78 +++ b/src/audio/ffmpeg_audio_processor.h
81 #if defined(USE_SWRESAMPLE)
82 #include "audio/ffmpeg_audio_processor_swresample.h"
83 -#elif defined(USE_AVRESAMPLE)
84 -#include "audio/ffmpeg_audio_processor_avresample.h"
86 #error "no audio processing library"
88 diff --git a/src/audio/ffmpeg_audio_processor_avresample.h b/src/audio/ffmpeg_audio_processor_avresample.h
89 deleted file mode 100644
90 index bd85f92..0000000
91 --- a/src/audio/ffmpeg_audio_processor_avresample.h
94 -// Copyright (C) 2016 Lukas Lalinsky
95 -// Distributed under the MIT license, see the LICENSE file for details.
97 -#ifndef CHROMAPRINT_AUDIO_FFMPEG_AUDIO_PROCESSOR_AVRESAMPLE_H_
98 -#define CHROMAPRINT_AUDIO_FFMPEG_AUDIO_PROCESSOR_AVRESAMPLE_H_
101 -#include <libavresample/avresample.h>
104 -namespace chromaprint {
106 -class FFmpegAudioProcessor {
108 - FFmpegAudioProcessor() {
109 - m_resample_ctx = avresample_alloc_context();
112 - ~FFmpegAudioProcessor() {
113 - avresample_free(&m_resample_ctx);
116 - void SetCompatibleMode() {
117 - av_opt_set_int(m_resample_ctx, "filter_size", 16, 0);
118 - av_opt_set_int(m_resample_ctx, "phase_shift", 8, 0);
119 - av_opt_set_int(m_resample_ctx, "linear_interp", 1, 0);
120 - av_opt_set_double(m_resample_ctx, "cutoff", 0.8, 0);
123 - void SetInputChannelLayout(int64_t channel_layout) {
124 - av_opt_set_int(m_resample_ctx, "in_channel_layout", channel_layout, 0);
127 - void SetInputSampleFormat(AVSampleFormat sample_format) {
128 - av_opt_set_int(m_resample_ctx, "in_sample_fmt", sample_format, 0);
131 - void SetInputSampleRate(int sample_rate) {
132 - av_opt_set_int(m_resample_ctx, "in_sample_rate", sample_rate, 0);
135 - void SetOutputChannelLayout(int64_t channel_layout) {
136 - av_opt_set_int(m_resample_ctx, "out_channel_layout", channel_layout, 0);
139 - void SetOutputSampleFormat(AVSampleFormat sample_format) {
140 - av_opt_set_int(m_resample_ctx, "out_sample_fmt", sample_format, 0);
143 - void SetOutputSampleRate(int sample_rate) {
144 - av_opt_set_int(m_resample_ctx, "out_sample_fmt", sample_rate, 0);
148 - return avresample_open(m_resample_ctx);
151 - int Convert(uint8_t **out, int out_count, const uint8_t **in, int in_count) {
152 - return avresample_convert(m_resample_ctx, out, 0, out_count, (uint8_t **) in, 0, in_count);
155 - int Flush(uint8_t **out, int out_count) {
156 - return avresample_read(m_resample_ctx, out, out_count);
160 - AVAudioResampleContext *m_resample_ctx = nullptr;
163 -}; // namespace chromaprint
166 diff --git a/src/audio/ffmpeg_audio_processor_swresample.h b/src/audio/ffmpeg_audio_processor_swresample.h
167 index b86266b..b1d4bea 100644
168 --- a/src/audio/ffmpeg_audio_processor_swresample.h
169 +++ b/src/audio/ffmpeg_audio_processor_swresample.h
170 @@ -28,30 +28,28 @@ class FFmpegAudioProcessor {
171 av_opt_set_double(m_swr_ctx, "cutoff", 0.8, 0);
174 - void SetInputChannelLayout(int64_t channel_layout) {
175 - av_opt_set_int(m_swr_ctx, "icl", channel_layout, 0);
176 - av_opt_set_int(m_swr_ctx, "ich", av_get_channel_layout_nb_channels(channel_layout), 0);
177 + void SetInputChannelLayout(AVChannelLayout *channel_layout) {
178 + av_opt_set_int(m_swr_ctx, "in_channel_layout", channel_layout->u.mask, 0);
181 void SetInputSampleFormat(AVSampleFormat sample_format) {
182 - av_opt_set_int(m_swr_ctx, "isf", sample_format, 0);
183 + av_opt_set_sample_fmt(m_swr_ctx, "in_sample_fmt", sample_format, 0);
186 void SetInputSampleRate(int sample_rate) {
187 - av_opt_set_int(m_swr_ctx, "isr", sample_rate, 0);
188 + av_opt_set_int(m_swr_ctx, "in_sample_rate", sample_rate, 0);
191 - void SetOutputChannelLayout(int64_t channel_layout) {
192 - av_opt_set_int(m_swr_ctx, "ocl", channel_layout, 0);
193 - av_opt_set_int(m_swr_ctx, "och", av_get_channel_layout_nb_channels(channel_layout), 0);
194 + void SetOutputChannelLayout(AVChannelLayout *channel_layout) {
195 + av_opt_set_int(m_swr_ctx, "out_channel_layout", channel_layout->u.mask, 0);
198 void SetOutputSampleFormat(AVSampleFormat sample_format) {
199 - av_opt_set_int(m_swr_ctx, "osf", sample_format, 0);
200 + av_opt_set_sample_fmt(m_swr_ctx, "out_sample_fmt", sample_format, 0);
203 void SetOutputSampleRate(int sample_rate) {
204 - av_opt_set_int(m_swr_ctx, "osr", sample_rate, 0);
205 + av_opt_set_int(m_swr_ctx, "out_sample_rate", sample_rate, 0);
209 diff --git a/src/audio/ffmpeg_audio_reader.h b/src/audio/ffmpeg_audio_reader.h
210 index 5550164..1c6b346 100644
211 --- a/src/audio/ffmpeg_audio_reader.h
212 +++ b/src/audio/ffmpeg_audio_reader.h
213 @@ -62,7 +62,7 @@ class FFmpegAudioReader {
214 bool Read(const int16_t **data, size_t *size);
216 bool IsOpen() const { return m_opened; }
217 - bool IsFinished() const { return m_finished && !m_got_frame; }
218 + bool IsFinished() const { return !m_has_more_packets && !m_has_more_frames; }
220 std::string GetError() const { return m_error; }
221 int GetErrorCode() const { return m_error_code; }
222 @@ -74,20 +74,19 @@ class FFmpegAudioReader {
223 uint8_t *m_convert_buffer[1] = { nullptr };
224 int m_convert_buffer_nb_samples = 0;
226 - AVInputFormat *m_input_fmt = nullptr;
227 + const AVInputFormat *m_input_fmt = nullptr;
228 AVDictionary *m_input_opts = nullptr;
230 AVFormatContext *m_format_ctx = nullptr;
231 AVCodecContext *m_codec_ctx = nullptr;
232 - AVFrame *m_frame = nullptr;
233 int m_stream_index = -1;
235 int m_error_code = 0;
236 - bool m_finished = false;
237 bool m_opened = false;
238 - int m_got_frame = 0;
240 - AVPacket m_packet0;
241 + bool m_has_more_packets = true;
242 + bool m_has_more_frames = true;
243 + AVPacket *m_packet = nullptr;
244 + AVFrame *m_frame = nullptr;
246 int m_output_sample_rate = 0;
247 int m_output_channels = 0;
248 @@ -98,19 +97,12 @@ class FFmpegAudioReader {
250 inline FFmpegAudioReader::FFmpegAudioReader() {
251 av_log_set_level(AV_LOG_QUIET);
253 - av_init_packet(&m_packet);
254 - m_packet.data = nullptr;
257 - m_packet0 = m_packet;
260 inline FFmpegAudioReader::~FFmpegAudioReader() {
262 av_dict_free(&m_input_opts);
263 av_freep(&m_convert_buffer[0]);
264 - av_packet_unref(&m_packet0);
267 inline bool FFmpegAudioReader::SetInputFormat(const char *name) {
268 @@ -135,11 +127,10 @@ inline bool FFmpegAudioReader::Open(const std::string &file_name) {
272 - av_init_packet(&m_packet);
273 - m_packet.data = nullptr;
276 - m_packet0 = m_packet;
277 + m_packet = av_packet_alloc();
282 ret = avformat_open_input(&m_format_ctx, file_name.c_str(), m_input_fmt, &m_input_opts);
284 @@ -153,26 +144,31 @@ inline bool FFmpegAudioReader::Open(const std::string &file_name) {
289 + const AVCodec *codec;
290 ret = av_find_best_stream(m_format_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
292 SetError("Could not find any audio stream in the file", ret);
295 m_stream_index = ret;
296 + auto stream = m_format_ctx->streams[m_stream_index];
298 - m_codec_ctx = m_format_ctx->streams[m_stream_index]->codec;
299 + m_codec_ctx = avcodec_alloc_context3(codec);
300 m_codec_ctx->request_sample_fmt = AV_SAMPLE_FMT_S16;
302 + ret = avcodec_parameters_to_context(m_codec_ctx, stream->codecpar);
304 + SetError("Could not copy the stream parameters", ret);
308 ret = avcodec_open2(m_codec_ctx, codec, nullptr);
310 SetError("Could not open the codec", ret);
314 - if (!m_codec_ctx->channel_layout) {
315 - m_codec_ctx->channel_layout = av_get_default_channel_layout(m_codec_ctx->channels);
317 + av_dump_format(m_format_ctx, 0, "foo", 0);
319 m_frame = av_frame_alloc();
321 @@ -183,19 +179,23 @@ inline bool FFmpegAudioReader::Open(const std::string &file_name) {
322 m_output_sample_rate = m_codec_ctx->sample_rate;
325 - if (!m_output_channels) {
326 - m_output_channels = m_codec_ctx->channels;
327 + AVChannelLayout output_channel_layout;
328 + if (m_output_channels) {
329 + av_channel_layout_default(&output_channel_layout, m_output_channels);
331 + m_output_channels = m_codec_ctx->ch_layout.nb_channels;
332 + av_channel_layout_default(&output_channel_layout, m_output_channels);
335 - if (m_codec_ctx->sample_fmt != AV_SAMPLE_FMT_S16 || m_codec_ctx->channels != m_output_channels || m_codec_ctx->sample_rate != m_output_sample_rate) {
336 + if (m_codec_ctx->sample_fmt != AV_SAMPLE_FMT_S16 || m_codec_ctx->ch_layout.nb_channels != m_output_channels || m_codec_ctx->sample_rate != m_output_sample_rate) {
337 m_converter.reset(new FFmpegAudioProcessor());
338 m_converter->SetCompatibleMode();
339 m_converter->SetInputSampleFormat(m_codec_ctx->sample_fmt);
340 m_converter->SetInputSampleRate(m_codec_ctx->sample_rate);
341 - m_converter->SetInputChannelLayout(m_codec_ctx->channel_layout);
342 + m_converter->SetInputChannelLayout(&(m_codec_ctx->ch_layout));
343 m_converter->SetOutputSampleFormat(AV_SAMPLE_FMT_S16);
344 m_converter->SetOutputSampleRate(m_output_sample_rate);
345 - m_converter->SetOutputChannelLayout(av_get_default_channel_layout(m_output_channels));
346 + m_converter->SetOutputChannelLayout(&output_channel_layout);
347 auto ret = m_converter->Init();
349 SetError("Could not create an audio converter instance", ret);
350 @@ -203,10 +203,11 @@ inline bool FFmpegAudioReader::Open(const std::string &file_name) {
354 + av_channel_layout_uninit(&output_channel_layout);
357 - m_finished = false;
360 + m_has_more_packets = true;
361 + m_has_more_frames = true;
365 @@ -214,6 +215,7 @@ inline bool FFmpegAudioReader::Open(const std::string &file_name) {
367 inline void FFmpegAudioReader::Close() {
368 av_frame_free(&m_frame);
369 + av_packet_free(&m_packet);
373 @@ -252,91 +254,98 @@ inline bool FFmpegAudioReader::Read(const int16_t **data, size_t *size) {
381 + bool needs_packet = false;
383 - while (m_packet.size <= 0) {
384 - av_packet_unref(&m_packet0);
385 - av_init_packet(&m_packet);
386 - m_packet.data = nullptr;
388 - ret = av_read_frame(m_format_ctx, &m_packet);
389 + while (needs_packet && m_packet->size == 0) {
390 + ret = av_read_frame(m_format_ctx, m_packet);
392 if (ret == AVERROR_EOF) {
394 + needs_packet = false;
395 + m_has_more_packets = false;
399 + SetError("Error reading from the audio source", ret);
402 + if (m_packet->stream_index == m_stream_index) {
403 + needs_packet = false;
405 + av_packet_unref(m_packet);
409 + if (m_packet->size != 0) {
410 + ret = avcodec_send_packet(m_codec_ctx, m_packet);
412 + if (ret != AVERROR(EAGAIN)) {
413 SetError("Error reading from the audio source", ret);
417 - m_packet0 = m_packet;
418 - if (m_packet.stream_index != m_stream_index) {
419 - m_packet.data = nullptr;
423 + av_packet_unref(m_packet);
427 - ret = avcodec_decode_audio4(m_codec_ctx, m_frame, &m_got_frame, &m_packet);
428 + ret = avcodec_receive_frame(m_codec_ctx, m_frame);
430 - if (m_decode_error) {
431 - SetError("Error decoding audio frame", m_decode_error);
433 + if (ret == AVERROR_EOF) {
434 + m_has_more_frames = false;
435 + } else if (ret == AVERROR(EAGAIN)) {
436 + if (m_has_more_packets) {
437 + needs_packet = true;
440 + m_has_more_frames = false;
443 - m_decode_error = ret;
444 - m_packet.data = nullptr;
447 + SetError("Error decoding the audio source", ret);
454 - m_decode_error = 0;
456 - const int decoded = std::min(ret, m_packet.size);
457 - m_packet.data += decoded;
458 - m_packet.size -= decoded;
462 - if (m_frame->nb_samples > m_convert_buffer_nb_samples) {
464 - av_freep(&m_convert_buffer[0]);
465 - m_convert_buffer_nb_samples = std::max(1024 * 8, m_frame->nb_samples);
466 - ret = av_samples_alloc(m_convert_buffer, &linsize, m_codec_ctx->channels, m_convert_buffer_nb_samples, AV_SAMPLE_FMT_S16, 1);
468 - SetError("Couldn't allocate audio converter buffer", ret);
469 + if (m_frame->nb_samples > 0) {
471 + if (m_frame->nb_samples > m_convert_buffer_nb_samples) {
473 + av_freep(&m_convert_buffer[0]);
474 + m_convert_buffer_nb_samples = std::max(1024 * 8, m_frame->nb_samples);
475 + ret = av_samples_alloc(m_convert_buffer, &linsize, m_codec_ctx->ch_layout.nb_channels, m_convert_buffer_nb_samples, AV_SAMPLE_FMT_S16, 1);
477 + SetError("Couldn't allocate audio converter buffer", ret);
481 + auto nb_samples = m_converter->Convert(m_convert_buffer, m_convert_buffer_nb_samples, (const uint8_t **) m_frame->data, m_frame->nb_samples);
482 + if (nb_samples < 0) {
483 + SetError("Couldn't convert audio", ret);
487 - auto nb_samples = m_converter->Convert(m_convert_buffer, m_convert_buffer_nb_samples, (const uint8_t **) m_frame->data, m_frame->nb_samples);
488 - if (nb_samples < 0) {
489 - SetError("Couldn't convert audio", ret);
492 - *data = (const int16_t *) m_convert_buffer[0];
493 - *size = nb_samples;
495 - *data = (const int16_t *) m_frame->data[0];
496 - *size = m_frame->nb_samples;
499 - if (m_finished && m_converter) {
500 - auto nb_samples = m_converter->Flush(m_convert_buffer, m_convert_buffer_nb_samples);
501 - if (nb_samples < 0) {
502 - SetError("Couldn't convert audio", ret);
504 - } else if (nb_samples > 0) {
506 *data = (const int16_t *) m_convert_buffer[0];
509 + *data = (const int16_t *) m_frame->data[0];
510 + *size = m_frame->nb_samples;
514 + if (IsFinished()) {
515 + auto nb_samples = m_converter->Flush(m_convert_buffer, m_convert_buffer_nb_samples);
516 + if (nb_samples < 0) {
517 + SetError("Couldn't convert audio", ret);
519 + } else if (nb_samples > 0) {
520 + *data = (const int16_t *) m_convert_buffer[0];
521 + *size = nb_samples;
533 inline void FFmpegAudioReader::SetError(const char *message, int errnum) {
534 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
535 index a2b517b..123e643 100644
536 --- a/tests/CMakeLists.txt
537 +++ b/tests/CMakeLists.txt
538 @@ -38,6 +38,12 @@ set(SRCS
541 set(SRCS ${SRCS} ../src/audio/ffmpeg_audio_reader_test.cpp)
542 + include_directories(
543 + ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}
544 + ${FFMPEG_LIBAVCODEC_INCLUDE_DIRS}
545 + ${FFMPEG_LIBAVUTIL_INCLUDE_DIRS}
546 + ${AUDIO_PROCESSOR_INCLUDE_DIRS}
548 link_libraries(fpcalc_libs)