f27befb035a814f1c677e9b6c48020b8a2e88526
[dcpomatic.git] / src / lib / audio_filter_graph.cc
1 /*
2     Copyright (C) 2015 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 #include "audio_filter_graph.h"
22 #include "audio_buffers.h"
23 #include "compose.hpp"
24 extern "C" {
25 #include <libavfilter/buffersink.h>
26 #include <libavfilter/buffersrc.h>
27 }
28 #include <boost/make_shared.hpp>
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::cout;
34 using boost::shared_ptr;
35 using boost::make_shared;
36
37 AudioFilterGraph::AudioFilterGraph (int sample_rate, int channels)
38         : _sample_rate (sample_rate)
39         , _channels (channels)
40 {
41         /* FFmpeg doesn't know any channel layouts for any counts between 8 and 16,
42            so we need to tell it we're using 16 channels if we are using more than 8.
43         */
44         if (_channels > 8) {
45                 _channel_layout = av_get_default_channel_layout (16);
46         } else {
47                 _channel_layout = av_get_default_channel_layout (_channels);
48         }
49
50         _in_frame = av_frame_alloc ();
51 }
52
53 AudioFilterGraph::~AudioFilterGraph()
54 {
55         av_frame_free (&_in_frame);
56 }
57
58 string
59 AudioFilterGraph::src_parameters () const
60 {
61         SafeStringStream a;
62
63         char buffer[64];
64         av_get_channel_layout_string (buffer, sizeof(buffer), 0, _channel_layout);
65
66         a << "time_base=1/1:sample_rate=" << _sample_rate << ":"
67           << "sample_fmt=" << av_get_sample_fmt_name(AV_SAMPLE_FMT_FLTP) << ":"
68           << "channel_layout=" << buffer;
69
70         return a.str ();
71 }
72
73 void *
74 AudioFilterGraph::sink_parameters () const
75 {
76         AVABufferSinkParams* sink_params = av_abuffersink_params_alloc ();
77
78         AVSampleFormat* sample_fmts = new AVSampleFormat[2];
79         sample_fmts[0] = AV_SAMPLE_FMT_FLTP;
80         sample_fmts[1] = AV_SAMPLE_FMT_NONE;
81         sink_params->sample_fmts = sample_fmts;
82
83         int64_t* channel_layouts = new int64_t[2];
84         channel_layouts[0] = _channel_layout;
85         channel_layouts[1] = -1;
86         sink_params->channel_layouts = channel_layouts;
87
88         sink_params->sample_rates = new int[2];
89         sink_params->sample_rates[0] = _sample_rate;
90         sink_params->sample_rates[1] = -1;
91
92         return sink_params;
93 }
94
95 string
96 AudioFilterGraph::src_name () const
97 {
98         return "abuffer";
99 }
100
101 string
102 AudioFilterGraph::sink_name () const
103 {
104         return "abuffersink";
105 }
106
107 void
108 AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
109 {
110         int const process_channels = av_get_channel_layout_nb_channels (_channel_layout);
111         DCPOMATIC_ASSERT (process_channels >= buffers->channels());
112
113         if (buffers->channels() < process_channels) {
114                 /* We are processing more data than we actually have (see the hack in
115                    the constructor) so we need to create new buffers with some extra
116                    silent channels.
117                 */
118                 shared_ptr<AudioBuffers> extended_buffers = make_shared<AudioBuffers> (process_channels, buffers->frames());
119                 for (int i = 0; i < buffers->channels(); ++i) {
120                         extended_buffers->copy_channel_from (buffers.get(), i, i);
121                 }
122                 for (int i = buffers->channels(); i < process_channels; ++i) {
123                         extended_buffers->make_silent (i);
124                 }
125
126                 buffers = extended_buffers;
127         }
128
129         _in_frame->extended_data = new uint8_t*[process_channels];
130         for (int i = 0; i < buffers->channels(); ++i) {
131                 if (i < AV_NUM_DATA_POINTERS) {
132                         _in_frame->data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
133                 }
134                 _in_frame->extended_data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
135         }
136
137         _in_frame->nb_samples = buffers->frames ();
138         _in_frame->format = AV_SAMPLE_FMT_FLTP;
139         _in_frame->sample_rate = _sample_rate;
140         _in_frame->channel_layout = _channel_layout;
141         _in_frame->channels = process_channels;
142
143         int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
144
145         delete[] _in_frame->extended_data;
146         /* Reset extended_data to its original value so that av_frame_free
147            does not try to free it.
148         */
149         _in_frame->extended_data = _in_frame->data;
150
151         if (r < 0) {
152                 char buffer[256];
153                 av_strerror (r, buffer, sizeof(buffer));
154                 throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), buffer));
155         }
156
157         while (true) {
158                 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
159                         break;
160                 }
161
162                 /* We don't extract audio data here, since the only use of this class
163                    is for ebur128.
164                 */
165
166                 av_frame_unref (_frame);
167         }
168 }