Port filter graphs to new FFmpeg API.
authorCarl Hetherington <cth@carlh.net>
Fri, 30 Apr 2021 17:36:01 +0000 (19:36 +0200)
committerCarl Hetherington <cth@carlh.net>
Fri, 30 Apr 2021 23:31:57 +0000 (01:31 +0200)
src/lib/audio_filter_graph.cc
src/lib/audio_filter_graph.h
src/lib/filter_graph.cc
src/lib/filter_graph.h
src/lib/video_filter_graph.cc
src/lib/video_filter_graph.h

index 6318c68ab58fbced508595bd74cbac9c8632706c..fc43b5a344a972a9ef20d69e1257441024616412 100644 (file)
@@ -25,6 +25,7 @@ extern "C" {
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
 #include <libavutil/channel_layout.h>
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
 #include <libavutil/channel_layout.h>
+#include <libavutil/opt.h>
 }
 #include <iostream>
 
 }
 #include <iostream>
 
@@ -70,28 +71,24 @@ AudioFilterGraph::src_parameters () const
        return buffer;
 }
 
        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;
+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);
 
 
-       sink_params->sample_rates = new int[2];
-       sink_params->sample_rates[0] = _sample_rate;
-       sink_params->sample_rates[1] = -1;
+       int64_t channel_layouts[] = { _channel_layout, -1 };
+       r = av_opt_set_int_list (context, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN);
+       DCPOMATIC_ASSERT (r >= 0);
 
 
-       return sink_params;
+       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
 {
 string
 AudioFilterGraph::src_name () const
 {
index 91216c7c3657cb4c88b4ed4533c3ca650b28ef8d..af438a7e7eac19a9afd0fd0545990516614771ed 100644 (file)
@@ -34,10 +34,10 @@ public:
        void process (std::shared_ptr<const AudioBuffers> audio);
 
 protected:
        void process (std::shared_ptr<const AudioBuffers> audio);
 
 protected:
-       std::string src_parameters () const;
-       std::string src_name () const;
-       void* sink_parameters () const;
-       std::string sink_name () const;
+       std::string src_parameters () const override;
+       std::string src_name () const override;
+       void set_parameters (AVFilterContext* context) const override;
+       std::string sink_name () const override;
 
 private:
        int _sample_rate;
 
 private:
        int _sample_rate;
index 2a690eeb9aa276a79afe08508b0d864edc5af882..ba0e01ed534f131c7025f0a34c726cfe6f66ed15 100644 (file)
 
 */
 
 
 */
 
+
 /** @file src/lib/filter_graph.cc
  *  @brief A graph of FFmpeg filters.
  */
 
 /** @file src/lib/filter_graph.cc
  *  @brief A graph of FFmpeg filters.
  */
 
+
 #include "filter_graph.h"
 #include "filter.h"
 #include "exceptions.h"
 #include "filter_graph.h"
 #include "filter.h"
 #include "exceptions.h"
@@ -36,6 +38,7 @@ extern "C" {
 
 #include "i18n.h"
 
 
 #include "i18n.h"
 
+
 using std::string;
 using std::list;
 using std::pair;
 using std::string;
 using std::list;
 using std::pair;
@@ -46,6 +49,7 @@ using std::shared_ptr;
 using std::weak_ptr;
 using dcp::Size;
 
 using std::weak_ptr;
 using dcp::Size;
 
+
 /** Construct a FilterGraph for the settings in a piece of content */
 FilterGraph::FilterGraph ()
        : _graph (0)
 /** Construct a FilterGraph for the settings in a piece of content */
 FilterGraph::FilterGraph ()
        : _graph (0)
@@ -73,12 +77,12 @@ FilterGraph::setup (vector<Filter const *> filters)
                throw DecodeError (N_("could not create filter graph."));
        }
 
                throw DecodeError (N_("could not create filter graph."));
        }
 
-       AVFilter const * buffer_src = avfilter_get_by_name (src_name().c_str());
+       auto const buffer_src = avfilter_get_by_name (src_name().c_str());
        if (!buffer_src) {
                throw DecodeError (N_("could not find buffer src filter"));
        }
 
        if (!buffer_src) {
                throw DecodeError (N_("could not find buffer src filter"));
        }
 
-       AVFilter const * buffer_sink = avfilter_get_by_name (sink_name().c_str());
+       auto const buffer_sink = avfilter_get_by_name (sink_name().c_str());
        if (!buffer_sink) {
                throw DecodeError (N_("Could not create buffer sink filter"));
        }
        if (!buffer_sink) {
                throw DecodeError (N_("Could not create buffer sink filter"));
        }
@@ -87,21 +91,19 @@ FilterGraph::setup (vector<Filter const *> filters)
                throw DecodeError (N_("could not create buffer source"));
        }
 
                throw DecodeError (N_("could not create buffer source"));
        }
 
-       void* sink_params = sink_parameters ();
-
-       if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, _graph) < 0) {
+       if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), nullptr, nullptr, _graph) < 0) {
                throw DecodeError (N_("could not create buffer sink."));
        }
 
                throw DecodeError (N_("could not create buffer sink."));
        }
 
