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