Some comments and a few small cleanups.
[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 "compose.hpp"
26 #include <boost/foreach.hpp>
27 #include <iostream>
28
29 #include "i18n.h"
30
31 using std::cout;
32 using std::map;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 AudioDecoder::AudioDecoder (Decoder* parent, shared_ptr<const AudioContent> content, shared_ptr<Log> log)
37         : DecoderPart (parent, log)
38 {
39         /* Set up _positions so that we have one for each stream */
40         BOOST_FOREACH (AudioStreamPtr i, content->streams ()) {
41                 _positions[i] = 0;
42         }
43 }
44
45 void
46 AudioDecoder::emit (AudioStreamPtr stream, shared_ptr<const AudioBuffers> data, ContentTime time)
47 {
48         if (ignore ()) {
49                 return;
50         }
51
52         if (_positions[stream] == 0) {
53                 /* This is the first data we have received since initialisation or seek.  Set
54                    the position based on the ContentTime that was given.  After this first time
55                    we just count samples, as it seems that ContentTimes are unreliable from
56                    FFmpegDecoder (not quite continuous; perhaps due to some rounding error).
57                 */
58                 _positions[stream] = time.frames_round (stream->frame_rate ());
59         }
60
61         Data (stream, ContentAudio (data, _positions[stream]));
62         _positions[stream] += data->frames();
63 }
64
65 ContentTime
66 AudioDecoder::position () const
67 {
68         optional<ContentTime> p;
69         for (map<AudioStreamPtr, Frame>::const_iterator i = _positions.begin(); i != _positions.end(); ++i) {
70                 ContentTime const ct = ContentTime::from_frames (i->second, i->first->frame_rate ());
71                 if (!p || ct < *p) {
72                         p = ct;
73                 }
74         }
75
76         return p.get_value_or(ContentTime());
77 }
78
79 void
80 AudioDecoder::seek ()
81 {
82         for (map<AudioStreamPtr, Frame>::iterator i = _positions.begin(); i != _positions.end(); ++i) {
83                 i->second = 0;
84         }
85 }