2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "compose.hpp"
23 #include "video_filter_graph.h"
26 #include <libavfilter/buffersrc.h>
27 #include <libavfilter/buffersink.h>
37 using std::shared_ptr;
39 VideoFilterGraph::VideoFilterGraph (dcp::Size s, AVPixelFormat p, dcp::Fraction r)
47 /** Take an AVFrame and process it using our configured filters, returning a
48 * set of Images. Caller handles memory management of the input frame.
50 list<pair<shared_ptr<Image>, int64_t> >
51 VideoFilterGraph::process (AVFrame* frame)
53 list<pair<shared_ptr<Image>, int64_t> > images;
55 DCPOMATIC_DISABLE_WARNINGS
57 images.push_back (make_pair (shared_ptr<Image> (new Image (frame)), av_frame_get_best_effort_timestamp (frame)));
59 int r = av_buffersrc_write_frame (_buffer_src_context, frame);
61 throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)."), r));
65 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
69 images.push_back (make_pair (shared_ptr<Image> (new Image (_frame)), av_frame_get_best_effort_timestamp (_frame)));
70 av_frame_unref (_frame);
73 DCPOMATIC_ENABLE_WARNINGS
78 /** @param s Image size.
79 * @param p Pixel format.
80 * @return true if this chain can process images with `s' and `p', otherwise false.
83 VideoFilterGraph::can_process (dcp::Size s, AVPixelFormat p) const
85 return (_size == s && _pixel_format == p);
89 VideoFilterGraph::src_parameters () const
93 buffer, sizeof(buffer),
94 "video_size=%dx%d:pix_fmt=%d:frame_rate=%d/%d:time_base=1/1:pixel_aspect=1/1",
95 _size.width, _size.height,
97 _frame_rate.numerator, _frame_rate.denominator
103 VideoFilterGraph::sink_parameters () const
105 AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
106 AVPixelFormat* pixel_fmts = new AVPixelFormat[2];
107 pixel_fmts[0] = _pixel_format;
108 pixel_fmts[1] = AV_PIX_FMT_NONE;
109 sink_params->pixel_fmts = pixel_fmts;
114 VideoFilterGraph::src_name () const
120 VideoFilterGraph::sink_name () const