-       av_free (sink_params);
+       set_parameters (_buffer_sink_context);
 
 
-       AVFilterInOut* outputs = avfilter_inout_alloc ();
+       auto outputs = avfilter_inout_alloc ();
        outputs->name = av_strdup(N_("in"));
        outputs->filter_ctx = _buffer_src_context;
        outputs->pad_idx = 0;
        outputs->next = 0;
 
        outputs->name = av_strdup(N_("in"));
        outputs->filter_ctx = _buffer_src_context;
        outputs->pad_idx = 0;
        outputs->next = 0;
 
-       AVFilterInOut* inputs = avfilter_inout_alloc ();
+       auto inputs = avfilter_inout_alloc ();
        inputs->name = av_strdup(N_("out"));
        inputs->filter_ctx = _buffer_sink_context;
        inputs->pad_idx = 0;
        inputs->name = av_strdup(N_("out"));
        inputs->filter_ctx = _buffer_sink_context;
        inputs->pad_idx = 0;
@@ -113,10 +115,11 @@ FilterGraph::setup (vector<Filter const *> filters)
 
        int e = avfilter_graph_config (_graph, 0);
        if (e < 0) {
 
        int e = avfilter_graph_config (_graph, 0);
        if (e < 0) {
-               throw DecodeError (String::compose (N_("could not configure filter graph (%1)"), e));
+               throw DecodeError (String::compose(N_("could not configure filter graph (%1)"), e));
        }
 }
 
        }
 }
 
+
 FilterGraph::~FilterGraph ()
 {
        if (_frame) {
 FilterGraph::~FilterGraph ()
 {
        if (_frame) {
@@ -128,8 +131,9 @@ FilterGraph::~FilterGraph ()
        }
 }
 
        }
 }
 
+
 AVFilterContext *
 FilterGraph::get (string name)
 {
 AVFilterContext *
 FilterGraph::get (string name)
 {
-       return avfilter_graph_get_filter (_graph, name.c_str ());
+       return avfilter_graph_get_filter (_graph, name.c_str());
 }
 }
index 2b54d7829b87ef24d9f41e40adc05546fdf54f3d..eccfe4954ea1442e421f863f5a5462638c3c1325 100644 (file)
@@ -53,7 +53,7 @@ public:
 protected:
        virtual std::string src_parameters () const = 0;
        virtual std::string src_name () const = 0;
 protected:
        virtual std::string src_parameters () const = 0;
        virtual std::string src_name () const = 0;
-       virtual void* sink_parameters () const = 0;
+       virtual void set_parameters (AVFilterContext* context) const = 0;
        virtual std::string sink_name () const = 0;
 
        AVFilterGraph* _graph;
        virtual std::string sink_name () const = 0;
 
        AVFilterGraph* _graph;
index c24d9673d592b24b86679b8743e3634bf59b4f3b..a61da6773bbb8517330895aa2a8b01eae3e39c76 100644 (file)
@@ -26,6 +26,7 @@
 extern "C" {
 #include <libavfilter/buffersrc.h>
 #include <libavfilter/buffersink.h>
 extern "C" {
 #include <libavfilter/buffersrc.h>
 #include <libavfilter/buffersink.h>
+#include <libavutil/opt.h>
 }
 
 #include "i18n.h"
 }
 
 #include "i18n.h"
@@ -70,7 +71,7 @@ VideoFilterGraph::process (AVFrame* frame)
                                break;
                        }
 
                                break;
                        }
 
-                       images.push_back (make_pair(make_shared<Image>(_frame), _frame->best_effort_timestamp));
+                       images.push_back (make_pair(make_shared<Image>(_frame), frame->best_effort_timestamp));
                        av_frame_unref (_frame);
                }
        }
                        av_frame_unref (_frame);
                }
        }
@@ -105,15 +106,12 @@ VideoFilterGraph::src_parameters () const
 }
 
 
 }
 
 
-void *
-VideoFilterGraph::sink_parameters () const
+void
+VideoFilterGraph::set_parameters (AVFilterContext* context) const
 {
 {
-       auto sink_params = av_buffersink_params_alloc ();
-       auto pixel_fmts = new AVPixelFormat[2];
-       pixel_fmts[0] = _pixel_format;
-       pixel_fmts[1] = AV_PIX_FMT_NONE;
-       sink_params->pixel_fmts = pixel_fmts;
-       return sink_params;
+       AVPixelFormat pix_fmts[] = { _pixel_format, AV_PIX_FMT_NONE };
+       int r = av_opt_set_int_list (context, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
+       DCPOMATIC_ASSERT (r >= 0);
 }
 
 
 }
 
 
index fb6c7eba1162ef6b597d287843a6af880e596526..d887e551bda8e586b5bcedd67137048b0ca6a019 100644 (file)
@@ -31,10 +31,10 @@ public:
        std::list<std::pair<std::shared_ptr<Image>, int64_t>> process (AVFrame * frame);
 
 protected:
        std::list<std::pair<std::shared_ptr<Image>, int64_t>> process (AVFrame * frame);
 
 protected:
-       std::string src_parameters () const;
-       std::string src_name () const;
-       void* sink_parameters () const;
-       std::string sink_name () const;
+       std::string src_parameters () const override;
+       std::string src_name () const override;
+       void set_parameters (AVFilterContext* context) const override;
+       std::string sink_name () const override;
 
 private:
        dcp::Size _size; ///< size of the images that this chain can process
 
 private:
        dcp::Size _size; ///< size of the images that this chain can process