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