Add Piece::position().
[dcpomatic.git] / src / lib / piece.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "audio_content.h"
23 #include "content.h"
24 #include "film.h"
25 #include "piece.h"
26 #include "video_content.h"
27
28
29 using std::shared_ptr;
30 using namespace dcpomatic;
31
32
33 Piece::Piece (shared_ptr<Content> c, shared_ptr<Decoder> d, FrameRateChange f)
34         : content (c)
35         , decoder (d)
36         , frc (f)
37 {
38         if (content->audio) {
39                 for (auto j: content->audio->streams()) {
40                         _stream_last_push_end[j] = content->position();
41                 }
42         }
43 }
44
45
46
47 void
48 Piece::update_pull_to (DCPTime& pull_to) const
49 {
50         if (done) {
51                 return;
52         }
53
54         for (auto const& i: _stream_last_push_end) {
55                 pull_to = std::min(pull_to, i.second);
56         }
57 }
58
59
60 void
61 Piece::set_last_push_end (AudioStreamPtr stream, DCPTime end)
62 {
63         DCPOMATIC_ASSERT (_stream_last_push_end.find(stream) != _stream_last_push_end.end());
64         _stream_last_push_end[stream] = end;
65 }
66
67
68 DCPTime
69 Piece::content_video_to_dcp (Frame f) const
70 {
71         /* See comment in resampled_audio_to_dcp */
72         auto const d = DCPTime::from_frames(f * frc.factor(), frc.dcp) - DCPTime(content->trim_start(), frc);
73         return d + content->position();
74 }
75
76
77 DCPTime
78 Piece::resampled_audio_to_dcp (Frame f, shared_ptr<const Film> film) const
79 {
80         /* It might seem more logical here to convert s to a ContentTime (using the FrameRateChange)
81            then convert that ContentTime to frames at the content's rate.  However this fails for
82            situations like content at 29.9978733fps, DCP at 30fps.  The accuracy of the Time type is not
83            enough to distinguish between the two with low values of time (e.g. 3200 in Time units).
84
85            Instead we convert the DCPTime using the DCP video rate then account for any skip/repeat.
86         */
87         return DCPTime::from_frames(f, film->audio_frame_rate())
88                 - DCPTime(content->trim_start(), frc)
89                 + content->position();
90 }
91
92
93 ContentTime
94 Piece::dcp_to_content_time (DCPTime t, shared_ptr<const Film> film) const
95 {
96         auto s = t - content->position ();
97         s = min (content->length_after_trim(film), s);
98         return max (ContentTime(), ContentTime(s, frc) + content->trim_start());
99 }
100
101
102 DCPTime
103 Piece::content_time_to_dcp (ContentTime t) const
104 {
105         return max (DCPTime(), DCPTime(t - content->trim_start(), frc) + content->position());
106 }
107
108
109 bool
110 Piece::use_video () const
111 {
112         return content->video && content->video->use();
113 }
114
115
116 dcpomatic::DCPTime
117 Piece::position () const
118 {
119         return content->position ();
120 }
121