09cd46343abd8a8c026983e481474d08f7a130c0
[libdcp.git] / src / smpte_subtitle_asset.h
1 /*
2     Copyright (C) 2012-2020 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 /** @file  src/smpte_subtitle_asset.h
35  *  @brief SMPTESubtitleAsset class.
36  */
37
38 #include "subtitle_asset.h"
39 #include "language_tag.h"
40 #include "local_time.h"
41 #include "mxf.h"
42 #include "crypto_context.h"
43 #include <boost/filesystem.hpp>
44
45 namespace ASDCP {
46         namespace TimedText {
47                 class MXFReader;
48         }
49 }
50
51 namespace dcp {
52
53 class SMPTELoadFontNode;
54
55 /** @class SMPTESubtitleAsset
56  *  @brief A set of subtitles to be read and/or written in the SMPTE format.
57  */
58 class SMPTESubtitleAsset : public SubtitleAsset, public MXF
59 {
60 public:
61         SMPTESubtitleAsset ();
62
63         /** @param file File name
64          */
65         explicit SMPTESubtitleAsset (boost::filesystem::path file);
66
67         bool equals (
68                 boost::shared_ptr<const Asset>,
69                 EqualityOptions,
70                 NoteHandler note
71                 ) const;
72
73         std::list<boost::shared_ptr<LoadFontNode> > load_font_nodes () const;
74
75         std::string xml_as_string () const;
76         void write (boost::filesystem::path path) const;
77         void add (boost::shared_ptr<Subtitle>);
78         void add_font (std::string id, boost::filesystem::path file);
79         void set_key (Key key);
80
81         void set_content_title_text (std::string t) {
82                 _content_title_text = t;
83         }
84
85         void set_language (dcp::LanguageTag l) {
86                 _language = l.to_string();
87         }
88
89         void set_issue_date (LocalTime t) {
90                 _issue_date = t;
91         }
92
93         void set_reel_number (int r) {
94                 _reel_number = r;
95         }
96
97         void set_edit_rate (Fraction e) {
98                 _edit_rate = e;
99         }
100
101         void set_time_code_rate (int t) {
102                 _time_code_rate = t;
103         }
104
105         void set_start_time (Time t) {
106                 _start_time = t;
107         }
108
109         void set_intrinsic_duration (int64_t d) {
110                 _intrinsic_duration = d;
111         }
112
113         /** @return title of the film that these subtitles are for,
114          *  to be presented to the user.
115          */
116         std::string content_title_text () const {
117                 return _content_title_text;
118         }
119
120         /** @return Language, if one was set.  This should be a xs:language, but
121          *  it might not be if a non-compliant DCP was read in.
122          */
123         boost::optional<std::string> language () const {
124                 return _language;
125         }
126
127         /** @return annotation text, to be presented to the user */
128         boost::optional<std::string> annotation_text () const {
129                 return _annotation_text;
130         }
131
132         /** @return file creation time and date */
133         LocalTime issue_date () const {
134                 return _issue_date;
135         }
136
137         boost::optional<int> reel_number () const {
138                 return _reel_number;
139         }
140
141         Fraction edit_rate () const {
142                 return _edit_rate;
143         }
144
145         /** @return subdivision of 1 second that is used for subtitle times;
146          *  e.g. a time_code_rate of 250 means that a subtitle time of 0:0:0:001
147          *  represents 4ms.
148          */
149         int time_code_rate () const {
150                 return _time_code_rate;
151         }
152
153         boost::optional<Time> start_time () const {
154                 return _start_time;
155         }
156
157         std::string xml_id () const {
158                 return _xml_id;
159         }
160
161         static bool valid_mxf (boost::filesystem::path);
162         static std::string static_pkl_type (Standard) {
163                 return "application/mxf";
164         }
165
166 protected:
167
168         std::string pkl_type (Standard s) const {
169                 return static_pkl_type (s);
170         }
171
172 private:
173         friend struct ::write_smpte_subtitle_test;
174         friend struct ::write_smpte_subtitle_test2;
175
176         void read_fonts (boost::shared_ptr<ASDCP::TimedText::MXFReader>);
177         void parse_xml (boost::shared_ptr<cxml::Document> xml);
178         void read_mxf_descriptor (boost::shared_ptr<ASDCP::TimedText::MXFReader> reader, boost::shared_ptr<DecryptionContext> dec);
179
180         /** The total length of this content in video frames.  The amount of
181          *  content presented may be less than this.
182          */
183         int64_t _intrinsic_duration;
184         /** <ContentTitleText> from the asset */
185         std::string _content_title_text;
186         /** This is stored and returned as a string so that we can tolerate non-RFC-5646 strings,
187          *  but must be set as a dcp::LanguageTag to try to ensure that we create compliant output.
188          */
189         boost::optional<std::string> _language;
190         boost::optional<std::string> _annotation_text;
191         LocalTime _issue_date;
192         boost::optional<int> _reel_number;
193         Fraction _edit_rate;
194         int _time_code_rate;
195         boost::optional<Time> _start_time;
196
197         std::list<boost::shared_ptr<SMPTELoadFontNode> > _load_font_nodes;
198         /** UUID for the XML inside the MXF, which should be different to the ID of the MXF according to
199          *  Doremi's 2.8.18 release notes.
200          */
201         std::string _xml_id;
202 };
203
204 }