Revert "Use make_shared<>."
[dcpomatic.git] / src / lib / audio_decoder_stream.cc
1 /*
2     Copyright (C) 2012-2016 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_stream.h"
22 #include "audio_buffers.h"
23 #include "audio_processor.h"
24 #include "audio_decoder.h"
25 #include "resampler.h"
26 #include "util.h"
27 #include "film.h"
28 #include "log.h"
29 #include "audio_content.h"
30 #include "compose.hpp"
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::list;
36 using std::pair;
37 using std::cout;
38 using std::min;
39 using std::max;
40 using boost::optional;
41 using boost::shared_ptr;
42
43 AudioDecoderStream::AudioDecoderStream (shared_ptr<const AudioContent> content, AudioStreamPtr stream, Decoder* decoder, bool fast, shared_ptr<Log> log)
44         : _content (content)
45         , _stream (stream)
46         , _decoder (decoder)
47         , _log (log)
48 {
49         if (content->resampled_frame_rate() != _stream->frame_rate() && _stream->channels() > 0) {
50                 _resampler.reset (new Resampler (_stream->frame_rate(), content->resampled_frame_rate(), _stream->channels (), fast));
51         }
52
53         reset_decoded ();
54 }
55
56 void
57 AudioDecoderStream::reset_decoded ()
58 {
59         _decoded = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_stream->channels(), 0)), 0);
60 }
61
62 ContentAudio
63 AudioDecoderStream::get (Frame frame, Frame length, bool accurate)
64 {
65         shared_ptr<ContentAudio> dec;
66
67         _log->log (String::compose ("-> ADS has request for %1 %2", frame, length), LogEntry::TYPE_DEBUG_DECODE);
68
69         Frame const end = frame + length - 1;
70
71         if (frame < _decoded.frame || end > (_decoded.frame + length * 4)) {
72                 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
73                 _decoder->seek (ContentTime::from_frames (frame, _content->resampled_frame_rate()), accurate);
74         }
75
76         /* Offset of the data that we want from the start of _decoded.audio
77            (to be set up shortly)
78         */
79         Frame decoded_offset = 0;
80
81         /* Now enough pass() calls will either:
82          *  (a) give us what we want, or
83          *  (b) hit the end of the decoder.
84          *
85          * If we are being accurate, we want the right frames,
86          * otherwise any frames will do.
87          */
88         if (accurate) {
89                 /* Keep stuffing data into _decoded until we have enough data, or the subclass does not want to give us any more */
90                 while (
91                         (_decoded.frame > frame || (_decoded.frame + _decoded.audio->frames()) < end) &&
92                         !_decoder->pass (Decoder::PASS_REASON_AUDIO, accurate)
93                         )
94                 {}
95
96                 decoded_offset = frame - _decoded.frame;
97
98                 _log->log (
99                         String::compose ("Accurate ADS::get has offset %1 from request %2 and available %3", decoded_offset, frame, _decoded.frame),
100                         LogEntry::TYPE_DEBUG_DECODE
101                         );
102         } else {
103                 while (
104                         _decoded.audio->frames() < length &&
105                         !_decoder->pass (Decoder::PASS_REASON_AUDIO, accurate)
106                         )
107                 {}
108
109                 /* Use decoded_offset of 0, as we don't really care what frames we return */
110         }
111
112         /* The amount of data available in _decoded.audio starting from `frame'.  This could be -ve
113            if pass() returned true before we got enough data.
114         */
115         Frame const available = _decoded.audio->frames() - decoded_offset;
116
117         /* We will return either that, or the requested amount, whichever is smaller */
118         Frame const to_return = max ((Frame) 0, min (available, length));
119
120         /* Copy our data to the output */
121         shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded.audio->channels(), to_return));
122         out->copy_from (_decoded.audio.get(), to_return, decoded_offset, 0);
123
124         Frame const remaining = max ((Frame) 0, available - to_return);
125
126         /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */
127         _decoded.audio->move (decoded_offset + to_return, 0, remaining);
128         /* And set up the number of frames we have left */
129         _decoded.audio->set_frames (remaining);
130         /* Also bump where those frames are in terms of the content */
131         _decoded.frame += decoded_offset + to_return;
132
133         return ContentAudio (out, frame);
134 }
135
136 /** Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
137  *  We have to assume that we are feeding continuous data into the resampler, and so we get continuous
138  *  data out.  Hence we do the timestamping here, post-resampler, just by counting samples.
139  *
140  *  The time is passed in here so that after a seek we can set up our _position.  The
141  *  time is ignored once this has been done.
142  */
143 void
144 AudioDecoderStream::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
145 {
146         _log->log (String::compose ("ADS receives %1 %2", time, data->frames ()), LogEntry::TYPE_DEBUG_DECODE);
147
148         if (_resampler) {
149                 data = _resampler->run (data);
150         }
151
152         Frame const frame_rate = _content->resampled_frame_rate ();
153
154         if (_seek_reference) {
155                 /* We've had an accurate seek and now we're seeing some data */
156                 ContentTime const delta = time - _seek_reference.get ();
157                 Frame const delta_frames = delta.frames_round (frame_rate);
158                 if (delta_frames > 0) {
159                         /* This data comes after the seek time.  Pad the data with some silence. */
160                         shared_ptr<AudioBuffers> padded (new AudioBuffers (data->channels(), data->frames() + delta_frames));
161                         padded->make_silent ();
162                         padded->copy_from (data.get(), data->frames(), 0, delta_frames);
163                         data = padded;
164                         time -= delta;
165                 } else if (delta_frames < 0) {
166                         /* This data comes before the seek time.  Throw some data away */
167                         Frame const to_discard = min (-delta_frames, static_cast<Frame> (data->frames()));
168                         Frame const to_keep = data->frames() - to_discard;
169                         if (to_keep == 0) {
170                                 /* We have to throw all this data away, so keep _seek_reference and
171                                    try again next time some data arrives.
172                                 */
173                                 return;
174                         }
175                         shared_ptr<AudioBuffers> trimmed (new AudioBuffers (data->channels(), to_keep));
176                         trimmed->copy_from (data.get(), to_keep, to_discard, 0);
177                         data = trimmed;
178                         time += ContentTime::from_frames (to_discard, frame_rate);
179                 }
180                 _seek_reference = optional<ContentTime> ();
181         }
182
183         if (!_position) {
184                 _position = time.frames_round (frame_rate);
185         }
186
187         DCPOMATIC_ASSERT (_position.get() >= (_decoded.frame + _decoded.audio->frames()));
188
189         add (data);
190 }
191
192 void
193 AudioDecoderStream::add (shared_ptr<const AudioBuffers> data)
194 {
195         if (!_position) {
196                 /* This should only happen when there is a seek followed by a flush, but
197                    we need to cope with it.
198                 */
199                 return;
200         }
201
202         /* Resize _decoded to fit the new data */
203         int new_size = 0;
204         if (_decoded.audio->frames() == 0) {
205                 /* There's nothing in there, so just store the new data */
206                 new_size = data->frames ();
207                 _decoded.frame = _position.get ();
208         } else {
209                 /* Otherwise we need to extend _decoded to include the new stuff */
210                 new_size = _position.get() + data->frames() - _decoded.frame;
211         }
212
213         _decoded.audio->ensure_size (new_size);
214         _decoded.audio->set_frames (new_size);
215
216         /* Copy new data in */
217         _decoded.audio->copy_from (data.get(), data->frames(), 0, _position.get() - _decoded.frame);
218         _position = _position.get() + data->frames ();
219
220         /* Limit the amount of data we keep in case nobody is asking for it */
221         int const max_frames = _content->resampled_frame_rate () * 10;
222         if (_decoded.audio->frames() > max_frames) {
223                 int const to_remove = _decoded.audio->frames() - max_frames;
224                 _decoded.frame += to_remove;
225                 _decoded.audio->move (to_remove, 0, max_frames);
226                 _decoded.audio->set_frames (max_frames);
227         }
228 }
229
230 void
231 AudioDecoderStream::flush ()
232 {
233         if (!_resampler) {
234                 return;
235         }
236
237         shared_ptr<const AudioBuffers> b = _resampler->flush ();
238         if (b) {
239                 add (b);
240         }
241 }
242
243 void
244 AudioDecoderStream::seek (ContentTime t, bool accurate)
245 {
246         _position.reset ();
247         reset_decoded ();
248         if (accurate) {
249                 _seek_reference = t;
250         }
251 }