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