Various fixes to incorrect Atmos read/write wrt XML namespaces.
[libdcp.git] / src / reel.cc
1 /*
2     Copyright (C) 2014-2016 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 #include "reel.h"
35 #include "util.h"
36 #include "picture_asset.h"
37 #include "mono_picture_asset.h"
38 #include "stereo_picture_asset.h"
39 #include "sound_asset.h"
40 #include "subtitle_asset.h"
41 #include "reel_mono_picture_asset.h"
42 #include "reel_stereo_picture_asset.h"
43 #include "reel_sound_asset.h"
44 #include "reel_subtitle_asset.h"
45 #include "decrypted_kdm_key.h"
46 #include "decrypted_kdm.h"
47 #include "interop_subtitle_asset.h"
48 #include "smpte_subtitle_asset.h"
49 #include "reel_atmos_asset.h"
50 #include <libxml++/nodes/element.h>
51
52 using std::string;
53 using std::list;
54 using std::cout;
55 using std::max;
56 using boost::shared_ptr;
57 using boost::dynamic_pointer_cast;
58 using namespace dcp;
59
60 Reel::Reel (boost::shared_ptr<const cxml::Node> node)
61         : Object (remove_urn_uuid (node->string_child ("Id")))
62 {
63         shared_ptr<cxml::Node> asset_list = node->node_child ("AssetList");
64
65         shared_ptr<cxml::Node> main_picture = asset_list->optional_node_child ("MainPicture");
66         if (main_picture) {
67                 _main_picture.reset (new ReelMonoPictureAsset (main_picture));
68         }
69
70         shared_ptr<cxml::Node> main_stereoscopic_picture = asset_list->optional_node_child ("MainStereoscopicPicture");
71         if (main_stereoscopic_picture) {
72                 _main_picture.reset (new ReelStereoPictureAsset (main_stereoscopic_picture));
73         }
74
75         shared_ptr<cxml::Node> main_sound = asset_list->optional_node_child ("MainSound");
76         if (main_sound) {
77                 _main_sound.reset (new ReelSoundAsset (main_sound));
78         }
79
80         shared_ptr<cxml::Node> main_subtitle = asset_list->optional_node_child ("MainSubtitle");
81         if (main_subtitle) {
82                 _main_subtitle.reset (new ReelSubtitleAsset (main_subtitle));
83         }
84
85         shared_ptr<cxml::Node> atmos = asset_list->optional_node_child ("AuxData");
86         if (atmos) {
87                 _atmos.reset (new ReelAtmosAsset (atmos));
88         }
89
90         node->ignore_child ("AnnotationText");
91         node->done ();
92 }
93
94 void
95 Reel::write_to_cpl (xmlpp::Element* node, Standard standard) const
96 {
97         xmlpp::Element* reel = node->add_child ("Reel");
98         reel->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
99         xmlpp::Element* asset_list = reel->add_child ("AssetList");
100
101         if (_main_picture && dynamic_pointer_cast<ReelMonoPictureAsset> (_main_picture)) {
102                 /* Mono pictures come before other stuff... */
103                 _main_picture->write_to_cpl (asset_list, standard);
104         }
105
106         if (_main_sound) {
107                 _main_sound->write_to_cpl (asset_list, standard);
108         }
109
110         if (_main_subtitle) {
111                 _main_subtitle->write_to_cpl (asset_list, standard);
112         }
113
114         if (_main_picture && dynamic_pointer_cast<ReelStereoPictureAsset> (_main_picture)) {
115                 /* ... but stereo pictures must come after */
116                 _main_picture->write_to_cpl (asset_list, standard);
117         }
118
119         if (_atmos) {
120                 _atmos->write_to_cpl (asset_list, standard);
121         }
122 }
123
124 bool
125 Reel::equals (boost::shared_ptr<const Reel> other, EqualityOptions opt, NoteHandler note) const
126 {
127         if ((_main_picture && !other->_main_picture) || (!_main_picture && other->_main_picture)) {
128                 note (DCP_ERROR, "Reel: assets differ");
129                 return false;
130         }
131
132         if (_main_picture && !_main_picture->equals (other->_main_picture, opt, note)) {
133                 return false;
134         }
135
136         if ((_main_sound && !other->_main_sound) || (!_main_sound && other->_main_sound)) {
137                 note (DCP_ERROR, "Reel: assets differ");
138                 return false;
139         }
140
141         if (_main_sound && !_main_sound->equals (other->_main_sound, opt, note)) {
142                 return false;
143         }
144
145         if ((_main_subtitle && !other->_main_subtitle) || (!_main_subtitle && other->_main_subtitle)) {
146                 note (DCP_ERROR, "Reel: assets differ");
147                 return false;
148         }
149
150         if (_main_subtitle && !_main_subtitle->equals (other->_main_subtitle, opt, note)) {
151                 return false;
152         }
153
154         if ((_atmos && !other->_atmos) || (!_atmos && other->_atmos)) {
155                 note (DCP_ERROR, "Reel: assets differ");
156                 return false;
157         }
158
159         if (_atmos && !_atmos->equals (other->_atmos, opt, note)) {
160                 return false;
161         }
162
163         return true;
164 }
165
166 bool
167 Reel::encrypted () const
168 {
169         return (
170                 (_main_picture && _main_picture->encrypted ()) ||
171                 (_main_sound && _main_sound->encrypted ()) ||
172                 (_atmos && _atmos->encrypted ())
173                 );
174 }
175
176 void
177 Reel::add (DecryptedKDM const & kdm)
178 {
179         list<DecryptedKDMKey> keys = kdm.keys ();
180
181         for (list<DecryptedKDMKey>::iterator i = keys.begin(); i != keys.end(); ++i) {
182                 if (_main_picture && i->id() == _main_picture->key_id()) {
183                         _main_picture->asset()->set_key (i->key ());
184                 }
185                 if (_main_sound && i->id() == _main_sound->key_id()) {
186                         _main_sound->asset()->set_key (i->key ());
187                 }
188                 if (_main_subtitle && i->id() == _main_subtitle->key_id()) {
189                         shared_ptr<SMPTESubtitleAsset> s = dynamic_pointer_cast<SMPTESubtitleAsset> (_main_subtitle->asset());
190                         if (s) {
191                                 s->set_key (i->key ());
192                         }
193                 }
194                 if (_atmos && i->id() == _atmos->key_id()) {
195                         _atmos->asset()->set_key (i->key ());
196                 }
197         }
198 }
199
200 void
201 Reel::add (shared_ptr<ReelAsset> asset)
202 {
203         shared_ptr<ReelPictureAsset> p = dynamic_pointer_cast<ReelPictureAsset> (asset);
204         shared_ptr<ReelSoundAsset> so = dynamic_pointer_cast<ReelSoundAsset> (asset);
205         shared_ptr<ReelSubtitleAsset> su = dynamic_pointer_cast<ReelSubtitleAsset> (asset);
206         shared_ptr<ReelAtmosAsset> a = dynamic_pointer_cast<ReelAtmosAsset> (asset);
207         if (p) {
208                 _main_picture = p;
209         } else if (so) {
210                 _main_sound = so;
211         } else if (su) {
212                 _main_subtitle = su;
213         } else if (a) {
214                 _atmos = a;
215         }
216 }
217
218 void
219 Reel::resolve_refs (list<shared_ptr<Asset> > assets)
220 {
221         if (_main_picture) {
222                 _main_picture->asset_ref().resolve (assets);
223         }
224
225         if (_main_sound) {
226                 _main_sound->asset_ref().resolve (assets);
227         }
228
229         if (_main_subtitle) {
230                 _main_subtitle->asset_ref().resolve (assets);
231
232                 /* Interop subtitle handling is all special cases */
233                 shared_ptr<InteropSubtitleAsset> iop = dynamic_pointer_cast<InteropSubtitleAsset> (_main_subtitle->asset_ref().asset ());
234                 if (iop) {
235                         iop->resolve_fonts (assets);
236                 }
237         }
238
239         if (_atmos) {
240                 _atmos->asset_ref().resolve (assets);
241         }
242 }
243
244 int64_t
245 Reel::duration () const
246 {
247         int64_t d = 0;
248
249         if (_main_picture) {
250                 d = max (d, _main_picture->duration ());
251         }
252         if (_main_sound) {
253                 d = max (d, _main_sound->duration ());
254         }
255         if (_main_subtitle) {
256                 d = max (d, _main_subtitle->duration ());
257         }
258         if (_atmos) {
259                 d = max (d, _atmos->duration ());
260         }
261
262         return d;
263 }