2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "audio_filter_graph.h"
21 #include "audio_buffers.h"
22 #include "compose.hpp"
24 #include <libavfilter/buffersink.h>
25 #include <libavfilter/buffersrc.h>
32 using boost::shared_ptr;
34 AudioFilterGraph::AudioFilterGraph (int sample_rate, int64_t channel_layout)
35 : _sample_rate (sample_rate)
36 , _channel_layout (channel_layout)
38 _in_frame = av_frame_alloc ();
41 AudioFilterGraph::~AudioFilterGraph()
44 av_frame_free (&_in_frame);
49 AudioFilterGraph::src_parameters () const
54 av_get_channel_layout_string (buffer, sizeof(buffer), 0, _channel_layout);
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;
64 AudioFilterGraph::sink_parameters () const
66 AVABufferSinkParams* sink_params = av_abuffersink_params_alloc ();
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;
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;
78 sink_params->sample_rates = new int[2];
79 sink_params->sample_rates[0] = _sample_rate;
80 sink_params->sample_rates[1] = -1;
86 AudioFilterGraph::src_name () const
92 AudioFilterGraph::sink_name () const
98 AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
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));
105 _in_frame->extended_data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
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);
114 int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
116 delete[] _in_frame->extended_data;
120 av_strerror (r, buffer, sizeof(buffer));
121 throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), buffer));
125 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
129 /* We don't extract audio data here, since the only use of this class
133 av_frame_unref (_frame);