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