Add const qualifier.
[dcpomatic.git] / src / lib / filter_graph.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file src/lib/filter_graph.cc
22  *  @brief A graph of FFmpeg filters.
23  */
24
25 #include "filter_graph.h"
26 #include "filter.h"
27 #include "exceptions.h"
28 #include "image.h"
29 #include "compose.hpp"
30 extern "C" {
31 #include <libavfilter/avfiltergraph.h>
32 #include <libavfilter/buffersrc.h>
33 #include <libavfilter/buffersink.h>
34 #include <libavformat/avio.h>
35 }
36 #include <iostream>
37
38 #include "i18n.h"
39
40 using std::string;
41 using std::list;
42 using std::pair;
43 using std::make_pair;
44 using std::cout;
45 using std::vector;
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 FilterGraph::FilterGraph ()
52         : _graph (0)
53         , _copy (false)
54         , _buffer_src_context (0)
55         , _buffer_sink_context (0)
56         , _frame (0)
57 {
58
59 }
60
61 void
62 FilterGraph::setup (vector<Filter const *> filters)
63 {
64         string const filters_string = Filter::ffmpeg_string (filters);
65         if (filters.empty ()) {
66                 _copy = true;
67                 return;
68         }
69
70         _frame = av_frame_alloc ();
71
72         _graph = avfilter_graph_alloc();
73         if (!_graph) {
74                 throw DecodeError (N_("could not create filter graph."));
75         }
76
77         AVFilter const * buffer_src = avfilter_get_by_name (src_name().c_str());
78         if (!buffer_src) {
79                 throw DecodeError (N_("could not find buffer src filter"));
80         }
81
82         AVFilter const * buffer_sink = avfilter_get_by_name (sink_name().c_str());
83         if (!buffer_sink) {
84                 throw DecodeError (N_("Could not create buffer sink filter"));
85         }
86
87         if (avfilter_graph_create_filter (&_buffer_src_context, buffer_src, N_("in"), src_parameters().c_str(), 0, _graph) < 0) {
88                 throw DecodeError (N_("could not create buffer source"));
89         }
90
91         void* sink_params = sink_parameters ();
92
93         if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, _graph) < 0) {
94                 throw DecodeError (N_("could not create buffer sink."));
95         }
96
97         av_free (sink_params);
98
99         AVFilterInOut* outputs = avfilter_inout_alloc ();
100         outputs->name = av_strdup(N_("in"));
101         outputs->filter_ctx = _buffer_src_context;
102         outputs->pad_idx = 0;
103         outputs->next = 0;
104
105         AVFilterInOut* inputs = avfilter_inout_alloc ();
106         inputs->name = av_strdup(N_("out"));
107         inputs->filter_ctx = _buffer_sink_context;
108         inputs->pad_idx = 0;
109         inputs->next = 0;
110
111         if (avfilter_graph_parse (_graph, filters_string.c_str(), inputs, outputs, 0) < 0) {
112                 throw DecodeError (N_("could not set up filter graph."));
113         }
114
115         if (avfilter_graph_config (_graph, 0) < 0) {
116                 throw DecodeError (N_("could not configure filter graph."));
117         }
118 }
119
120 FilterGraph::~FilterGraph ()
121 {
122         if (_frame) {
123                 av_frame_free (&_frame);
124         }
125
126         if (_graph) {
127                 avfilter_graph_free (&_graph);
128         }
129 }
130
131 AVFilterContext *
132 FilterGraph::get (string name)
133 {
134         return avfilter_graph_get_filter (_graph, name.c_str ());
135 }