Optimise filter graphs with no filters.
authorCarl Hetherington <cth@carlh.net>
Tue, 1 Jul 2014 15:05:28 +0000 (16:05 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 1 Jul 2014 15:05:28 +0000 (16:05 +0100)
src/lib/filter_graph.cc
src/lib/filter_graph.h

index 8b259a12d01e26c1b126c9d1296ac2632c3ee10d..f6a6c4529df2376bc08f81a2adaf721d5516459d 100644 (file)
@@ -53,18 +53,21 @@ using dcp::Size;
  *  @param p Pixel format of the images to process.
  */
 FilterGraph::FilterGraph (shared_ptr<const FFmpegContent> content, dcp::Size s, AVPixelFormat p)
-       : _buffer_src_context (0)
+       : _copy (false)
+       , _buffer_src_context (0)
        , _buffer_sink_context (0)
        , _size (s)
        , _pixel_format (p)
+       , _frame (0)
 {
-       _frame = av_frame_alloc ();
-       
-       string filters = Filter::ffmpeg_string (content->filters());
+       string const filters = Filter::ffmpeg_string (content->filters());
        if (filters.empty ()) {
-               filters = "copy";
+               _copy = true;
+               return;
        }
 
+       _frame = av_frame_alloc ();
+       
        AVFilterGraph* graph = avfilter_graph_alloc();
        if (graph == 0) {
                throw DecodeError (N_("could not create filter graph."));
@@ -128,7 +131,9 @@ FilterGraph::FilterGraph (shared_ptr<const FFmpegContent> content, dcp::Size s,
 
 FilterGraph::~FilterGraph ()
 {
-       av_frame_free (&_frame);
+       if (_frame) {
+               av_frame_free (&_frame);
+       }
 }
 
 /** Take an AVFrame and process it using our configured filters, returning a
@@ -139,19 +144,23 @@ FilterGraph::process (AVFrame* frame)
 {
        list<pair<shared_ptr<Image>, int64_t> > images;
 
-       if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
-               throw DecodeError (N_("could not push buffer into filter chain."));
-       }
-
-       while (true) {
-               if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
-                       break;
+       if (_copy) {
+               images.push_back (make_pair (shared_ptr<Image> (new Image (frame)), av_frame_get_best_effort_timestamp (frame)));
+       } else {
+               if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
+                       throw DecodeError (N_("could not push buffer into filter chain."));
+               }
+               
+               while (true) {
+                       if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
+                               break;
+                       }
+                       
+                       images.push_back (make_pair (shared_ptr<Image> (new Image (_frame)), av_frame_get_best_effort_timestamp (_frame)));
+                       av_frame_unref (_frame);
                }
-
-               images.push_back (make_pair (shared_ptr<Image> (new Image (_frame)), av_frame_get_best_effort_timestamp (_frame)));
-               av_frame_unref (_frame);
        }
-       
+               
        return images;
 }
 
index 45ad5d99874a0ef5c95b21c045a8fd6dd9520a24..5b43c5512fb9fa127bf5c364e292a22d9a7e1ce0 100644 (file)
@@ -43,6 +43,8 @@ public:
        std::list<std::pair<boost::shared_ptr<Image>, int64_t> > process (AVFrame * frame);
 
 private:
+       /** true if this graph has no filters in, so it just copies stuff straight through */
+       bool _copy;
        AVFilterContext* _buffer_src_context;
        AVFilterContext* _buffer_sink_context;
        dcp::Size _size; ///< size of the images that this chain can process