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