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