Add VideoFilterGraph::process for Image.
authorCarl Hetherington <cth@carlh.net>
Thu, 6 Oct 2022 22:39:59 +0000 (00:39 +0200)
committerCarl Hetherington <cth@carlh.net>
Tue, 18 Oct 2022 18:25:57 +0000 (20:25 +0200)
src/lib/video_filter_graph.cc
src/lib/video_filter_graph.h

index 64f7deb192d31b4a584cc3aac41b0401340e4e77..26f858437173294e80cb1691b708ab6ae000254e 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "compose.hpp"
 #include "image.h"
+#include "scope_guard.h"
 #include "video_filter_graph.h"
 extern "C" {
 #include <libavfilter/buffersrc.h>
@@ -48,6 +49,49 @@ VideoFilterGraph::VideoFilterGraph (dcp::Size s, AVPixelFormat p, dcp::Fraction
 }
 
 
+list<shared_ptr<const Image>>
+VideoFilterGraph::process(shared_ptr<const Image> image)
+{
+       if (_copy) {
+               return { image };
+       }
+
+       auto frame = av_frame_alloc();
+       if (!frame) {
+               throw std::bad_alloc();
+       }
+
+       ScopeGuard sg = [&frame]() { av_frame_free(&frame); };
+
+       for (int i = 0; i < image->planes(); ++i) {
+               frame->data[i] = image->data()[i];
+               frame->linesize[i] = image->stride()[i];
+       }
+
+       frame->width = image->size().width;
+       frame->height = image->size().height;
+       frame->format = image->pixel_format();
+
+       int r = av_buffersrc_write_frame(_buffer_src_context, frame);
+       if (r < 0) {
+               throw DecodeError(String::compose(N_("could not push buffer into filter chain (%1)."), r));
+       }
+
+       list<shared_ptr<const Image>> images;
+
+       while (true) {
+               if (av_buffersink_get_frame(_buffer_sink_context, _frame) < 0) {
+                       break;
+               }
+
+               images.push_back(make_shared<Image>(_frame, Image::Alignment::PADDED));
+               av_frame_unref (_frame);
+       }
+
+       return images;
+}
+
+
 /** Take an AVFrame and process it using our configured filters, returning a
  *  set of Images.  Caller handles memory management of the input frame.
  */
index e120fb467f02c1d0fd075fdb88a04a4bae8e991b..1fb322282ec9195e34da463228c231f5b17c1744 100644 (file)
@@ -29,6 +29,7 @@ public:
 
        bool can_process (dcp::Size s, AVPixelFormat p) const;
        std::list<std::pair<std::shared_ptr<const Image>, int64_t>> process (AVFrame * frame);
+       std::list<std::shared_ptr<const Image>> process(std::shared_ptr<const Image> image);
 
 protected:
        std::string src_parameters () const override;