Some more tidying up.
[dcpomatic.git] / src / lib / filter_graph.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/lib/filter_graph.cc
21  *  @brief A graph of FFmpeg filters.
22  */
23
24 extern "C" {
25 #include <libavfilter/avfiltergraph.h>
26 #ifdef HAVE_BUFFERSRC_H 
27 #include <libavfilter/buffersrc.h>
28 #endif  
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>
34 #endif
35 #include <libavformat/avio.h>
36 }
37 #include "decoder.h"
38 #include "filter_graph.h"
39 #include "ffmpeg_compatibility.h"
40 #include "filter.h"
41 #include "exceptions.h"
42 #include "image.h"
43 #include "film.h"
44 #include "ffmpeg_decoder.h"
45
46 #include "i18n.h"
47
48 using std::stringstream;
49 using std::string;
50 using std::list;
51 using boost::shared_ptr;
52 using boost::weak_ptr;
53 using libdcp::Size;
54
55 /** Construct a FilterGraph for the settings in a film.
56  *  @param film Film.
57  *  @param decoder Decoder that we are using.
58  *  @param s Size of the images to process.
59  *  @param p Pixel format of the images to process.
60  */
61 FilterGraph::FilterGraph (weak_ptr<const Film> weak_film, FFmpegDecoder* decoder, libdcp::Size s, AVPixelFormat p)
62         : _buffer_src_context (0)
63         , _buffer_sink_context (0)
64         , _size (s)
65         , _pixel_format (p)
66 {
67         shared_ptr<const Film> film = weak_film.lock ();
68         assert (film);
69         
70         string filters = Filter::ffmpeg_strings (film->filters()).first;
71         if (!filters.empty ()) {
72                 filters += N_(",");
73         }
74
75         Crop crop = decoder->ffmpeg_content()->crop ();
76         libdcp::Size cropped_size = decoder->video_size ();
77         cropped_size.width -= crop.left + crop.right;
78         cropped_size.height -= crop.top + crop.bottom;
79         filters += crop_string (Position (crop.left, crop.top), cropped_size);
80
81         AVFilterGraph* graph = avfilter_graph_alloc();
82         if (graph == 0) {
83                 throw DecodeError (N_("could not create filter graph."));
84         }
85
86         AVFilter* buffer_src = avfilter_get_by_name(N_("buffer"));
87         if (buffer_src == 0) {
88                 throw DecodeError (N_("could not find buffer src filter"));
89         }
90
91         AVFilter* buffer_sink = get_sink ();
92
93         stringstream a;
94         a << _size.width << N_(":")
95           << _size.height << N_(":")
96           << _pixel_format << N_(":")
97           << "0:1:0:1";
98
99         int r;
100
101         if ((r = avfilter_graph_create_filter (&_buffer_src_context, buffer_src, N_("in"), a.str().c_str(), 0, graph)) < 0) {
102                 throw DecodeError (N_("could not create buffer source"));
103         }
104
105         AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
106         PixelFormat* pixel_fmts = new PixelFormat[2];
107         pixel_fmts[0] = _pixel_format;
108         pixel_fmts[1] = PIX_FMT_NONE;
109         sink_params->pixel_fmts = pixel_fmts;
110         
111         if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, graph) < 0) {
112                 throw DecodeError (N_("could not create buffer sink."));
113         }
114
115         AVFilterInOut* outputs = avfilter_inout_alloc ();
116         outputs->name = av_strdup(N_("in"));
117         outputs->filter_ctx = _buffer_src_context;
118         outputs->pad_idx = 0;
119         outputs->next = 0;
120
121         AVFilterInOut* inputs = avfilter_inout_alloc ();
122         inputs->name = av_strdup(N_("out"));
123         inputs->filter_ctx = _buffer_sink_context;
124         inputs->pad_idx = 0;
125         inputs->next = 0;
126
127 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
128         if (avfilter_graph_parse (graph, filters.c_str(), inputs, outputs, 0) < 0) {
129                 throw DecodeError (N_("could not set up filter graph."));
130         }
131 #else   
132         if (avfilter_graph_parse (graph, filters.c_str(), &inputs, &outputs, 0) < 0) {
133                 throw DecodeError (N_("could not set up filter graph."));
134         }
135 #endif  
136         
137         if (avfilter_graph_config (graph, 0) < 0) {
138                 throw DecodeError (N_("could not configure filter graph."));
139         }
140
141         /* XXX: leaking `inputs' / `outputs' ? */
142 }
143
144 /** Take an AVFrame and process it using our configured filters, returning a
145  *  set of Images.
146  */
147 list<shared_ptr<Image> >
148 FilterGraph::process (AVFrame const * frame)
149 {
150         list<shared_ptr<Image> > images;
151         
152 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 53 && LIBAVFILTER_VERSION_MINOR <= 61
153
154         if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0) < 0) {
155                 throw DecodeError (N_("could not push buffer into filter chain."));
156         }
157
158 #elif LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
159
160         AVRational par;
161         par.num = sample_aspect_ratio_numerator ();
162         par.den = sample_aspect_ratio_denominator ();
163
164         if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0, par) < 0) {
165                 throw DecodeError (N_("could not push buffer into filter chain."));
166         }
167
168 #else
169
170         if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
171                 throw DecodeError (N_("could not push buffer into filter chain."));
172         }
173
174 #endif  
175         
176 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15 && LIBAVFILTER_VERSION_MINOR <= 61        
177         while (avfilter_poll_frame (_buffer_sink_context->inputs[0])) {
178 #else
179         while (av_buffersink_read (_buffer_sink_context, 0)) {
180 #endif          
181
182 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15
183                 
184                 int r = avfilter_request_frame (_buffer_sink_context->inputs[0]);
185                 if (r < 0) {
186                         throw DecodeError (N_("could not request filtered frame"));
187                 }
188                 
189                 AVFilterBufferRef* filter_buffer = _buffer_sink_context->inputs[0]->cur_buf;
190                 
191 #else
192
193                 AVFilterBufferRef* filter_buffer;
194                 if (av_buffersink_get_buffer_ref (_buffer_sink_context, &filter_buffer, 0) < 0) {
195                         filter_buffer = 0;
196                 }
197
198 #endif          
199                 
200                 if (filter_buffer) {
201                         /* This takes ownership of filter_buffer */
202                         images.push_back (shared_ptr<Image> (new FilterBufferImage ((PixelFormat) frame->format, filter_buffer)));
203                 }
204         }
205         
206         return images;
207 }
208
209 /** @param s Image size.
210  *  @param p Pixel format.
211  *  @return true if this chain can process images with `s' and `p', otherwise false.
212  */
213 bool
214 FilterGraph::can_process (libdcp::Size s, AVPixelFormat p) const
215 {
216         return (_size == s && _pixel_format == p);
217 }