2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "audio_decoder.h"
22 #include "audio_buffers.h"
23 #include "audio_content.h"
24 #include "dcpomatic_log.h"
26 #include "resampler.h"
27 #include "compose.hpp"
28 #include <boost/foreach.hpp>
36 using boost::shared_ptr;
37 using boost::optional;
39 AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, bool fast)
40 : DecoderPart (parent)
44 /* Set up _positions so that we have one for each stream */
45 BOOST_FOREACH (AudioStreamPtr i, content->streams ()) {
51 AudioDecoder::emit (shared_ptr<const Film> film, AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time)
57 if (_positions[stream] == 0) {
58 /* This is the first data we have received since initialisation or seek. Set
59 the position based on the ContentTime that was given. After this first time
60 we just count samples, as it seems that ContentTimes are unreliable from
61 FFmpegDecoder (not quite continuous; perhaps due to some rounding error).
63 if (_content->delay() > 0) {
64 /* Insert silence to give the delay */
65 silence (_content->delay ());
67 time += ContentTime::from_seconds (_content->delay() / 1000.0);
68 _positions[stream] = time.frames_round (_content->resampled_frame_rate(film));
71 shared_ptr<Resampler> resampler;
72 ResamplerMap::iterator i = _resamplers.find(stream);
73 if (i != _resamplers.end ()) {
74 resampler = i->second;
76 if (stream->frame_rate() != _content->resampled_frame_rate(film)) {
78 "Creating new resampler from %1 to %2 with %3 channels",
80 _content->resampled_frame_rate(film),
84 resampler.reset (new Resampler (stream->frame_rate(), _content->resampled_frame_rate(film), stream->channels()));
86 resampler->set_fast ();
88 _resamplers[stream] = resampler;
93 shared_ptr<const AudioBuffers> ro = resampler->run (data);
94 if (ro->frames() == 0) {
100 Data(stream, ContentAudio (data, _positions[stream]));
101 _positions[stream] += data->frames();
104 /** @return Time just after the last thing that was emitted from a given stream */
106 AudioDecoder::stream_position (shared_ptr<const Film> film, AudioStreamPtr stream) const
108 PositionMap::const_iterator i = _positions.find (stream);
109 DCPOMATIC_ASSERT (i != _positions.end ());
110 return ContentTime::from_frames (i->second, _content->resampled_frame_rate(film));
114 AudioDecoder::position (shared_ptr<const Film> film) const
116 optional<ContentTime> p;
117 for (PositionMap::const_iterator i = _positions.begin(); i != _positions.end(); ++i) {
118 ContentTime const ct = stream_position (film, i->first);
124 return p.get_value_or(ContentTime());
128 AudioDecoder::seek ()
130 for (ResamplerMap::iterator i = _resamplers.begin(); i != _resamplers.end(); ++i) {
135 for (PositionMap::iterator i = _positions.begin(); i != _positions.end(); ++i) {
141 AudioDecoder::flush ()
143 for (ResamplerMap::iterator i = _resamplers.begin(); i != _resamplers.end(); ++i) {
144 shared_ptr<const AudioBuffers> ro = i->second->flush ();
145 if (ro->frames() > 0) {
146 Data (i->first, ContentAudio (ro, _positions[i->first]));
147 _positions[i->first] += ro->frames();
151 if (_content->delay() < 0) {
152 /* Finish off with the gap caused by the delay */
153 silence (-_content->delay ());
158 AudioDecoder::silence (int milliseconds)
160 BOOST_FOREACH (AudioStreamPtr i, _content->streams ()) {
161 int const samples = ContentTime::from_seconds(milliseconds / 1000.0).frames_round(i->frame_rate());
162 shared_ptr<AudioBuffers> silence (new AudioBuffers (i->channels(), samples));
163 silence->make_silent ();
164 Data (i, ContentAudio (silence, _positions[i]));