b99f911f2238fe8424fe043416e37d66451f3e6f
[dcpomatic.git] / src / lib / empty_video.cc
1 /*
2     Copyright (C) 2017-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 "content.h"
23 #include "content_part.h"
24 #include "dcp_content.h"
25 #include "dcpomatic_time_coalesce.h"
26 #include "empty_video.h"
27 #include "film.h"
28 #include "piece.h"
29 #include "playlist.h"
30 #include "video_content.h"
31 #include <iostream>
32
33
34 using std::cout;
35 using std::dynamic_pointer_cast;
36 using std::function;
37 using std::list;
38 using std::make_pair;
39 using std::pair;
40 using std::shared_ptr;
41 using namespace dcpomatic;
42
43
44 EmptyVideo::EmptyVideo (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, DCPTime length)
45 {
46         list<DCPTimePeriod> full_left;
47         list<DCPTimePeriod> full_right;
48         list<DCPTimePeriod> full_both;
49         for (auto i: playlist->content()) {
50                 if (i->video && i->video->use() && i->can_be_played() && i->paths_valid()) {
51                         auto period = DCPTimePeriod(i->position(), i->end(film));
52                         if (i->video->frame_type() == VideoFrameType::THREE_D_LEFT) {
53                                 full_left.push_back (period);
54                         } else if (i->video->frame_type() == VideoFrameType::THREE_D_RIGHT) {
55                                 full_right.push_back (period);
56                         } else {
57                                 full_both.push_back (period);
58                         }
59                 }
60         }
61
62         auto const whole = DCPTimePeriod(DCPTime(), length);
63
64         auto empty_both = subtract(subtract(subtract({whole}, full_left), full_right), full_both);
65         auto empty_left = subtract(subtract(subtract({whole}, full_left), full_both), empty_both);
66         auto empty_right = subtract(subtract(subtract({whole}, full_right), full_both), empty_both);
67
68         for (auto left: empty_left) {
69                 _periods.push_back (make_pair(left, Eyes::LEFT));
70         }
71
72         for (auto right: empty_right) {
73                 _periods.push_back (make_pair(right, Eyes::RIGHT));
74         }
75
76         for (auto both: empty_both) {
77                 _periods.push_back (make_pair(both, Eyes::BOTH));
78         }
79
80         _periods.sort([](std::pair<DCPTimePeriod, Eyes> const& a, std::pair<DCPTimePeriod, Eyes> const& b) {
81                 return a.first < b.first;
82         });
83
84         if (!_periods.empty()) {
85                 _position = _periods.front().first.from;
86         }
87 }
88
89
90 void
91 EmptyVideo::set_position (DCPTime position)
92 {
93         _position = position;
94
95         for (auto i: _periods) {
96                 if (i.first.contains(_position)) {
97                         return;
98                 }
99         }
100
101         for (auto i: _periods) {
102                 if (i.first.from > _position) {
103                         _position = i.first.from;
104                         return;
105                 }
106         }
107 }
108
109
110 pair<DCPTimePeriod, Eyes>
111 EmptyVideo::period_at_position () const
112 {
113         for (auto i: _periods) {
114                 if (i.first.contains(_position)) {
115                         return make_pair(DCPTimePeriod(_position, i.first.to), i.second);
116                 }
117         }
118
119         DCPOMATIC_ASSERT (false);
120 }
121
122
123 bool
124 EmptyVideo::done () const
125 {
126         DCPTime latest;
127         for (auto i: _periods) {
128                 latest = max (latest, i.first.to);
129         }
130
131         return _position >= latest;
132 }