Add Film::set_dirty().
[dcpomatic.git] / src / lib / types.cc
1 /*
2     Copyright (C) 2013-2019 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 "compose.hpp"
23 #include "dcpomatic_assert.h"
24 #include "types.h"
25 #include "warnings.h"
26 #include <dcp/cpl.h>
27 #include <dcp/dcp.h>
28 #include <dcp/raw_convert.h>
29 #include <dcp/reel_asset.h>
30 #include <dcp/reel_file_asset.h>
31 DCPOMATIC_DISABLE_WARNINGS
32 #include <libxml++/libxml++.h>
33 DCPOMATIC_ENABLE_WARNINGS
34 #include <libcxml/cxml.h>
35
36 #include "i18n.h"
37
38
39 using std::max;
40 using std::min;
41 using std::string;
42 using std::list;
43 using std::shared_ptr;
44 using std::vector;
45 using dcp::raw_convert;
46
47
48 bool operator== (Crop const & a, Crop const & b)
49 {
50         return (a.left == b.left && a.right == b.right && a.top == b.top && a.bottom == b.bottom);
51 }
52
53
54 bool operator!= (Crop const & a, Crop const & b)
55 {
56         return !(a == b);
57 }
58
59
60 /** @param r Resolution.
61  *  @return Untranslated string representation.
62  */
63 string
64 resolution_to_string (Resolution r)
65 {
66         switch (r) {
67         case Resolution::TWO_K:
68                 return "2K";
69         case Resolution::FOUR_K:
70                 return "4K";
71         }
72
73         DCPOMATIC_ASSERT (false);
74         return "";
75 }
76
77
78 Resolution
79 string_to_resolution (string s)
80 {
81         if (s == "2K") {
82                 return Resolution::TWO_K;
83         }
84
85         if (s == "4K") {
86                 return Resolution::FOUR_K;
87         }
88
89         DCPOMATIC_ASSERT (false);
90         return Resolution::TWO_K;
91 }
92
93
94 Crop::Crop (shared_ptr<cxml::Node> node)
95 {
96         left = node->number_child<int> ("LeftCrop");
97         right = node->number_child<int> ("RightCrop");
98         top = node->number_child<int> ("TopCrop");
99         bottom = node->number_child<int> ("BottomCrop");
100 }
101
102
103 void
104 Crop::as_xml (xmlpp::Node* node) const
105 {
106         node->add_child("LeftCrop")->add_child_text (raw_convert<string> (left));
107         node->add_child("RightCrop")->add_child_text (raw_convert<string> (right));
108         node->add_child("TopCrop")->add_child_text (raw_convert<string> (top));
109         node->add_child("BottomCrop")->add_child_text (raw_convert<string> (bottom));
110 }
111
112
113 TextType
114 string_to_text_type (string s)
115 {
116         if (s == "unknown") {
117                 return TextType::UNKNOWN;
118         } else if (s == "open-subtitle") {
119                 return TextType::OPEN_SUBTITLE;
120         } else if (s == "closed-caption") {
121                 return TextType::CLOSED_CAPTION;
122         } else {
123                 throw MetadataError (String::compose ("Unknown text type %1", s));
124         }
125 }
126
127
128 string
129 text_type_to_string (TextType t)
130 {
131         switch (t) {
132         case TextType::UNKNOWN:
133                 return "unknown";
134         case TextType::OPEN_SUBTITLE:
135                 return "open-subtitle";
136         case TextType::CLOSED_CAPTION:
137                 return "closed-caption";
138         default:
139                 DCPOMATIC_ASSERT (false);
140         }
141 }
142
143
144 string
145 text_type_to_name (TextType t)
146 {
147         switch (t) {
148         case TextType::UNKNOWN:
149                 return _("Timed text");
150         case TextType::OPEN_SUBTITLE:
151                 return _("Open subtitles");
152         case TextType::CLOSED_CAPTION:
153                 return _("Closed captions");
154         default:
155                 DCPOMATIC_ASSERT (false);
156         }
157 }
158
159
160 string
161 video_frame_type_to_string (VideoFrameType t)
162 {
163         switch (t) {
164         case VideoFrameType::TWO_D:
165                 return "2d";
166         case VideoFrameType::THREE_D:
167                 return "3d";
168         case VideoFrameType::THREE_D_LEFT_RIGHT:
169                 return "3d-left-right";
170         case VideoFrameType::THREE_D_TOP_BOTTOM:
171                 return "3d-top-bottom";
172         case VideoFrameType::THREE_D_ALTERNATE:
173                 return "3d-alternate";
174         case VideoFrameType::THREE_D_LEFT:
175                 return "3d-left";
176         case VideoFrameType::THREE_D_RIGHT:
177                 return "3d-right";
178         default:
179                 DCPOMATIC_ASSERT (false);
180         }
181
182         DCPOMATIC_ASSERT (false);
183 }
184
185
186 VideoFrameType
187 string_to_video_frame_type (string s)
188 {
189         if (s == "2d") {
190                 return VideoFrameType::TWO_D;
191         } else if (s == "3d") {
192                 return VideoFrameType::THREE_D;
193         } else if (s == "3d-left-right") {
194                 return VideoFrameType::THREE_D_LEFT_RIGHT;
195         } else if (s == "3d-top-bottom") {
196                 return VideoFrameType::THREE_D_TOP_BOTTOM;
197         } else if (s == "3d-alternate") {
198                 return VideoFrameType::THREE_D_ALTERNATE;
199         } else if (s == "3d-left") {
200                 return VideoFrameType::THREE_D_LEFT;
201         } else if (s == "3d-right") {
202                 return VideoFrameType::THREE_D_RIGHT;
203         }
204
205         DCPOMATIC_ASSERT (false);
206 }
207
208
209 CPLSummary::CPLSummary (boost::filesystem::path p)
210         : dcp_directory (p.leaf().string())
211 {
212         dcp::DCP dcp (p);
213
214         vector<dcp::VerificationNote> notes;
215         dcp.read (&notes);
216         for (auto i: notes) {
217                 if (i.code() != dcp::VerificationNote::Code::EXTERNAL_ASSET) {
218                         /* It's not just a warning about this DCP being a VF */
219                         throw dcp::ReadError(dcp::note_to_string(i));
220                 }
221         }
222
223         cpl_id = dcp.cpls().front()->id();
224         cpl_annotation_text = dcp.cpls().front()->annotation_text();
225         cpl_file = dcp.cpls().front()->file().get();
226
227         encrypted = false;
228         for (auto j: dcp.cpls()) {
229                 for (auto k: j->reel_file_assets()) {
230                         if (k->encrypted()) {
231                                 encrypted = true;
232                         }
233                 }
234         }
235
236         boost::system::error_code ec;
237         auto last_write = boost::filesystem::last_write_time (p, ec);
238         last_write_time = ec ? 0 : last_write;
239 }
240
241
242 bool operator== (NamedChannel const& a, NamedChannel const& b)
243 {
244         return a.name == b.name && a.index == b.index;
245 }
246
247
248 string
249 video_range_to_string (VideoRange r)
250 {
251         switch (r) {
252         case VideoRange::FULL:
253                 return "full";
254         case VideoRange::VIDEO:
255                 return "video";
256         default:
257                 DCPOMATIC_ASSERT (false);
258         }
259 }
260
261
262 VideoRange
263 string_to_video_range (string s)
264 {
265         if (s == "full") {
266                 return VideoRange::FULL;
267         } else if (s == "video") {
268                 return VideoRange::VIDEO;
269         }
270
271         DCPOMATIC_ASSERT (false);
272         return VideoRange::FULL;
273 }
274