243e9e97e40764f798ab62f8f51dd8d0d52439cd
[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 "safe_stringstream.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  *  @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 ()
56         : _graph (0)
57         , _copy (false)
58         , _buffer_src_context (0)
59         , _buffer_sink_context (0)
60         , _frame (0)
61 {
62
63 }
64
65 void
66 FilterGraph::setup (vector<Filter const *> filters)
67 {
68         string const filters_string = Filter::ffmpeg_string (filters);
69         if (filters.empty ()) {
70                 _copy = true;
71                 return;
72         }
73
74         _frame = av_frame_alloc ();
75
76         _graph = avfilter_graph_alloc();
77         if (!_graph) {
78                 throw DecodeError (N_("could not create filter graph."));
79         }
80
81         AVFilter* buffer_src = avfilter_get_by_name (src_name().c_str());
82         if (!buffer_src) {
83                 throw DecodeError (N_("could not find buffer src filter"));
84         }
85
86         AVFilter* buffer_sink = avfilter_get_by_name (sink_name().c_str());
87         if (!buffer_sink) {
88                 throw DecodeError (N_("Could not create buffer sink filter"));
89         }
90
91         if (avfilter_graph_create_filter (&_buffer_src_context, buffer_src, N_("in"), src_parameters().c_str(), 0, _graph) < 0) {
92                 throw DecodeError (N_("could not create buffer source"));
93         }
94
95         void* sink_params = sink_parameters ();
96
97         if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, _graph) < 0) {
98                 throw DecodeError (N_("could not create buffer sink."));
99         }
100
101         av_free (sink_params);
102
103         AVFilterInOut* outputs = avfilter_inout_alloc ();
104         outputs->name = av_strdup(N_("in"));
105         outputs->filter_ctx = _buffer_src_context;
106         outputs->pad_idx = 0;
107         outputs->next = 0;
108
109         AVFilterInOut* inputs = avfilter_inout_alloc ();
110         inputs->name = av_strdup(N_("out"));
111         inputs->filter_ctx = _buffer_sink_context;
112         inputs->pad_idx = 0;
113         inputs->next = 0;
114
115         if (avfilter_graph_parse (_graph, filters_string.c_str(), inputs, outputs, 0) < 0) {
116                 throw DecodeError (N_("could not set up filter graph."));
117         }
118
119         if (avfilter_graph_config (_graph, 0) < 0) {
120                 throw DecodeError (N_("could not configure filter graph."));
121         }
122 }
123
124 FilterGraph::~FilterGraph ()
125 {
126         if (_frame) {
127                 av_frame_free (&_frame);
128         }
129
130         if (_graph) {
131                 avfilter_graph_free (&_graph);
132         }
133 }
134
135 AVFilterContext *
136 FilterGraph::get (string name)
137 {
138         return avfilter_graph_get_filter (_graph, name.c_str ());
139 }