Build with FFmpeg 4.4 and switch to the new send/receive APIs.
[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
22 /** @file src/lib/filter_graph.cc
23  *  @brief A graph of FFmpeg filters.
24  */
25
26
27 #include "filter_graph.h"
28 #include "filter.h"
29 #include "exceptions.h"
30 #include "image.h"
31 #include "compose.hpp"
32 extern "C" {
33 #include <libavfilter/buffersrc.h>
34 #include <libavfilter/buffersink.h>
35 #include <libavformat/avio.h>
36 }
37 #include <iostream>
38
39 #include "i18n.h"
40
41
42 using std::string;
43 using std::list;
44 using std::pair;
45 using std::make_pair;
46 using std::cout;
47 using std::vector;
48 using std::shared_ptr;
49 using std::weak_ptr;
50 using dcp::Size;
51
52
53 /** Construct a FilterGraph for the settings in a piece of content */
54 FilterGraph::FilterGraph ()
55         : _graph (0)
56         , _copy (false)
57         , _buffer_src_context (0)
58         , _buffer_sink_context (0)
59         , _frame (0)
60 {
61
62 }
63
64 void
65 FilterGraph::setup (vector<Filter const *> filters)
66 {
67         string const filters_string = Filter::ffmpeg_string (filters);
68         if (filters.empty ()) {
69                 _copy = true;
70                 return;
71         }
72
73         _frame = av_frame_alloc ();
74
75         _graph = avfilter_graph_alloc();
76         if (!_graph) {
77                 throw DecodeError (N_("could not create filter graph."));
78         }
79
80         auto const buffer_src = avfilter_get_by_name (src_name().c_str());
81         if (!buffer_src) {
82                 throw DecodeError (N_("could not find buffer src filter"));
83         }
84
85         auto const buffer_sink = avfilter_get_by_name (sink_name().c_str());
86         if (!buffer_sink) {
87                 throw DecodeError (N_("Could not create buffer sink filter"));
88         }
89
90         if (avfilter_graph_create_filter (&_buffer_src_context, buffer_src, N_("in"), src_parameters().c_str(), 0, _graph) < 0) {
91                 throw DecodeError (N_("could not create buffer source"));
92         }
93
94         if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), nullptr, nullptr, _graph) < 0) {
95                 throw DecodeError (N_("could not create buffer sink."));
96         }
97
98         set_parameters (_buffer_sink_context);
99
100         auto outputs = avfilter_inout_alloc ();
101         outputs->name = av_strdup(N_("in"));
102         outputs->filter_ctx = _buffer_src_context;
103         outputs->pad_idx = 0;
104         outputs->next = 0;
105
106         auto inputs = avfilter_inout_alloc ();
107         inputs->name = av_strdup(N_("out"));
108         inputs->filter_ctx = _buffer_sink_context;
109         inputs->pad_idx = 0;
110         inputs->next = 0;
111
112         if (avfilter_graph_parse (_graph, filters_string.c_str(), inputs, outputs, 0) < 0) {
113                 throw DecodeError (N_("could not set up filter graph."));
114         }
115
116         int e = avfilter_graph_config (_graph, 0);
117         if (e < 0) {
118                 throw DecodeError (String::compose(N_("could not configure filter graph (%1)"), e));
119         }
120 }
121
122
123 FilterGraph::~FilterGraph ()
124 {
125         if (_frame) {
126                 av_frame_free (&_frame);
127         }
128
129         if (_graph) {
130                 avfilter_graph_free (&_graph);
131         }
132 }
133
134
135 AVFilterContext *
136 FilterGraph::get (string name)
137 {
138         return avfilter_graph_get_filter (_graph, name.c_str());
139 }