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