Basic grunt-work, untested and unfinished, but it compiles.
[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 <iostream>
32
33 #include "i18n.h"
34
35 using std::list;
36 using std::pair;
37 using std::cout;
38 using std::min;
39 using std::max;
40 using boost::optional;
41 using boost::shared_ptr;
42
43 AudioDecoderStream::AudioDecoderStream (
44         shared_ptr<const AudioContent> content, AudioStreamPtr stream, Decoder* decoder, AudioDecoder* audio_decoder, shared_ptr<Log> log
45         )
46         : _content (content)
47         , _stream (stream)
48         , _decoder (decoder)
49         , _audio_decoder (audio_decoder)
50         , _log (log)
51           /* We effectively start having done a seek to zero; this allows silence-padding of the first
52              data that comes out of our decoder.
53           */
54         , _seek_reference (ContentTime ())
55 {
56         if (content->resampled_frame_rate() != _stream->frame_rate() && _stream->channels() > 0) {
57                 _resampler.reset (new Resampler (_stream->frame_rate(), content->resampled_frame_rate(), _stream->channels ()));
58         }
59
60         reset_decoded ();
61 }
62
63 void
64 AudioDecoderStream::reset_decoded ()
65 {
66         _decoded = ContentAudio (shared_ptr<AudioBuffers> (new AudioBuffers (_stream->channels(), 0)), 0);
67 }
68
69 /** Audio timestamping is made hard by many factors, but perhaps the most entertaining is resampling.
70  *  We have to assume that we are feeding continuous data into the resampler, and so we get continuous
71  *  data out.  Hence we do the timestamping here, post-resampler, just by counting samples.
72  *
73  *  The time is passed in here so that after a seek we can set up our _position.  The
74  *  time is ignored once this has been done.
75  */
76 void
77 AudioDecoderStream::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
78 {
79         _log->log (String::compose ("ADS receives %1 %2", to_string(time), data->frames ()), LogEntry::TYPE_DEBUG_DECODE);
80
81         if (_resampler) {
82                 data = _resampler->run (data);
83         }
84
85         Frame const frame_rate = _content->resampled_frame_rate ();
86
87         if (_seek_reference) {
88                 /* We've had an accurate seek and now we're seeing some data */
89                 ContentTime const delta = time - _seek_reference.get ();
90                 Frame const delta_frames = delta.frames_round (frame_rate);
91                 if (delta_frames > 0) {
92                         /* This data comes after the seek time.  Pad the data with some silence. */
93                         shared_ptr<AudioBuffers> padded (new AudioBuffers (data->channels(), data->frames() + delta_frames));
94                         padded->make_silent ();
95                         padded->copy_from (data.get(), data->frames(), 0, delta_frames);
96                         data = padded;
97                         time -= delta;
98                 }
99                 _seek_reference = optional<ContentTime> ();
100         }
101
102         if (!_position) {
103                 _position = time.frames_round (frame_rate);
104         }
105
106         DCPOMATIC_ASSERT (_position.get() >= (_decoded.frame + _decoded.audio->frames()));
107
108         add (data);
109 }
110
111 void
112 AudioDecoderStream::add (shared_ptr<const AudioBuffers> data)
113 {
114         if (!_position) {
115                 /* This should only happen when there is a seek followed by a flush, but
116                    we need to cope with it.
117                 */
118                 return;
119         }
120
121         /* Resize _decoded to fit the new data */
122         int new_size = 0;
123         if (_decoded.audio->frames() == 0) {
124                 /* There's nothing in there, so just store the new data */
125                 new_size = data->frames ();
126                 _decoded.frame = _position.get ();
127         } else {
128                 /* Otherwise we need to extend _decoded to include the new stuff */
129                 new_size = _position.get() + data->frames() - _decoded.frame;
130         }
131
132         _decoded.audio->ensure_size (new_size);
133         _decoded.audio->set_frames (new_size);
134
135         /* Copy new data in */
136         _decoded.audio->copy_from (data.get(), data->frames(), 0, _position.get() - _decoded.frame);
137         _position = _position.get() + data->frames ();
138
139         /* Limit the amount of data we keep in case nobody is asking for it */
140         int const max_frames = _content->resampled_frame_rate () * 10;
141         if (_decoded.audio->frames() > max_frames) {
142                 int const to_remove = _decoded.audio->frames() - max_frames;
143                 _decoded.frame += to_remove;
144                 _decoded.audio->move (to_remove, 0, max_frames);
145                 _decoded.audio->set_frames (max_frames);
146         }
147 }
148
149 void
150 AudioDecoderStream::flush ()
151 {
152         if (!_resampler) {
153                 return;
154         }
155
156         shared_ptr<const AudioBuffers> b = _resampler->flush ();
157         if (b) {
158                 add (b);
159         }
160 }
161
162 void
163 AudioDecoderStream::set_fast ()
164 {
165         if (_resampler) {
166                 _resampler->set_fast ();
167         }
168 }
169
170 optional<ContentTime>
171 AudioDecoderStream::position () const
172 {
173         if (!_position) {
174                 return optional<ContentTime> ();
175         }
176
177         return ContentTime::from_frames (_position.get(), _content->resampled_frame_rate());
178 }