6bac0cf3efe0298f0e734b10d6a1e60567f0a379
[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 <dcp/cpl.h>
25 #include <dcp/dcp.h>
26 #include <dcp/raw_convert.h>
27 #include <dcp/reel_asset.h>
28 #include <dcp/reel_file_asset.h>
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <libxml++/libxml++.h>
32 LIBDCP_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 std::vector;
43 using dcp::raw_convert;
44
45
46 /** @param r Resolution.
47  *  @return Untranslated string representation.
48  */
49 string
50 resolution_to_string (Resolution r)
51 {
52         switch (r) {
53         case Resolution::TWO_K:
54                 return "2K";
55         case Resolution::FOUR_K:
56                 return "4K";
57         }
58
59         DCPOMATIC_ASSERT (false);
60         return "";
61 }
62
63
64 Resolution
65 string_to_resolution (string s)
66 {
67         if (s == "2K") {
68                 return Resolution::TWO_K;
69         }
70
71         if (s == "4K") {
72                 return Resolution::FOUR_K;
73         }
74
75         DCPOMATIC_ASSERT (false);
76         return Resolution::TWO_K;
77 }
78
79
80 TextType
81 string_to_text_type (string s)
82 {
83         if (s == "unknown") {
84                 return TextType::UNKNOWN;
85         } else if (s == "open-subtitle") {
86                 return TextType::OPEN_SUBTITLE;
87         } else if (s == "closed-caption") {
88                 return TextType::CLOSED_CAPTION;
89         } else {
90                 throw MetadataError (String::compose ("Unknown text type %1", s));
91         }
92 }
93
94 string
95 text_type_to_string (TextType t)
96 {
97         switch (t) {
98         case TextType::UNKNOWN:
99                 return "unknown";
100         case TextType::OPEN_SUBTITLE:
101                 return "open-subtitle";
102         case TextType::CLOSED_CAPTION:
103                 return "closed-caption";
104         default:
105                 DCPOMATIC_ASSERT (false);
106         }
107 }
108
109 string
110 text_type_to_name (TextType t)
111 {
112         switch (t) {
113         case TextType::UNKNOWN:
114                 return _("Timed text");
115         case TextType::OPEN_SUBTITLE:
116                 return _("Open subtitles");
117         case TextType::CLOSED_CAPTION:
118                 return _("Closed captions");
119         default:
120                 DCPOMATIC_ASSERT (false);
121         }
122 }
123
124 string
125 video_frame_type_to_string (VideoFrameType t)
126 {
127         switch (t) {
128         case VideoFrameType::TWO_D:
129                 return "2d";
130         case VideoFrameType::THREE_D:
131                 return "3d";
132         case VideoFrameType::THREE_D_LEFT_RIGHT:
133                 return "3d-left-right";
134         case VideoFrameType::THREE_D_TOP_BOTTOM:
135                 return "3d-top-bottom";
136         case VideoFrameType::THREE_D_ALTERNATE:
137                 return "3d-alternate";
138         case VideoFrameType::THREE_D_LEFT:
139                 return "3d-left";
140         case VideoFrameType::THREE_D_RIGHT:
141                 return "3d-right";
142         default:
143                 DCPOMATIC_ASSERT (false);
144         }
145
146         DCPOMATIC_ASSERT (false);
147 }
148
149 VideoFrameType
150 string_to_video_frame_type (string s)
151 {
152         if (s == "2d") {
153                 return VideoFrameType::TWO_D;
154         } else if (s == "3d") {
155                 return VideoFrameType::THREE_D;
156         } else if (s == "3d-left-right") {
157                 return VideoFrameType::THREE_D_LEFT_RIGHT;
158         } else if (s == "3d-top-bottom") {
159                 return VideoFrameType::THREE_D_TOP_BOTTOM;
160         } else if (s == "3d-alternate") {
161                 return VideoFrameType::THREE_D_ALTERNATE;
162         } else if (s == "3d-left") {
163                 return VideoFrameType::THREE_D_LEFT;
164         } else if (s == "3d-right") {
165                 return VideoFrameType::THREE_D_RIGHT;
166         }
167
168         DCPOMATIC_ASSERT (false);
169 }
170
171 CPLSummary::CPLSummary (boost::filesystem::path p)
172         : dcp_directory (p.leaf().string())
173 {
174         dcp::DCP dcp (p);
175
176         vector<dcp::VerificationNote> notes;
177         dcp.read (&notes);
178         for (auto i: notes) {
179                 if (i.code() != dcp::VerificationNote::Code::EXTERNAL_ASSET) {
180                         /* It's not just a warning about this DCP being a VF */
181                         throw dcp::ReadError(dcp::note_to_string(i));
182                 }
183         }
184
185         cpl_id = dcp.cpls().front()->id();
186         cpl_annotation_text = dcp.cpls().front()->annotation_text();
187         cpl_file = dcp.cpls().front()->file().get();
188
189         encrypted = false;
190         for (auto j: dcp.cpls()) {
191                 for (auto k: j->reel_file_assets()) {
192                         if (k->encrypted()) {
193                                 encrypted = true;
194                         }
195                 }
196         }
197
198         boost::system::error_code ec;
199         auto last_write = boost::filesystem::last_write_time (p, ec);
200         last_write_time = ec ? 0 : last_write;
201 }
202
203
204 bool operator== (NamedChannel const& a, NamedChannel const& b)
205 {
206         return a.name == b.name && a.index == b.index;
207 }
208
209
210 string
211 video_range_to_string (VideoRange r)
212 {
213         switch (r) {
214         case VideoRange::FULL:
215                 return "full";
216         case VideoRange::VIDEO:
217                 return "video";
218         default:
219                 DCPOMATIC_ASSERT (false);
220         }
221 }
222
223
224 VideoRange
225 string_to_video_range (string s)
226 {
227         if (s == "full") {
228                 return VideoRange::FULL;
229         } else if (s == "video") {
230                 return VideoRange::VIDEO;
231         }
232
233         DCPOMATIC_ASSERT (false);
234         return VideoRange::FULL;
235 }
236