Very basic audio processing framework.
[dcpomatic.git] / src / lib / audio_decoder.cc
1 /*
2     Copyright (C) 2012-2014 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.h"
21 #include "audio_buffers.h"
22 #include "exceptions.h"
23 #include "log.h"
24 #include "resampler.h"
25 #include "util.h"
26 #include "film.h"
27 #include "audio_processor.h"
28
29 #include "i18n.h"
30
31 using std::stringstream;
32 using std::list;
33 using std::pair;
34 using std::cout;
35 using std::min;
36 using std::max;
37 using boost::optional;
38 using boost::shared_ptr;
39
40 AudioDecoder::AudioDecoder (shared_ptr<const AudioContent> content)
41         : _audio_content (content)
42 {
43         if (content->resampled_audio_frame_rate() != content->audio_frame_rate() && content->audio_channels ()) {
44                 _resampler.reset (new Resampler (content->audio_frame_rate(), content->resampled_audio_frame_rate(), content->audio_channels ()));
45         }
46
47         if (content->audio_processor ()) {
48                 _processor = content->audio_processor()->clone ();
49         }
50
51         reset_decoded_audio ();
52 }
53
54 void
55 AudioDecoder::reset_decoded_audio ()
56 {
57         _decoded_audio = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_audio_content->processed_audio_channels(), 0)), 0);
58 }
59
60 shared_ptr<ContentAudio>
61 AudioDecoder::get_audio (AudioFrame frame, AudioFrame length, bool accurate)
62 {
63         shared_ptr<ContentAudio> dec;
64
65         AudioFrame const end = frame + length - 1;
66                 
67         if (frame < _decoded_audio.frame || end > (_decoded_audio.frame + length * 4)) {
68                 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
69                 seek (ContentTime::from_frames (frame, _audio_content->audio_frame_rate()), accurate);
70         }
71
72         /* Offset of the data that we want from the start of _decoded_audio.audio
73            (to be set up shortly)
74         */
75         AudioFrame decoded_offset = 0;
76         
77         /* Now enough pass() calls will either:
78          *  (a) give us what we want, or
79          *  (b) hit the end of the decoder.
80          *
81          * If we are being accurate, we want the right frames,
82          * otherwise any frames will do.
83          */
84         if (accurate) {
85                 /* Keep stuffing data into _decoded_audio until we have enough data, or the subclass does not want to give us any more */
86                 while (!pass() && (_decoded_audio.frame > frame || (_decoded_audio.frame + _decoded_audio.audio->frames()) < end)) {}
87                 decoded_offset = frame - _decoded_audio.frame;
88         } else {
89                 while (!pass() && _decoded_audio.audio->frames() < length) {}
90                 /* Use decoded_offset of 0, as we don't really care what frames we return */
91         }
92
93         /* The amount of data available in _decoded_audio.audio starting from `frame'.  This could be -ve
94            if pass() returned true before we got enough data.
95         */
96         AudioFrame const available = _decoded_audio.audio->frames() - decoded_offset;
97
98         /* We will return either that, or the requested amount, whichever is smaller */
99         AudioFrame const to_return = max ((AudioFrame) 0, min (available, length));
100
101         /* Copy our data to the output */
102         shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded_audio.audio->channels(), to_return));
103         out->copy_from (_decoded_audio.audio.get(), to_return, decoded_offset, 0);
104
105         AudioFrame const remaining = max ((AudioFrame) 0, available - to_return);
106
107         /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */
108         _decoded_audio.audio->move (decoded_offset + to_return, 0, remaining);
109         /* And set up the number of frames we have left */
110         _decoded_audio.audio->set_frames (remaining);
111         /* Also bump where those frames are in terms of the content */
112         _decoded_audio.frame += decoded_offset + to_return;
113
114         return shared_ptr<ContentAudio> (new ContentAudio (out, frame));
115 }
116
117 /** Called by subclasses when audio data is ready.
118  *
119  *  Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
120  *  We have to assume that we are feeding continuous data into the resampler, and so we get continuous
121  *  data out.  Hence we do the timestamping here, post-resampler, just by counting samples.
122  *
123  *  The time is passed in here so that after a seek we can set up our _audio_position.  The
124  *  time is ignored once this has been done.
125  */
126 void
127 AudioDecoder::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
128 {
129         if (_resampler) {
130                 data = _resampler->run (data);
131         }
132
133         if (_processor) {
134                 data = _processor->run (data);
135         }
136
137         AudioFrame const frame_rate = _audio_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                 AudioFrame 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                         AudioFrame const to_discard = min (-delta_frames, static_cast<AudioFrame> (data->frames()));
153                         AudioFrame 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 (!_audio_position) {
169                 _audio_position = time.frames (frame_rate);
170         }
171
172         assert (_audio_position.get() >= (_decoded_audio.frame + _decoded_audio.audio->frames()));
173
174         /* Resize _decoded_audio to fit the new data */
175         int new_size = 0;
176         if (_decoded_audio.audio->frames() == 0) {
177                 /* There's nothing in there, so just store the new data */
178                 new_size = data->frames ();
179                 _decoded_audio.frame = _audio_position.get ();
180         } else {
181                 /* Otherwise we need to extend _decoded_audio to include the new stuff */
182                 new_size = _audio_position.get() + data->frames() - _decoded_audio.frame;
183         }
184         
185         _decoded_audio.audio->ensure_size (new_size);
186         _decoded_audio.audio->set_frames (new_size);
187
188         /* Copy new data in */
189         _decoded_audio.audio->copy_from (data.get(), data->frames(), 0, _audio_position.get() - _decoded_audio.frame);
190         _audio_position = _audio_position.get() + data->frames ();
191 }
192
193 /* XXX: called? */
194 void
195 AudioDecoder::flush ()
196 {
197         if (!_resampler) {
198                 return;
199         }
200
201         /*
202         shared_ptr<const AudioBuffers> b = _resampler->flush ();
203         if (b) {
204                 _pending.push_back (shared_ptr<DecodedAudio> (new DecodedAudio (b, _audio_position.get ())));
205                 _audio_position = _audio_position.get() + b->frames ();
206         }
207         */
208 }
209
210 void
211 AudioDecoder::seek (ContentTime t, bool accurate)
212 {
213         _audio_position.reset ();
214         reset_decoded_audio ();
215         if (accurate) {
216                 _seek_reference = t;
217         }
218 }