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.
21 #include <libavfilter/avfiltergraph.h>
22 #include <libavfilter/buffersrc.h>
23 #if (LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 53 && LIBAVFILTER_VERSION_MINOR <= 77) || LIBAVFILTER_VERSION_MAJOR == 3
24 #include <libavfilter/avcodec.h>
25 #include <libavfilter/buffersink.h>
26 #elif LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
27 #include <libavfilter/vsrc_buffer.h>
29 #include <libavformat/avio.h>
33 #include "filter_graph.h"
34 #include "ffmpeg_compatibility.h"
36 #include "exceptions.h"
39 using std::stringstream;
42 using boost::shared_ptr;
44 FilterGraph::FilterGraph (shared_ptr<Film> film, Decoder* decoder, bool crop, Size s, AVPixelFormat p)
45 : _buffer_src_context (0)
46 , _buffer_sink_context (0)
54 size_after_crop = film->cropped_size (decoder->native_size ());
55 fs << crop_string (Position (film->crop().left, film->crop().top), size_after_crop);
57 size_after_crop = decoder->native_size ();
58 fs << crop_string (Position (0, 0), size_after_crop);
61 string filters = Filter::ffmpeg_strings (film->filters()).first;
62 if (!filters.empty ()) {
68 avfilter_register_all ();
70 AVFilterGraph* graph = avfilter_graph_alloc();
72 throw DecodeError ("Could not create filter graph.");
75 AVFilter* buffer_src = avfilter_get_by_name("buffer");
76 if (buffer_src == 0) {
77 throw DecodeError ("Could not find buffer src filter");
80 AVFilter* buffer_sink = get_sink ();
83 a << _size.width << ":"
84 << _size.height << ":"
85 << _pixel_format << ":"
86 << decoder->time_base_numerator() << ":"
87 << decoder->time_base_denominator() << ":"
88 << decoder->sample_aspect_ratio_numerator() << ":"
89 << decoder->sample_aspect_ratio_denominator();
93 if ((r = avfilter_graph_create_filter (&_buffer_src_context, buffer_src, "in", a.str().c_str(), 0, graph)) < 0) {
94 throw DecodeError ("could not create buffer source");
97 AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
98 PixelFormat* pixel_fmts = new PixelFormat[2];
99 pixel_fmts[0] = _pixel_format;
100 pixel_fmts[1] = PIX_FMT_NONE;
101 sink_params->pixel_fmts = pixel_fmts;
103 if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, "out", 0, sink_params, graph) < 0) {
104 throw DecodeError ("could not create buffer sink.");
107 AVFilterInOut* outputs = avfilter_inout_alloc ();
108 outputs->name = av_strdup("in");
109 outputs->filter_ctx = _buffer_src_context;
110 outputs->pad_idx = 0;
113 AVFilterInOut* inputs = avfilter_inout_alloc ();
114 inputs->name = av_strdup("out");
115 inputs->filter_ctx = _buffer_sink_context;
119 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
120 if (avfilter_graph_parse (graph, filters.c_str(), inputs, outputs, 0) < 0) {
121 throw DecodeError ("could not set up filter graph.");
124 if (avfilter_graph_parse (graph, filters.c_str(), &inputs, &outputs, 0) < 0) {
125 throw DecodeError ("could not set up filter graph.");
129 if (avfilter_graph_config (graph, 0) < 0) {
130 throw DecodeError ("could not configure filter graph.");
133 /* XXX: leaking `inputs' / `outputs' ? */
136 list<shared_ptr<Image> >
137 FilterGraph::process (AVFrame const * frame)
139 list<shared_ptr<Image> > images;
141 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 53 && LIBAVFILTER_VERSION_MINOR <= 61
143 if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0) < 0) {
144 throw DecodeError ("could not push buffer into filter chain.");
147 #elif LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
150 par.num = sample_aspect_ratio_numerator ();
151 par.den = sample_aspect_ratio_denominator ();
153 if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0, par) < 0) {
154 throw DecodeError ("could not push buffer into filter chain.");
159 if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
160 throw DecodeError ("could not push buffer into filter chain.");
165 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15 && LIBAVFILTER_VERSION_MINOR <= 61
166 while (avfilter_poll_frame (_buffer_sink_context->inputs[0])) {
168 while (av_buffersink_read (_buffer_sink_context, 0)) {
171 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15
173 int r = avfilter_request_frame (_buffer_sink_context->inputs[0]);
175 throw DecodeError ("could not request filtered frame");
178 AVFilterBufferRef* filter_buffer = _buffer_sink_context->inputs[0]->cur_buf;
182 AVFilterBufferRef* filter_buffer;
183 if (av_buffersink_get_buffer_ref (_buffer_sink_context, &filter_buffer, 0) < 0) {
190 /* This takes ownership of filter_buffer */
191 images.push_back (shared_ptr<Image> (new FilterBufferImage ((PixelFormat) frame->format, filter_buffer)));
199 FilterGraph::can_process (Size s, AVPixelFormat p) const
201 return (_size == s && _pixel_format == p);