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