2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/lib/filter_graph.cc
21 * @brief A graph of FFmpeg filters.
25 #include <libavfilter/avfiltergraph.h>
26 #include <libavfilter/buffersrc.h>
27 #include <libavfilter/avcodec.h>
28 #include <libavfilter/buffersink.h>
29 #include <libavformat/avio.h>
32 #include "filter_graph.h"
34 #include "exceptions.h"
37 #include "ffmpeg_decoder.h"
41 using std::stringstream;
44 using boost::shared_ptr;
47 /** Construct a FFmpegFilterGraph for the settings in a film.
49 * @param decoder Decoder that we are using.
50 * @param s Size of the images to process.
51 * @param p Pixel format of the images to process.
53 FFmpegFilterGraph::FFmpegFilterGraph (shared_ptr<Film> film, FFmpegDecoder* decoder, libdcp::Size s, AVPixelFormat p)
54 : _buffer_src_context (0)
55 , _buffer_sink_context (0)
59 _frame = av_frame_alloc ();
61 string filters = Filter::ffmpeg_strings (film->filters()).first;
62 if (!filters.empty ()) {
66 filters += crop_string (Position (film->crop().left, film->crop().top), film->cropped_size (decoder->native_size()));
68 AVFilterGraph* graph = avfilter_graph_alloc();
70 throw DecodeError (N_("could not create filter graph."));
73 AVFilter* buffer_src = avfilter_get_by_name(N_("buffer"));
74 if (buffer_src == 0) {
75 throw DecodeError (N_("could not find buffer src filter"));
78 AVFilter* buffer_sink = avfilter_get_by_name(N_("buffersink"));
79 if (buffer_sink == 0) {
80 throw DecodeError (N_("Could not create buffer sink filter"));
84 a << "video_size=" << _size.width << "x" << _size.height << ":"
85 << "pix_fmt=" << _pixel_format << ":"
86 << "time_base=" << decoder->time_base_numerator() << "/" << decoder->time_base_denominator() << ":"
87 << "pixel_aspect=" << decoder->sample_aspect_ratio_numerator() << "/" << decoder->sample_aspect_ratio_denominator();
91 if ((r = avfilter_graph_create_filter (&_buffer_src_context, buffer_src, "in", a.str().c_str(), 0, graph)) < 0) {
92 throw DecodeError (N_("could not create buffer source"));
95 AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
96 PixelFormat* pixel_fmts = new PixelFormat[2];
97 pixel_fmts[0] = _pixel_format;
98 pixel_fmts[1] = PIX_FMT_NONE;
99 sink_params->pixel_fmts = pixel_fmts;
101 if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, graph) < 0) {
102 throw DecodeError (N_("could not create buffer sink."));
105 av_free (sink_params);
107 AVFilterInOut* outputs = avfilter_inout_alloc ();
108 outputs->name = av_strdup(N_("in"));
109 outputs->filter_ctx = _buffer_src_context;
110 outputs->pad_idx = 0;
113 AVFilterInOut* inputs = avfilter_inout_alloc ();
114 inputs->name = av_strdup(N_("out"));
115 inputs->filter_ctx = _buffer_sink_context;
119 if (avfilter_graph_parse (graph, filters.c_str(), &inputs, &outputs, 0) < 0) {
120 throw DecodeError (N_("could not set up filter graph."));
123 if (avfilter_graph_config (graph, 0) < 0) {
124 throw DecodeError (N_("could not configure filter graph."));
127 /* XXX: leaking `inputs' / `outputs' ? */
130 FFmpegFilterGraph::~FFmpegFilterGraph ()
132 av_frame_free (&_frame);
135 /** Take an AVFrame and process it using our configured filters, returning a
136 * set of Images. Caller handles memory management of the input frame.
138 list<shared_ptr<Image> >
139 FFmpegFilterGraph::process (AVFrame* frame)
141 list<shared_ptr<Image> > images;
143 if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
144 throw DecodeError (N_("could not push buffer into filter chain."));
148 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
152 images.push_back (shared_ptr<Image> (new SimpleImage (_frame)));
153 av_frame_unref (_frame);
159 /** @param s Image size.
160 * @param p Pixel format.
161 * @return true if this chain can process images with `s' and `p', otherwise false.
164 FFmpegFilterGraph::can_process (libdcp::Size s, AVPixelFormat p) const
166 return (_size == s && _pixel_format == p);
169 list<shared_ptr<Image> >
170 EmptyFilterGraph::process (AVFrame* frame)
172 list<shared_ptr<Image> > im;
173 im.push_back (shared_ptr<Image> (new SimpleImage (frame)));
177 shared_ptr<FilterGraph>
178 filter_graph_factory (shared_ptr<Film> film, FFmpegDecoder* decoder, libdcp::Size size, AVPixelFormat pixel_format)
180 if (film->filters().empty() && film->crop() == Crop()) {
181 return shared_ptr<FilterGraph> (new EmptyFilterGraph);
184 return shared_ptr<FilterGraph> (new FFmpegFilterGraph (film, decoder, size, pixel_format));