Bump ffmpeg to 5.1.2 "Riemann"
[dcpomatic.git] / src / lib / audio_filter_graph.cc
index 6b70300fce195550eee6b2eec22b83d714ede4e1..cb888c16206459f50be0e80680f9df6acdad31f4 100644 (file)
 
 */
 
-#include "audio_filter_graph.h"
+
 #include "audio_buffers.h"
+#include "audio_filter_graph.h"
 #include "compose.hpp"
+#include "dcpomatic_assert.h"
+#include "exceptions.h"
 extern "C" {
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
+#include <libavutil/channel_layout.h>
+#include <libavutil/opt.h>
 }
+#include <iostream>
 
 #include "i18n.h"
 
-using std::string;
+
 using std::cout;
-using boost::shared_ptr;
+using std::make_shared;
+using std::shared_ptr;
+using std::string;
+
 
 AudioFilterGraph::AudioFilterGraph (int sample_rate, int channels)
        : _sample_rate (sample_rate)
@@ -39,13 +48,12 @@ AudioFilterGraph::AudioFilterGraph (int sample_rate, int channels)
        /* FFmpeg doesn't know any channel layouts for any counts between 8 and 16,
           so we need to tell it we're using 16 channels if we are using more than 8.
        */
-       if (_channels > 8) {
-               _channel_layout = av_get_default_channel_layout (16);
-       } else {
-               _channel_layout = av_get_default_channel_layout (_channels);
-       }
+       av_channel_layout_default(&_channel_layout, _channels > 8 ? 16 : _channels);
 
        _in_frame = av_frame_alloc ();
+       if (_in_frame == nullptr) {
+               throw std::bad_alloc();
+       }
 }
 
 AudioFilterGraph::~AudioFilterGraph()
@@ -56,40 +64,37 @@ AudioFilterGraph::~AudioFilterGraph()
 string
 AudioFilterGraph::src_parameters () const
 {
-       locked_stringstream a;
+       char layout[64];
+       av_channel_layout_describe(&_channel_layout, layout, sizeof(layout));
 
-       char buffer[64];
-       av_get_channel_layout_string (buffer, sizeof(buffer), 0, _channel_layout);
+       char buffer[256];
+       snprintf (
+               buffer, sizeof(buffer), "time_base=1/1:sample_rate=%d:sample_fmt=%s:channel_layout=%s",
+               _sample_rate, av_get_sample_fmt_name(AV_SAMPLE_FMT_FLTP), layout
+               );
 
-       a << "time_base=1/1:sample_rate=" << _sample_rate << ":"
-         << "sample_fmt=" << av_get_sample_fmt_name(AV_SAMPLE_FMT_FLTP) << ":"
-         << "channel_layout=" << buffer;
-
-       return a.str ();
+       return buffer;
 }
 
-void *
-AudioFilterGraph::sink_parameters () const
-{
-       AVABufferSinkParams* sink_params = av_abuffersink_params_alloc ();
-
-       AVSampleFormat* sample_fmts = new AVSampleFormat[2];
-       sample_fmts[0] = AV_SAMPLE_FMT_FLTP;
-       sample_fmts[1] = AV_SAMPLE_FMT_NONE;
-       sink_params->sample_fmts = sample_fmts;
-
-       int64_t* channel_layouts = new int64_t[2];
-       channel_layouts[0] = _channel_layout;
-       channel_layouts[1] = -1;
-       sink_params->channel_layouts = channel_layouts;
 
-       sink_params->sample_rates = new int[2];
-       sink_params->sample_rates[0] = _sample_rate;
-       sink_params->sample_rates[1] = -1;
-
-       return sink_params;
+void
+AudioFilterGraph::set_parameters (AVFilterContext* context) const
+{
+       AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
+       int r = av_opt_set_int_list (context, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
+       DCPOMATIC_ASSERT (r >= 0);
+
+       char ch_layout[64];
+       av_channel_layout_describe(&_channel_layout, ch_layout, sizeof(ch_layout));
+       r = av_opt_set(context, "ch_layouts", ch_layout, AV_OPT_SEARCH_CHILDREN);
+       DCPOMATIC_ASSERT (r >= 0);
+
+       int sample_rates[] = { _sample_rate, -1 };
+       r = av_opt_set_int_list (context, "sample_rates", sample_rates, -1, AV_OPT_SEARCH_CHILDREN);
+       DCPOMATIC_ASSERT (r >= 0);
 }
 
+
 string
 AudioFilterGraph::src_name () const
 {
@@ -103,9 +108,10 @@ AudioFilterGraph::sink_name () const
 }
 
 void
-AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
+AudioFilterGraph::process (shared_ptr<AudioBuffers> buffers)
 {
-       int const process_channels = av_get_channel_layout_nb_channels (_channel_layout);
+       DCPOMATIC_ASSERT (buffers->frames() > 0);
+       int const process_channels = _channel_layout.nb_channels;
        DCPOMATIC_ASSERT (process_channels >= buffers->channels());
 
        if (buffers->channels() < process_channels) {
@@ -113,7 +119,7 @@ AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
                   the constructor) so we need to create new buffers with some extra
                   silent channels.
                */
-               shared_ptr<AudioBuffers> extended_buffers (new AudioBuffers (process_channels, buffers->frames()));
+               auto extended_buffers = make_shared<AudioBuffers>(process_channels, buffers->frames());
                for (int i = 0; i < buffers->channels(); ++i) {
                        extended_buffers->copy_channel_from (buffers.get(), i, i);
                }
@@ -135,8 +141,10 @@ AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
        _in_frame->nb_samples = buffers->frames ();
        _in_frame->format = AV_SAMPLE_FMT_FLTP;
        _in_frame->sample_rate = _sample_rate;
-       _in_frame->channel_layout = _channel_layout;
+       _in_frame->ch_layout = _channel_layout;
+LIBDCP_DISABLE_WARNINGS
        _in_frame->channels = process_channels;
+LIBDCP_ENABLE_WARNINGS
 
        int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
 
@@ -149,7 +157,7 @@ AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
        if (r < 0) {
                char buffer[256];
                av_strerror (r, buffer, sizeof(buffer));
-               throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), buffer));
+               throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), &buffer[0]));
        }
 
        while (true) {