Various fixes so that audio_delay_test works again.
[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
28 #include "i18n.h"
29
30 using std::stringstream;
31 using std::list;
32 using std::pair;
33 using std::cout;
34 using std::min;
35 using std::max;
36 using boost::optional;
37 using boost::shared_ptr;
38
39 AudioDecoder::AudioDecoder (shared_ptr<const AudioContent> content)
40         : _audio_content (content)
41 {
42         if (content->output_audio_frame_rate() != content->content_audio_frame_rate() && content->audio_channels ()) {
43                 _resampler.reset (new Resampler (content->content_audio_frame_rate(), content->output_audio_frame_rate(), content->audio_channels ()));
44         }
45
46         reset_decoded_audio ();
47 }
48
49 void
50 AudioDecoder::reset_decoded_audio ()
51 {
52         _decoded_audio = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_audio_content->audio_channels(), 0)), 0);
53 }
54
55 shared_ptr<ContentAudio>
56 AudioDecoder::get_audio (AudioFrame frame, AudioFrame length, bool accurate)
57 {
58         shared_ptr<ContentAudio> dec;
59
60         AudioFrame const end = frame + length - 1;
61                 
62         if (frame < _decoded_audio.frame || end > (_decoded_audio.frame + length * 4)) {
63                 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
64                 seek (ContentTime::from_frames (frame, _audio_content->content_audio_frame_rate()), accurate);
65         }
66
67         /* Offset of the data that we want from the start of _decoded_audio.audio
68            (to be set up shortly)
69         */
70         AudioFrame decoded_offset = 0;
71         
72         /* Now enough pass() calls will either:
73          *  (a) give us what we want, or
74          *  (b) hit the end of the decoder.
75          *
76          * If we are being accurate, we want the right frames,
77          * otherwise any frames will do.
78          */
79         if (accurate) {
80                 /* Keep stuffing data into _decoded_audio until we have enough data, or the subclass does not want to give us any more */
81                 while (!pass() && (_decoded_audio.frame > frame || (_decoded_audio.frame + _decoded_audio.audio->frames()) < end)) {}
82                 decoded_offset = frame - _decoded_audio.frame;
83         } else {
84                 while (!pass() && _decoded_audio.audio->frames() < length) {}
85                 /* Use decoded_offset of 0, as we don't really care what frames we return */
86         }
87
88         /* The amount of data available in _decoded_audio.audio starting from `frame'.  This could be -ve
89            if pass() returned true before we got enough data
90         */
91         AudioFrame const amount_left = _decoded_audio.audio->frames() - decoded_offset;
92         
93         /* We will return either that, or the requested amount, whichever is smaller */
94         AudioFrame const to_return = max ((AudioFrame) 0, min (amount_left, length));
95
96         /* Copy our data to the output */
97         shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded_audio.audio->channels(), to_return));
98         out->copy_from (_decoded_audio.audio.get(), to_return, decoded_offset, 0);
99         
100         /* Clean up decoded; first, move the data after what we just returned to the start of the buffer */
101         _decoded_audio.audio->move (decoded_offset + to_return, 0, amount_left - to_return);
102         /* And set up the number of frames we have left */
103         _decoded_audio.audio->set_frames (amount_left - to_return);
104         /* Also bump where those frames are in terms of the content */
105         _decoded_audio.frame += decoded_offset + to_return;
106
107         return shared_ptr<ContentAudio> (new ContentAudio (out, frame));
108 }
109
110 /** Called by subclasses when audio data is ready.
111  *
112  *  Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
113  *  We have to assume that we are feeding continuous data into the resampler, and so we get continuous
114  *  data out.  Hence we do the timestamping here, post-resampler, just by counting samples.
115  *
116  *  The time is passed in here so that after a seek we can set up our _audio_position.  The
117  *  time is ignored once this has been done.
118  */
119 void
120 AudioDecoder::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
121 {
122         if (_resampler) {
123                 data = _resampler->run (data);
124         }
125
126         if (!_audio_position) {
127                 _audio_position = time.frames (_audio_content->output_audio_frame_rate ());
128         }
129
130         assert (_audio_position.get() >= (_decoded_audio.frame + _decoded_audio.audio->frames()));
131
132         /* Resize _decoded_audio to fit the new data */
133         int const new_size = _audio_position.get() + data->frames() - _decoded_audio.frame;
134         _decoded_audio.audio->ensure_size (new_size);
135         _decoded_audio.audio->set_frames (new_size);
136
137         /* Copy new data in */
138         _decoded_audio.audio->copy_from (data.get(), data->frames(), 0, _audio_position.get() - _decoded_audio.frame);
139         _audio_position = _audio_position.get() + data->frames ();
140 }
141
142 /* XXX: called? */
143 void
144 AudioDecoder::flush ()
145 {
146         if (!_resampler) {
147                 return;
148         }
149
150         /*
151         shared_ptr<const AudioBuffers> b = _resampler->flush ();
152         if (b) {
153                 _pending.push_back (shared_ptr<DecodedAudio> (new DecodedAudio (b, _audio_position.get ())));
154                 _audio_position = _audio_position.get() + b->frames ();
155         }
156         */
157 }
158
159 void
160 AudioDecoder::seek (ContentTime, bool)
161 {
162         _audio_position.reset ();
163         reset_decoded_audio ();
164 }