57e00334b1b96c7faff2899899efed89fa2070f0
[dcpomatic.git] / src / lib / piece.cc
1 /*
2     Copyright (C) 2013-2020 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 "film.h"
23 #include "piece.h"
24
25
26 using boost::shared_ptr;
27 using namespace dcpomatic;
28
29
30 DCPTime
31 Piece::position () const
32 {
33         return content->position ();
34 }
35
36
37 DCPTime
38 Piece::end (shared_ptr<const Film> film) const
39 {
40         return content->end (film);
41 }
42
43
44 DCPTime
45 Piece::content_video_to_dcp (Frame f) const
46 {
47         /* It might seem more logical here to convert s to a ContentTime (using the FrameRateChange)
48            then convert that ContentTime to frames at the content's rate.  However this fails for
49            situations like content at 29.9978733fps, DCP at 30fps.  The accuracy of the Time type is not
50            enough to distinguish between the two with low values of time (e.g. 3200 in Time units).
51
52            Instead we convert the DCPTime using the DCP video rate then account for any skip/repeat.
53            */
54         DCPTime const d = DCPTime::from_frames(f * frc.factor(), frc.dcp) - DCPTime(content->trim_start(), frc);
55         return d + position();
56 }
57
58
59 DCPTime
60 Piece::content_time_to_dcp (ContentTime t) const
61 {
62         return max (DCPTime(), DCPTime(t - content->trim_start(), frc) + position());
63 }
64
65
66 Crop
67 Piece::video_crop () const
68 {
69         return content->video->crop ();
70 }
71
72
73 DCPTime
74 Piece::resampled_audio_to_dcp (shared_ptr<const Film> film, Frame f) const
75 {
76         /* See notes in content_video_to_dcp */
77         return DCPTime::from_frames(f, film->audio_frame_rate())
78                 - DCPTime(content->trim_start(), frc)
79                 + position();
80 }
81
82
83 ContentTime
84 Piece::dcp_to_content_time (shared_ptr<const Film> film, DCPTime t) const
85 {
86         DCPTime s = t - position();
87         s = min(content->length_after_trim(film), s);
88         return max(ContentTime(), ContentTime(s, frc) + content->trim_start());
89 }
90