2 Copyright (C) 2012 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_decoder.h"
21 #include "audio_buffers.h"
22 #include "exceptions.h"
27 using std::stringstream;
28 using boost::optional;
29 using boost::shared_ptr;
31 AudioDecoder::AudioDecoder (shared_ptr<const Film> f, shared_ptr<const AudioContent> c)
35 , _output_audio_frame_rate (_audio_content->output_audio_frame_rate (f))
37 if (_audio_content->content_audio_frame_rate() != _output_audio_frame_rate) {
40 s << String::compose ("Will resample audio from %1 to %2", _audio_content->content_audio_frame_rate(), _output_audio_frame_rate);
41 _film->log()->log (s.str ());
43 /* We will be using planar float data when we call the
44 resampler. As far as I can see, the audio channel
45 layout is not necessary for our purposes; it seems
46 only to be used get the number of channels and
47 decide if rematrixing is needed. It won't be, since
48 input and output layouts are the same.
51 _swr_context = swr_alloc_set_opts (
53 av_get_default_channel_layout (MAX_AUDIO_CHANNELS),
55 _output_audio_frame_rate,
56 av_get_default_channel_layout (MAX_AUDIO_CHANNELS),
58 _audio_content->content_audio_frame_rate(),
62 swr_init (_swr_context);
68 AudioDecoder::~AudioDecoder ()
71 swr_free (&_swr_context);
78 AudioDecoder::process_end ()
82 shared_ptr<AudioBuffers> out (new AudioBuffers (_film->audio_mapping().dcp_channels(), 256));
85 int const frames = swr_convert (_swr_context, (uint8_t **) out->data(), 256, 0, 0);
88 throw EncodeError (_("could not run sample-rate converter"));
95 out->set_frames (frames);
104 AudioDecoder::emit_audio (shared_ptr<const AudioBuffers> data, Time time)
106 /* XXX: map audio to 5.1 */
111 /* Compute the resampled frames count and add 32 for luck */
112 int const max_resampled_frames = ceil ((int64_t) data->frames() * _output_audio_frame_rate / _audio_content->content_audio_frame_rate()) + 32;
114 shared_ptr<AudioBuffers> resampled (new AudioBuffers (MAX_AUDIO_CHANNELS, max_resampled_frames));
117 int const resampled_frames = swr_convert (
118 _swr_context, (uint8_t **) resampled->data(), max_resampled_frames, (uint8_t const **) data->data(), data->frames()
121 if (resampled_frames < 0) {
122 throw EncodeError (_("could not run sample-rate converter"));
125 resampled->set_frames (resampled_frames);
127 /* And point our variables at the resampled audio */
133 _next_audio = time + _film->audio_frames_to_time (data->frames());