Setup fast state of decoder after creation.
[dcpomatic.git] / src / lib / audio_decoder.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "audio_decoder.h"
22 #include "audio_buffers.h"
23 #include "audio_content.h"
24 #include "dcpomatic_log.h"
25 #include "log.h"
26 #include "resampler.h"
27 #include "compose.hpp"
28 #include <boost/foreach.hpp>
29 #include <iostream>
30
31 #include "i18n.h"
32
33 using std::cout;
34 using std::map;
35 using std::pair;
36 using boost::shared_ptr;
37 using boost::optional;
38 using namespace dcpomatic;
39
40 AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content)
41         : DecoderPart (parent)
42         , _content (content)
43         , _fast (false)
44 {
45         /* Set up _positions so that we have one for each stream */
46         BOOST_FOREACH (AudioStreamPtr i, content->streams ()) {
47                 _positions[i] = 0;
48         }
49 }
50
51 void
52 AudioDecoder::emit (shared_ptr<const Film> film, AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time)
53 {
54         if (ignore ()) {
55                 return;
56         }
57
58         if (_positions[stream] == 0) {
59                 /* This is the first data we have received since initialisation or seek.  Set
60                    the position based on the ContentTime that was given.  After this first time
61                    we just count samples, as it seems that ContentTimes are unreliable from
62                    FFmpegDecoder (not quite continuous; perhaps due to some rounding error).
63                 */
64                 if (_content->delay() > 0) {
65                         /* Insert silence to give the delay */
66                         silence (_content->delay ());
67                 }
68                 time += ContentTime::from_seconds (_content->delay() / 1000.0);
69                 _positions[stream] = time.frames_round (_content->resampled_frame_rate(film));
70         }
71
72         shared_ptr<Resampler> resampler;
73         ResamplerMap::iterator i = _resamplers.find(stream);
74         if (i != _resamplers.end ()) {
75                 resampler = i->second;
76         } else {
77                 if (stream->frame_rate() != _content->resampled_frame_rate(film)) {
78                         LOG_GENERAL (
79                                 "Creating new resampler from %1 to %2 with %3 channels",
80                                 stream->frame_rate(),
81                                 _content->resampled_frame_rate(film),
82                                 stream->channels()
83                                 );
84
85                         resampler.reset (new Resampler (stream->frame_rate(), _content->resampled_frame_rate(film), stream->channels()));
86                         resampler->set_fast (_fast);
87                         _resamplers[stream] = resampler;
88                 }
89         }
90
91         if (resampler) {
92                 shared_ptr<const AudioBuffers> ro = resampler->run (data);
93                 if (ro->frames() == 0) {
94                         return;
95                 }
96                 data = ro;
97         }
98
99         Data(stream, ContentAudio (data, _positions[stream]));
100         _positions[stream] += data->frames();
101 }
102
103 /** @return Time just after the last thing that was emitted from a given stream */
104 ContentTime
105 AudioDecoder::stream_position (shared_ptr<const Film> film, AudioStreamPtr stream) const
106 {
107         PositionMap::const_iterator i = _positions.find (stream);
108         DCPOMATIC_ASSERT (i != _positions.end ());
109         return ContentTime::from_frames (i->second, _content->resampled_frame_rate(film));
110 }
111
112 ContentTime
113 AudioDecoder::position (shared_ptr<const Film> film) const
114 {
115         optional<ContentTime> p;
116         for (PositionMap::const_iterator i = _positions.begin(); i != _positions.end(); ++i) {
117                 ContentTime const ct = stream_position (film, i->first);
118                 if (!p || ct < *p) {
119                         p = ct;
120                 }
121         }
122
123         return p.get_value_or(ContentTime());
124 }
125
126 void
127 AudioDecoder::seek ()
128 {
129         for (ResamplerMap::iterator i = _resamplers.begin(); i != _resamplers.end(); ++i) {
130                 i->second->flush ();
131                 i->second->reset ();
132         }
133
134         for (PositionMap::iterator i = _positions.begin(); i != _positions.end(); ++i) {
135                 i->second = 0;
136         }
137 }
138
139 void
140 AudioDecoder::flush ()
141 {
142         for (ResamplerMap::iterator i = _resamplers.begin(); i != _resamplers.end(); ++i) {
143                 shared_ptr<const AudioBuffers> ro = i->second->flush ();
144                 if (ro->frames() > 0) {
145                         Data (i->first, ContentAudio (ro, _positions[i->first]));
146                         _positions[i->first] += ro->frames();
147                 }
148         }
149
150         if (_content->delay() < 0) {
151                 /* Finish off with the gap caused by the delay */
152                 silence (-_content->delay ());
153         }
154 }
155
156 void
157 AudioDecoder::silence (int milliseconds)
158 {
159         BOOST_FOREACH (AudioStreamPtr i, _content->streams ()) {
160                 int const samples = ContentTime::from_seconds(milliseconds / 1000.0).frames_round(i->frame_rate());
161                 shared_ptr<AudioBuffers> silence (new AudioBuffers (i->channels(), samples));
162                 silence->make_silent ();
163                 Data (i, ContentAudio (silence, _positions[i]));
164         }
165 }
166
167 void
168 AudioDecoder::set_fast (bool fast)
169 {
170         _fast = fast;
171         for (ResamplerMap::iterator i = _resamplers.begin(); i != _resamplers.end(); ++i) {
172                 i->second->set_fast (_fast);
173         }
174 }