It builds.
[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 boost::optional;
35 using boost::shared_ptr;
36
37 AudioDecoder::AudioDecoder (shared_ptr<const AudioContent> content)
38         : _audio_content (content)
39 {
40         if (content->output_audio_frame_rate() != content->content_audio_frame_rate() && content->audio_channels ()) {
41                 _resampler.reset (new Resampler (content->content_audio_frame_rate(), content->output_audio_frame_rate(), content->audio_channels ()));
42         }
43 }
44
45 shared_ptr<ContentAudio>
46 AudioDecoder::get_audio (AudioFrame frame, AudioFrame length, bool accurate)
47 {
48         shared_ptr<ContentAudio> dec;
49
50         AudioFrame const end = frame + length - 1;
51                 
52         if (frame < _decoded_audio.frame || end > (_decoded_audio.frame + length * 4)) {
53                 /* Either we have no decoded data, or what we do have is a long way from what we want: seek */
54                 seek (ContentTime::from_frames (frame, _audio_content->content_audio_frame_rate()), accurate);
55         }
56
57         /* Now enough pass() calls will either:
58          *  (a) give us what we want, or
59          *  (b) hit the end of the decoder.
60          *
61          * If we are being accurate, we want the right frames,
62          * otherwise any frames will do.
63          */
64         if (accurate) {
65                 while (!pass() && _decoded_audio.audio->frames() < length) {}
66         } else {
67                 while (!pass() && (_decoded_audio.frame > frame || (_decoded_audio.frame + _decoded_audio.audio->frames()) < end)) {}
68         }
69         
70         /* Clean up decoded */
71
72         AudioFrame const decoded_offset = frame - _decoded_audio.frame;
73         AudioFrame const amount_left = _decoded_audio.audio->frames() - decoded_offset;
74         _decoded_audio.audio->move (decoded_offset, 0, amount_left);
75         _decoded_audio.audio->set_frames (amount_left);
76
77         shared_ptr<AudioBuffers> out (new AudioBuffers (_decoded_audio.audio->channels(), length));
78         out->copy_from (_decoded_audio.audio.get(), length, frame - _decoded_audio.frame, 0);
79
80         return shared_ptr<ContentAudio> (new ContentAudio (out, frame));
81 }
82
83 /** Called by subclasses when audio data is ready.
84  *
85  *  Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
86  *  We have to assume that we are feeding continuous data into the resampler, and so we get continuous
87  *  data out.  Hence we do the timestamping here, post-resampler, just by counting samples.
88  *
89  *  The time is passed in here so that after a seek we can set up our _audio_position.  The
90  *  time is ignored once this has been done.
91  */
92 void
93 AudioDecoder::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
94 {
95         if (_resampler) {
96                 data = _resampler->run (data);
97         }
98
99         if (!_audio_position) {
100                 _audio_position = time.frames (_audio_content->output_audio_frame_rate ());
101         }
102
103         assert (_audio_position >= (_decoded_audio.frame + _decoded_audio.audio->frames()));
104
105         /* Resize _decoded_audio to fit the new data */
106         _decoded_audio.audio->ensure_size (_audio_position.get() + data->frames() - _decoded_audio.frame);
107
108         /* Copy new data in */
109         _decoded_audio.audio->copy_from (data.get(), data->frames(), 0, _audio_position.get() - _decoded_audio.frame);
110         _audio_position = _audio_position.get() + data->frames ();
111 }
112
113 /* XXX: called? */
114 void
115 AudioDecoder::flush ()
116 {
117         if (!_resampler) {
118                 return;
119         }
120
121         /*
122         shared_ptr<const AudioBuffers> b = _resampler->flush ();
123         if (b) {
124                 _pending.push_back (shared_ptr<DecodedAudio> (new DecodedAudio (b, _audio_position.get ())));
125                 _audio_position = _audio_position.get() + b->frames ();
126         }
127         */
128 }
129
130 void
131 AudioDecoder::seek (ContentTime, bool)
132 {
133         _audio_position.reset ();
134 }