Attempts to simplify black-filling logic in Player.
[dcpomatic.git] / src / lib / audio_decoder.cc
1 /*
2     Copyright (C) 2012-2017 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.h"
22 #include "audio_buffers.h"
23 #include "audio_content.h"
24 #include "log.h"
25 #include "resampler.h"
26 #include "compose.hpp"
27 #include <boost/foreach.hpp>
28 #include <iostream>
29
30 #include "i18n.h"
31
32 #define LOG_GENERAL(...) _log->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
33
34 using std::cout;
35 using std::map;
36 using std::pair;
37 using boost::shared_ptr;
38 using boost::optional;
39
40 AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, shared_ptr<Log> log)
41         : DecoderPart (parent, log)
42         , _content (content)
43 {
44         /* Set up _positions so that we have one for each stream */
45         BOOST_FOREACH (AudioStreamPtr i, content->streams ()) {
46                 _positions[i] = 0;
47         }
48 }
49
50 void
51 AudioDecoder::emit (AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time)
52 {
53         if (ignore ()) {
54                 return;
55         }
56
57         if (_positions[stream] == 0) {
58                 /* This is the first data we have received since initialisation or seek.  Set
59                    the position based on the ContentTime that was given.  After this first time
60                    we just count samples, as it seems that ContentTimes are unreliable from
61                    FFmpegDecoder (not quite continuous; perhaps due to some rounding error).
62                 */
63                 _positions[stream] = time.frames_round (stream->frame_rate ());
64         }
65
66         shared_ptr<Resampler> resampler;
67         map<AudioStreamPtr, shared_ptr<Resampler> >::iterator i = _resamplers.find(stream);
68         if (i != _resamplers.end ()) {
69                 resampler = i->second;
70         } else {
71                 if (stream->frame_rate() != _content->resampled_frame_rate()) {
72                         LOG_GENERAL (
73                                 "Creating new resampler from %1 to %2 with %3 channels",
74                                 stream->frame_rate(),
75                                 _content->resampled_frame_rate(),
76                                 stream->channels()
77                                 );
78
79                         resampler.reset (new Resampler (stream->frame_rate(), _content->resampled_frame_rate(), stream->channels()));
80                         _resamplers[stream] = resampler;
81                 }
82         }
83
84         if (resampler) {
85                 shared_ptr<const AudioBuffers> ro = resampler->run (data);
86                 if (ro->frames() == 0) {
87                         return;
88                 }
89                 data = ro;
90         }
91
92         Data (stream, ContentAudio (data, _positions[stream]));
93         _positions[stream] += data->frames();
94 }
95
96 /** @return Time just after the last thing that was emitted from a given stream */
97 ContentTime
98 AudioDecoder::stream_position (AudioStreamPtr stream) const
99 {
100         map<AudioStreamPtr, Frame>::const_iterator i = _positions.find (stream);
101         DCPOMATIC_ASSERT (i != _positions.end ());
102         return ContentTime::from_frames (i->second, _content->resampled_frame_rate());
103 }
104
105 ContentTime
106 AudioDecoder::position () const
107 {
108         optional<ContentTime> p;
109         for (map<AudioStreamPtr, Frame>::const_iterator i = _positions.begin(); i != _positions.end(); ++i) {
110                 ContentTime const ct = stream_position (i->first);
111                 if (!p || ct < *p) {
112                         p = ct;
113                 }
114         }
115
116         return p.get_value_or(ContentTime());
117 }
118
119 void
120 AudioDecoder::seek ()
121 {
122         for (map<AudioStreamPtr, shared_ptr<Resampler> >::iterator i = _resamplers.begin(); i != _resamplers.end(); ++i) {
123                 i->second->flush ();
124                 i->second->reset ();
125         }
126
127         for (map<AudioStreamPtr, Frame>::iterator i = _positions.begin(); i != _positions.end(); ++i) {
128                 i->second = 0;
129         }
130 }
131
132 void
133 AudioDecoder::flush ()
134 {
135         for (map<AudioStreamPtr, shared_ptr<Resampler> >::iterator i = _resamplers.begin(); i != _resamplers.end(); ++i) {
136                 shared_ptr<const AudioBuffers> ro = i->second->flush ();
137                 if (ro->frames() > 0) {
138                         Data (i->first, ContentAudio (ro, _positions[i->first]));
139                         _positions[i->first] += ro->frames();
140                 }
141         }
142 }