Basic writing of DCPs containing Atmos MXFs; untested.
[libdcp.git] / src / reel.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "reel.h"
21 #include "util.h"
22 #include "picture_asset.h"
23 #include "mono_picture_asset.h"
24 #include "stereo_picture_asset.h"
25 #include "sound_asset.h"
26 #include "subtitle_asset.h"
27 #include "reel_mono_picture_asset.h"
28 #include "reel_stereo_picture_asset.h"
29 #include "reel_sound_asset.h"
30 #include "reel_subtitle_asset.h"
31 #include "decrypted_kdm_key.h"
32 #include "decrypted_kdm.h"
33 #include "interop_subtitle_asset.h"
34 #include "reel_atmos_asset.h"
35 #include <libxml++/nodes/element.h>
36
37 using std::string;
38 using std::list;
39 using std::cout;
40 using std::max;
41 using boost::shared_ptr;
42 using boost::dynamic_pointer_cast;
43 using namespace dcp;
44
45 Reel::Reel (boost::shared_ptr<const cxml::Node> node)
46         : Object (remove_urn_uuid (node->string_child ("Id")))
47 {
48         shared_ptr<cxml::Node> asset_list = node->node_child ("AssetList");
49
50         shared_ptr<cxml::Node> main_picture = asset_list->optional_node_child ("MainPicture");
51         if (main_picture) {
52                 _main_picture.reset (new ReelMonoPictureAsset (main_picture));
53         }
54
55         shared_ptr<cxml::Node> main_stereoscopic_picture = asset_list->optional_node_child ("MainStereoscopicPicture");
56         if (main_stereoscopic_picture) {
57                 _main_picture.reset (new ReelStereoPictureAsset (main_stereoscopic_picture));
58         }
59
60         shared_ptr<cxml::Node> main_sound = asset_list->optional_node_child ("MainSound");
61         if (main_sound) {
62                 _main_sound.reset (new ReelSoundAsset (main_sound));
63         }
64
65         shared_ptr<cxml::Node> main_subtitle = asset_list->optional_node_child ("MainSubtitle");
66         if (main_subtitle) {
67                 _main_subtitle.reset (new ReelSubtitleAsset (main_subtitle));
68         }
69
70         shared_ptr<cxml::Node> atmos = asset_list->optional_node_child ("axd:AuxData");
71         if (atmos) {
72                 _atmos.reset (new ReelAtmosAsset (atmos));
73         }
74
75         node->ignore_child ("AnnotationText");
76         node->done ();
77 }
78
79 void
80 Reel::write_to_cpl (xmlpp::Element* node, Standard standard) const
81 {
82         xmlpp::Element* reel = node->add_child ("Reel");
83         reel->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
84         xmlpp::Element* asset_list = reel->add_child ("AssetList");
85
86         if (_main_picture && dynamic_pointer_cast<ReelMonoPictureAsset> (_main_picture)) {
87                 /* Mono pictures come before other stuff... */
88                 _main_picture->write_to_cpl (asset_list, standard);
89         }
90
91         if (_main_sound) {
92                 _main_sound->write_to_cpl (asset_list, standard);
93         }
94
95         if (_main_subtitle) {
96                 _main_subtitle->write_to_cpl (asset_list, standard);
97         }
98
99         if (_main_picture && dynamic_pointer_cast<ReelStereoPictureAsset> (_main_picture)) {
100                 /* ... but stereo pictures must come after */
101                 _main_picture->write_to_cpl (asset_list, standard);
102         }
103
104         if (_atmos) {
105                 _atmos->write_to_cpl (asset_list, standard);
106         }
107 }
108
109 bool
110 Reel::equals (boost::shared_ptr<const Reel> other, EqualityOptions opt, NoteHandler note) const
111 {
112         if ((_main_picture && !other->_main_picture) || (!_main_picture && other->_main_picture)) {
113                 note (DCP_ERROR, "Reel: assets differ");
114                 return false;
115         }
116
117         if (_main_picture && !_main_picture->equals (other->_main_picture, opt, note)) {
118                 return false;
119         }
120
121         if ((_main_sound && !other->_main_sound) || (!_main_sound && other->_main_sound)) {
122                 note (DCP_ERROR, "Reel: assets differ");
123                 return false;
124         }
125
126         if (_main_sound && !_main_sound->equals (other->_main_sound, opt, note)) {
127                 return false;
128         }
129
130         if ((_main_subtitle && !other->_main_subtitle) || (!_main_subtitle && other->_main_subtitle)) {
131                 note (DCP_ERROR, "Reel: assets differ");
132                 return false;
133         }
134
135         if (_main_subtitle && !_main_subtitle->equals (other->_main_subtitle, opt, note)) {
136                 return false;
137         }
138
139         if ((_atmos && !other->_atmos) || (!_atmos && other->_atmos)) {
140                 note (DCP_ERROR, "Reel: assets differ");
141                 return false;
142         }
143
144         if (_atmos && !_atmos->equals (other->_atmos, opt, note)) {
145                 return false;
146         }
147
148         return true;
149 }
150
151 bool
152 Reel::encrypted () const
153 {
154         return (
155                 (_main_picture && _main_picture->encrypted ()) ||
156                 (_main_sound && _main_sound->encrypted ()) ||
157                 (_atmos && _atmos->encrypted ())
158                 );
159 }
160
161 void
162 Reel::add (DecryptedKDM const & kdm)
163 {
164         list<DecryptedKDMKey> keys = kdm.keys ();
165
166         for (list<DecryptedKDMKey>::iterator i = keys.begin(); i != keys.end(); ++i) {
167                 if (i->id() == _main_picture->key_id()) {
168                         _main_picture->asset()->set_key (i->key ());
169                 }
170                 if (i->id() == _main_sound->key_id()) {
171                         _main_sound->asset()->set_key (i->key ());
172                 }
173                 if (i->id() == _atmos->key_id()) {
174                         _atmos->asset()->set_key (i->key ());
175                 }
176         }
177 }
178
179 void
180 Reel::add (shared_ptr<ReelAsset> asset)
181 {
182         shared_ptr<ReelPictureAsset> p = dynamic_pointer_cast<ReelPictureAsset> (asset);
183         shared_ptr<ReelSoundAsset> so = dynamic_pointer_cast<ReelSoundAsset> (asset);
184         shared_ptr<ReelSubtitleAsset> su = dynamic_pointer_cast<ReelSubtitleAsset> (asset);
185         shared_ptr<ReelAtmosAsset> a = dynamic_pointer_cast<ReelAtmosAsset> (asset);
186         if (p) {
187                 _main_picture = p;
188         } else if (so) {
189                 _main_sound = so;
190         } else if (su) {
191                 _main_subtitle = su;
192         } else if (a) {
193                 _atmos = a;
194         }
195 }
196
197 void
198 Reel::resolve_refs (list<shared_ptr<Asset> > assets)
199 {
200         if (_main_picture) {
201                 _main_picture->asset_ref().resolve (assets);
202         }
203
204         if (_main_sound) {
205                 _main_sound->asset_ref().resolve (assets);
206         }
207
208         if (_main_subtitle) {
209                 _main_subtitle->asset_ref().resolve (assets);
210
211                 /* Interop subtitle handling is all special cases */
212                 shared_ptr<InteropSubtitleAsset> iop = dynamic_pointer_cast<InteropSubtitleAsset> (_main_subtitle->asset_ref().asset ());
213                 if (iop) {
214                         iop->resolve_fonts (assets);
215                 }
216         }
217
218         if (_atmos) {
219                 _atmos->asset_ref().resolve (assets);
220         }
221 }
222
223 int64_t
224 Reel::duration () const
225 {
226         int64_t d = 0;
227
228         if (_main_picture) {
229                 d = max (d, _main_picture->duration ());
230         }
231         if (_main_sound) {
232                 d = max (d, _main_sound->duration ());
233         }
234         if (_main_subtitle) {
235                 d = max (d, _main_subtitle->duration ());
236         }
237         if (_atmos) {
238                 d = max (d, _atmos->duration ());
239         }
240
241         return d;
242 }