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