2 Copyright (C) 2012-2021 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"
35 using std::shared_ptr;
36 using std::make_shared;
37 using boost::optional;
38 using namespace dcpomatic;
40 AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, bool fast)
41 : DecoderPart (parent)
45 /* Set up _positions so that we have one for each stream */
46 for (auto i: content->streams ()) {
51 /** @param time_already_delayed true if the delay should not be added to time */
53 AudioDecoder::emit (shared_ptr<const Film> film, AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time, bool time_already_delayed)
59 /* Amount of error we will tolerate on audio timestamps; see comment below.
60 * We'll use 1 24fps video frame at 48kHz as this seems to be roughly how
63 static Frame const slack_frames = 48000 / 24;
65 int const resampled_rate = _content->resampled_frame_rate(film);
66 if (!time_already_delayed) {
67 time += ContentTime::from_seconds (_content->delay() / 1000.0);
71 if (_positions[stream] == 0) {
72 /* This is the first data we have received since initialisation or seek. Set
73 the position based on the ContentTime that was given. After this first time
74 we just count samples unless the timestamp is more than slack_frames away
75 from where we think it should be. This is because ContentTimes seem to be
76 slightly unreliable from FFmpegDecoder (i.e. not sample accurate), but we still
77 need to obey them sometimes otherwise we get sync problems such as #1833.
79 if (_content->delay() > 0) {
80 /* Insert silence to give the delay */
81 silence (_content->delay ());
84 } else if (std::abs(_positions[stream] - time.frames_round(resampled_rate)) > slack_frames) {
87 "Reset audio position: was %1, new data at %2, slack: %3 frames",
89 time.frames_round(resampled_rate),
90 std::abs(_positions[stream] - time.frames_round(resampled_rate))
95 _positions[stream] = time.frames_round (resampled_rate);
98 shared_ptr<Resampler> resampler;
99 auto i = _resamplers.find(stream);
100 if (i != _resamplers.end ()) {
101 resampler = i->second;
103 if (stream->frame_rate() != resampled_rate) {
105 "Creating new resampler from %1 to %2 with %3 channels",
106 stream->frame_rate(),
111 resampler = make_shared<Resampler>(stream->frame_rate(), resampled_rate, stream->channels());
113 resampler->set_fast ();
115 _resamplers[stream] = resampler;
120 auto ro = resampler->run (data);
121 if (ro->frames() == 0) {
127 Data(stream, ContentAudio (data, _positions[stream]));
128 _positions[stream] += data->frames();
131 /** @return Time just after the last thing that was emitted from a given stream */
133 AudioDecoder::stream_position (shared_ptr<const Film> film, AudioStreamPtr stream) const
135 auto i = _positions.find (stream);
136 DCPOMATIC_ASSERT (i != _positions.end ());
137 return ContentTime::from_frames (i->second, _content->resampled_frame_rate(film));
140 boost::optional<ContentTime>
141 AudioDecoder::position (shared_ptr<const Film> film) const
143 optional<ContentTime> p;
144 for (auto i: _positions) {
145 auto const ct = stream_position (film, i.first);
155 AudioDecoder::seek ()
157 for (auto i: _resamplers) {
162 for (auto& i: _positions) {
168 AudioDecoder::flush ()
170 for (auto const& i: _resamplers) {
171 auto ro = i.second->flush ();
172 if (ro->frames() > 0) {
173 Data (i.first, ContentAudio (ro, _positions[i.first]));
174 _positions[i.first] += ro->frames();
178 if (_content->delay() < 0) {
179 /* Finish off with the gap caused by the delay */
180 silence (-_content->delay ());
185 AudioDecoder::silence (int milliseconds)
187 for (auto i: _content->streams()) {
188 int const samples = ContentTime::from_seconds(milliseconds / 1000.0).frames_round(i->frame_rate());
189 auto silence = make_shared<AudioBuffers>(i->channels(), samples);
190 silence->make_silent ();
191 Data (i, ContentAudio (silence, _positions[i]));