1ece2405c10952028648a4841c79d99915e1ea02
[libdcp.git] / src / smpte_subtitle_asset.h
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #ifndef LIBDCP_SMPTE_SUBTITLE_ASSET_H
36 #define LIBDCP_SMPTE_SUBTITLE_ASSET_H
37
38
39 /** @file  src/smpte_subtitle_asset.h
40  *  @brief SMPTESubtitleAsset class
41  */
42
43
44 #include "subtitle_asset.h"
45 #include "language_tag.h"
46 #include "local_time.h"
47 #include "mxf.h"
48 #include "crypto_context.h"
49 #include <boost/filesystem.hpp>
50
51
52 namespace ASDCP {
53         namespace TimedText {
54                 class MXFReader;
55         }
56 }
57
58
59 struct verify_invalid_language1;
60 struct verify_invalid_language2;
61
62
63 namespace dcp {
64
65
66 class SMPTELoadFontNode;
67
68
69 /** @class SMPTESubtitleAsset
70  *  @brief A set of subtitles to be read and/or written in the SMPTE format
71  */
72 class SMPTESubtitleAsset : public SubtitleAsset, public MXF
73 {
74 public:
75         SMPTESubtitleAsset ();
76
77         /** Construct a SMPTESubtitleAsset by reading an MXF or XML file
78          *  @param file Filename
79          */
80         explicit SMPTESubtitleAsset (boost::filesystem::path file);
81
82         bool equals (
83                 std::shared_ptr<const Asset>,
84                 EqualityOptions,
85                 NoteHandler note
86                 ) const override;
87
88         std::vector<std::shared_ptr<LoadFontNode>> load_font_nodes () const override;
89
90         std::string xml_as_string () const override;
91
92         /** Write this content to a MXF file */
93         void write (boost::filesystem::path path) const override;
94
95         void add (std::shared_ptr<Subtitle>) override;
96         void add_font (std::string id, dcp::ArrayData data) override;
97         void set_key (Key key) override;
98
99         void set_content_title_text (std::string t) {
100                 _content_title_text = t;
101         }
102
103         void set_language (dcp::LanguageTag l) {
104                 _language = l.to_string();
105         }
106
107         void set_issue_date (LocalTime t) {
108                 _issue_date = t;
109         }
110
111         void set_reel_number (int r) {
112                 _reel_number = r;
113         }
114
115         void set_edit_rate (Fraction e) {
116                 _edit_rate = e;
117         }
118
119         void set_time_code_rate (int t) {
120                 _time_code_rate = t;
121         }
122
123         void set_start_time (Time t) {
124                 _start_time = t;
125         }
126
127         void set_intrinsic_duration (int64_t d) {
128                 _intrinsic_duration = d;
129         }
130
131         int64_t intrinsic_duration () const {
132                 return _intrinsic_duration;
133         }
134
135         /** @return title of the film that these subtitles are for,
136          *  to be presented to the user
137          */
138         std::string content_title_text () const {
139                 return _content_title_text;
140         }
141
142         /** @return Language, if one was set.  This should be a xs:language, but
143          *  it might not be if a non-compliant DCP was read in.
144          */
145         boost::optional<std::string> language () const {
146                 return _language;
147         }
148
149         /** @return annotation text, to be presented to the user */
150         boost::optional<std::string> annotation_text () const {
151                 return _annotation_text;
152         }
153
154         /** @return file issue time and date */
155         LocalTime issue_date () const {
156                 return _issue_date;
157         }
158
159         boost::optional<int> reel_number () const {
160                 return _reel_number;
161         }
162
163         Fraction edit_rate () const {
164                 return _edit_rate;
165         }
166
167         /** @return subdivision of 1 second that is used for subtitle times;
168          *  e.g. a time_code_rate of 250 means that a subtitle time of 0:0:0:001
169          *  represents 4ms.
170          */
171         int time_code_rate () const override {
172                 return _time_code_rate;
173         }
174
175         boost::optional<Time> start_time () const {
176                 return _start_time;
177         }
178
179         std::string xml_id () const {
180                 return _xml_id;
181         }
182
183         /** @return ResourceID read from the MXF, if there was one */
184         boost::optional<std::string> resource_id () const {
185                 return _resource_id;
186         }
187
188         static bool valid_mxf (boost::filesystem::path);
189         static std::string static_pkl_type (Standard) {
190                 return "application/mxf";
191         }
192
193 protected:
194
195         std::string pkl_type (Standard s) const override {
196                 return static_pkl_type (s);
197         }
198
199 private:
200         friend struct ::write_smpte_subtitle_test;
201         friend struct ::write_smpte_subtitle_test2;
202         friend struct ::verify_invalid_language1;
203         friend struct ::verify_invalid_language2;
204
205         void read_fonts (std::shared_ptr<ASDCP::TimedText::MXFReader>);
206         void parse_xml (std::shared_ptr<cxml::Document> xml);
207         void read_mxf_descriptor (std::shared_ptr<ASDCP::TimedText::MXFReader> reader, std::shared_ptr<DecryptionContext> dec);
208
209         /** The total length of this content in video frames.  The amount of
210          *  content presented may be less than this.
211          */
212         int64_t _intrinsic_duration = 0;
213         /** <ContentTitleText> from the asset */
214         std::string _content_title_text;
215         /** This is stored and returned as a string so that we can tolerate non-RFC-5646 strings,
216          *  but must be set as a dcp::LanguageTag to try to ensure that we create compliant output.
217          */
218         boost::optional<std::string> _language;
219         boost::optional<std::string> _annotation_text;
220         LocalTime _issue_date;
221         boost::optional<int> _reel_number;
222         Fraction _edit_rate;
223         int _time_code_rate = 0;
224         boost::optional<Time> _start_time;
225
226         std::vector<std::shared_ptr<SMPTELoadFontNode>> _load_font_nodes;
227         /** UUID for the XML inside the MXF, which should be the same as the ResourceID in the MXF (our _resource_id)
228          *  but different to the AssetUUID in the MXF (our _id) according to SMPTE Bv2.1 and Doremi's 2.8.18 release notes.
229          */
230         std::string _xml_id;
231
232         /** ResourceID read from the MXF, if there was one */
233         boost::optional<std::string> _resource_id;
234 };
235
236
237 }
238
239
240 #endif