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