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