Merge master.
[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 #include <libavfilter/avcodec.h>
28 #include <libavfilter/buffersink.h>
29 #include <libavformat/avio.h>
30 }
31 #include "decoder.h"
32 #include "filter_graph.h"
33 #include "filter.h"
34 #include "exceptions.h"
35 #include "image.h"
36 #include "ffmpeg_content.h"
37 #include "safe_stringstream.h"
38
39 #include "i18n.h"
40
41 using std::string;
42 using std::list;
43 using std::pair;
44 using std::make_pair;
45 using std::cout;
46 using boost::shared_ptr;
47 using boost::weak_ptr;
48 using dcp::Size;
49
50 /** Construct a FilterGraph for the settings in a piece of content.
51  *  @param content Content.
52  *  @param s Size of the images to process.
53  *  @param p Pixel format of the images to process.
54  */
55 FilterGraph::FilterGraph (shared_ptr<const FFmpegContent> content, dcp::Size s, AVPixelFormat p)
56         : _copy (false)
57         , _buffer_src_context (0)
58         , _buffer_sink_context (0)
59         , _size (s)
60         , _pixel_format (p)
61         , _frame (0)
62 {
63         string const filters = Filter::ffmpeg_string (content->filters());
64         if (filters.empty ()) {
65                 _copy = true;
66                 return;
67         }
68
69         _frame = av_frame_alloc ();
70         
71         AVFilterGraph* graph = avfilter_graph_alloc();
72         if (graph == 0) {
73                 throw DecodeError (N_("could not create filter graph."));
74         }
75
76         AVFilter* buffer_src = avfilter_get_by_name(N_("buffer"));
77         if (buffer_src == 0) {
78                 throw DecodeError (N_("could not find buffer src filter"));
79         }
80
81         AVFilter* buffer_sink = avfilter_get_by_name(N_("buffersink"));
82         if (buffer_sink == 0) {
83                 throw DecodeError (N_("Could not create buffer sink filter"));
84         }
85
86         SafeStringStream a;
87         a << "video_size=" << _size.width << "x" << _size.height << ":"
88           << "pix_fmt=" << _pixel_format << ":"
89           << "time_base=1/1:"
90           << "pixel_aspect=1/1";
91
92         if (avfilter_graph_create_filter (&_buffer_src_context, buffer_src, "in", a.str().c_str(), 0, graph) < 0) {
93                 throw DecodeError (N_("could not create buffer source"));
94         }
95
96         AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
97         PixelFormat* pixel_fmts = new PixelFormat[2];
98         pixel_fmts[0] = _pixel_format;
99         pixel_fmts[1] = PIX_FMT_NONE;
100         sink_params->pixel_fmts = pixel_fmts;
101         
102         if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, graph) < 0) {
103                 throw DecodeError (N_("could not create buffer sink."));
104         }
105
106         av_free (sink_params);
107
108         AVFilterInOut* outputs = avfilter_inout_alloc ();
109         outputs->name = av_strdup(N_("in"));
110         outputs->filter_ctx = _buffer_src_context;
111         outputs->pad_idx = 0;
112         outputs->next = 0;
113
114         AVFilterInOut* inputs = avfilter_inout_alloc ();
115         inputs->name = av_strdup(N_("out"));
116         inputs->filter_ctx = _buffer_sink_context;
117         inputs->pad_idx = 0;
118         inputs->next = 0;
119
120         if (avfilter_graph_parse (graph, filters.c_str(), inputs, outputs, 0) < 0) {
121                 throw DecodeError (N_("could not set up filter graph."));
122         }
123         
124         if (avfilter_graph_config (graph, 0) < 0) {
125                 throw DecodeError (N_("could not configure filter graph."));
126         }
127
128         avfilter_inout_free (&inputs);
129         avfilter_inout_free (&outputs);
130 }
131
132 FilterGraph::~FilterGraph ()
133 {
134         if (_frame) {
135                 av_frame_free (&_frame);
136         }
137 }
138
139 /** Take an AVFrame and process it using our configured filters, returning a
140  *  set of Images.  Caller handles memory management of the input frame.
141  */
142 list<pair<shared_ptr<Image>, int64_t> >
143 FilterGraph::process (AVFrame* frame)
144 {
145         list<pair<shared_ptr<Image>, int64_t> > images;
146
147         if (_copy) {
148                 images.push_back (make_pair (shared_ptr<Image> (new Image (frame)), av_frame_get_best_effort_timestamp (frame)));
149         } else {
150                 if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
151                         throw DecodeError (N_("could not push buffer into filter chain."));
152                 }
153                 
154                 while (true) {
155                         if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
156                                 break;
157                         }
158                         
159                         images.push_back (make_pair (shared_ptr<Image> (new Image (_frame)), av_frame_get_best_effort_timestamp (_frame)));
160                         av_frame_unref (_frame);
161                 }
162         }
163                 
164         return images;
165 }
166
167 /** @param s Image size.
168  *  @param p Pixel format.
169  *  @return true if this chain can process images with `s' and `p', otherwise false.
170  */
171 bool
172 FilterGraph::can_process (dcp::Size s, AVPixelFormat p) const
173 {
174         return (_size == s && _pixel_format == p);
175 }