Missing files.
[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, int64_t channel_layout)
35         : _sample_rate (sample_rate)
36         , _channel_layout (channel_layout)
37 {
38         _in_frame = av_frame_alloc ();
39 }
40
41 AudioFilterGraph::~AudioFilterGraph()
42 {
43         if (_in_frame) {
44                 av_frame_free (&_in_frame);
45         }
46 }
47
48 string
49 AudioFilterGraph::src_parameters () const
50 {
51         SafeStringStream a;
52
53         char buffer[64];
54         av_get_channel_layout_string (buffer, sizeof(buffer), 0, _channel_layout);
55
56         a << "time_base=1/1:sample_rate=" << _sample_rate << ":"
57           << "sample_fmt=" << av_get_sample_fmt_name(AV_SAMPLE_FMT_FLTP) << ":"
58           << "channel_layout=" << buffer;
59
60         return a.str ();
61 }
62
63 void *
64 AudioFilterGraph::sink_parameters () const
65 {
66         AVABufferSinkParams* sink_params = av_abuffersink_params_alloc ();
67
68         AVSampleFormat* sample_fmts = new AVSampleFormat[2];
69         sample_fmts[0] = AV_SAMPLE_FMT_FLTP;
70         sample_fmts[1] = AV_SAMPLE_FMT_NONE;
71         sink_params->sample_fmts = sample_fmts;
72
73         int64_t* channel_layouts = new int64_t[2];
74         channel_layouts[0] = _channel_layout;
75         channel_layouts[1] = -1;
76         sink_params->channel_layouts = channel_layouts;
77
78         sink_params->sample_rates = new int[2];
79         sink_params->sample_rates[0] = _sample_rate;
80         sink_params->sample_rates[1] = -1;
81
82         return sink_params;
83 }
84
85 string
86 AudioFilterGraph::src_name () const
87 {
88         return "abuffer";
89 }
90
91 string
92 AudioFilterGraph::sink_name () const
93 {
94         return "abuffersink";
95 }
96
97 void
98 AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
99 {
100         _in_frame->extended_data = new uint8_t*[buffers->channels()];
101         for (int i = 0; i < buffers->channels(); ++i) {
102                 if (i < AV_NUM_DATA_POINTERS) {
103                         _in_frame->data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
104                 }
105                 _in_frame->extended_data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
106         }
107
108         _in_frame->nb_samples = buffers->frames ();
109         _in_frame->format = AV_SAMPLE_FMT_FLTP;
110         _in_frame->sample_rate = _sample_rate;
111         _in_frame->channel_layout = _channel_layout;
112         _in_frame->channels = av_get_channel_layout_nb_channels (_channel_layout);
113
114         int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
115
116         delete[] _in_frame->extended_data;
117
118         if (r < 0) {
119                 char buffer[256];
120                 av_strerror (r, buffer, sizeof(buffer));
121                 throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), buffer));
122         }
123
124         while (true) {
125                 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
126                         break;
127                 }
128
129                 /* We don't extract audio data here, since the only use of this class
130                    is for ebur128.
131                 */
132
133                 av_frame_unref (_frame);
134         }
135 }