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 #ifdef HAVE_BUFFERSRC_H
27 #include <libavfilter/buffersrc.h>
29 #if (LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 53 && LIBAVFILTER_VERSION_MINOR <= 77) || LIBAVFILTER_VERSION_MAJOR == 3
30 #include <libavfilter/avcodec.h>
31 #include <libavfilter/buffersink.h>
32 #elif LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
33 #include <libavfilter/vsrc_buffer.h>
35 #include <libavformat/avio.h>
38 #include "filter_graph.h"
39 #include "ffmpeg_compatibility.h"
41 #include "exceptions.h"
44 #include "ffmpeg_decoder.h"
48 using std::stringstream;
51 using boost::shared_ptr;
54 /** Construct a FilterGraph for the settings in a film.
56 * @param decoder Decoder that we are using.
57 * @param s Size of the images to process.
58 * @param p Pixel format of the images to process.
60 FilterGraph::FilterGraph (shared_ptr<const Film> film, FFmpegDecoder* decoder, libdcp::Size s, AVPixelFormat p)
61 : _buffer_src_context (0)
62 , _buffer_sink_context (0)
66 string filters = Filter::ffmpeg_strings (film->filters()).first;
67 if (!filters.empty ()) {
71 filters += crop_string (Position (film->crop().left, film->crop().top), film->cropped_size (decoder->native_size()));
73 AVFilterGraph* graph = avfilter_graph_alloc();
75 throw DecodeError (N_("could not create filter graph."));
78 AVFilter* buffer_src = avfilter_get_by_name(N_("buffer"));
79 if (buffer_src == 0) {
80 throw DecodeError (N_("could not find buffer src filter"));
83 AVFilter* buffer_sink = get_sink ();
86 a << _size.width << N_(":")
87 << _size.height << N_(":")
88 << _pixel_format << N_(":")
89 << decoder->time_base_numerator() << N_(":")
90 << decoder->time_base_denominator() << N_(":")
91 << decoder->sample_aspect_ratio_numerator() << N_(":")
92 << decoder->sample_aspect_ratio_denominator();
96 if ((r = avfilter_graph_create_filter (&_buffer_src_context, buffer_src, N_("in"), a.str().c_str(), 0, graph)) < 0) {
97 throw DecodeError (N_("could not create buffer source"));
100 AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
101 PixelFormat* pixel_fmts = new PixelFormat[2];
102 pixel_fmts[0] = _pixel_format;
103 pixel_fmts[1] = PIX_FMT_NONE;
104 sink_params->pixel_fmts = pixel_fmts;
106 if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, graph) < 0) {
107 throw DecodeError (N_("could not create buffer sink."));
110 AVFilterInOut* outputs = avfilter_inout_alloc ();
111 outputs->name = av_strdup(N_("in"));
112 outputs->filter_ctx = _buffer_src_context;
113 outputs->pad_idx = 0;
116 AVFilterInOut* inputs = avfilter_inout_alloc ();
117 inputs->name = av_strdup(N_("out"));
118 inputs->filter_ctx = _buffer_sink_context;
122 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
123 if (avfilter_graph_parse (graph, filters.c_str(), inputs, outputs, 0) < 0) {
124 throw DecodeError (N_("could not set up filter graph."));
127 if (avfilter_graph_parse (graph, filters.c_str(), &inputs, &outputs, 0) < 0) {
128 throw DecodeError (N_("could not set up filter graph."));
132 if (avfilter_graph_config (graph, 0) < 0) {
133 throw DecodeError (N_("could not configure filter graph."));
136 /* XXX: leaking `inputs' / `outputs' ? */
139 /** Take an AVFrame and process it using our configured filters, returning a
142 list<shared_ptr<Image> >
143 FilterGraph::process (AVFrame const * frame)
145 list<shared_ptr<Image> > images;
147 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 53 && LIBAVFILTER_VERSION_MINOR <= 61
149 if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0) < 0) {
150 throw DecodeError (N_("could not push buffer into filter chain."));
153 #elif LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
156 par.num = sample_aspect_ratio_numerator ();
157 par.den = sample_aspect_ratio_denominator ();
159 if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0, par) < 0) {
160 throw DecodeError (N_("could not push buffer into filter chain."));
165 if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
166 throw DecodeError (N_("could not push buffer into filter chain."));
171 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15 && LIBAVFILTER_VERSION_MINOR <= 61
172 while (avfilter_poll_frame (_buffer_sink_context->inputs[0])) {
174 while (av_buffersink_read (_buffer_sink_context, 0)) {
177 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15
179 int r = avfilter_request_frame (_buffer_sink_context->inputs[0]);
181 throw DecodeError (N_("could not request filtered frame"));
184 AVFilterBufferRef* filter_buffer = _buffer_sink_context->inputs[0]->cur_buf;
188 AVFilterBufferRef* filter_buffer;
189 if (av_buffersink_get_buffer_ref (_buffer_sink_context, &filter_buffer, 0) < 0) {
196 /* This takes ownership of filter_buffer */
197 images.push_back (shared_ptr<Image> (new FilterBufferImage ((PixelFormat) frame->format, filter_buffer)));
204 /** @param s Image size.
205 * @param p Pixel format.
206 * @return true if this chain can process images with `s' and `p', otherwise false.
209 FilterGraph::can_process (libdcp::Size s, AVPixelFormat p) const
211 return (_size == s && _pixel_format == p);