fd7056809009efb30ab5c9265f09ed336f49b046
[libdcp.git] / src / cpl.cc
1 /*
2     Copyright (C) 2012 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 <fstream>
21 #include "cpl.h"
22 #include "parse/cpl.h"
23 #include "util.h"
24 #include "picture_asset.h"
25 #include "sound_asset.h"
26 #include "subtitle_asset.h"
27 #include "parse/asset_map.h"
28 #include "reel.h"
29 #include "metadata.h"
30
31 using std::string;
32 using std::stringstream;
33 using std::ofstream;
34 using std::ostream;
35 using std::list;
36 using boost::shared_ptr;
37 using boost::lexical_cast;
38 using namespace libdcp;
39
40 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
41         : _directory (directory)
42         , _name (name)
43         , _content_kind (content_kind)
44         , _length (length)
45         , _fps (frames_per_second)
46 {
47         _uuid = make_uuid ();
48 }
49
50 /** Construct a CPL object from a XML file.
51  *  @param directory The directory containing this CPL's DCP.
52  *  @param file The CPL XML filename.
53  *  @param asset_map The corresponding asset map.
54  *  @param require_mxfs true to throw an exception if a required MXF file does not exist.
55  */
56 CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMap> asset_map, bool require_mxfs)
57         : _directory (directory)
58         , _content_kind (FEATURE)
59         , _length (0)
60         , _fps (0)
61 {
62         /* Read the XML */
63         shared_ptr<parse::CPL> cpl;
64         try {
65                 cpl.reset (new parse::CPL (file));
66         } catch (FileError& e) {
67                 boost::throw_exception (FileError ("could not load CPL file", file));
68         }
69         
70         /* Now cherry-pick the required bits into our own data structure */
71         
72         _name = cpl->annotation_text;
73         _content_kind = cpl->content_kind;
74
75         for (list<shared_ptr<libdcp::parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
76
77                 shared_ptr<parse::Picture> p;
78
79                 if ((*i)->asset_list->main_picture) {
80                         p = (*i)->asset_list->main_picture;
81                 } else {
82                         p = (*i)->asset_list->main_stereoscopic_picture;
83                 }
84                 
85                 _fps = p->edit_rate.numerator;
86                 _length += p->duration;
87
88                 shared_ptr<PictureAsset> picture;
89                 shared_ptr<SoundAsset> sound;
90                 shared_ptr<SubtitleAsset> subtitle;
91
92                 /* Some rather twisted logic to decide if we are 3D or not;
93                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
94                    just have a FrameRate twice the EditRate and apparently
95                    expect you to divine the fact that they are hence 3D.
96                 */
97
98                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
99
100                         try {
101                                 picture.reset (new MonoPictureAsset (
102                                                        _directory,
103                                                        asset_map->asset_from_id (p->id)->chunks.front()->path
104                                                        )
105                                         );
106
107                                 picture->set_entry_point (p->entry_point);
108                                 picture->set_duration (p->duration);
109                         } catch (MXFFileError) {
110                                 if (require_mxfs) {
111                                         throw;
112                                 }
113                         }
114                         
115                 } else {
116                         try {
117                                 picture.reset (new StereoPictureAsset (
118                                                        _directory,
119                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
120                                                        _fps,
121                                                        p->duration
122                                                        )
123                                         );
124
125                                 picture->set_entry_point (p->entry_point);
126                                 picture->set_duration (p->duration);
127                                 
128                         } catch (MXFFileError) {
129                                 if (require_mxfs) {
130                                         throw;
131                                 }
132                         }
133                         
134                 }
135                 
136                 if ((*i)->asset_list->main_sound) {
137                         
138                         try {
139                                 sound.reset (new SoundAsset (
140                                                      _directory,
141                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path
142                                                      )
143                                         );
144
145                                 sound->set_entry_point ((*i)->asset_list->main_sound->entry_point);
146                                 sound->set_duration ((*i)->asset_list->main_sound->duration);
147                         } catch (MXFFileError) {
148                                 if (require_mxfs) {
149                                         throw;
150                                 }
151                         }
152                 }
153
154                 if ((*i)->asset_list->main_subtitle) {
155                         
156                         subtitle.reset (new SubtitleAsset (
157                                                 _directory,
158                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
159                                                 )
160                                 );
161
162                         subtitle->set_entry_point ((*i)->asset_list->main_subtitle->entry_point);
163                         subtitle->set_duration ((*i)->asset_list->main_subtitle->duration);
164                 }
165                         
166                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
167         }
168 }
169
170 void
171 CPL::add_reel (shared_ptr<const Reel> reel)
172 {
173         _reels.push_back (reel);
174 }
175
176 void
177 CPL::write_xml (XMLMetadata const & metadata) const
178 {
179         boost::filesystem::path p;
180         p /= _directory;
181         stringstream s;
182         s << _uuid << "_cpl.xml";
183         p /= s.str();
184
185         xmlpp::Document doc;
186         xmlpp::Element* root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
187         root->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
188         root->add_child("AnnotationText")->add_child_text (_name);
189         root->add_child("IssueDate")->add_child_text (metadata.issue_date);
190         root->add_child("Creator")->add_child_text (metadata.creator);
191         root->add_child("ContentTitleText")->add_child_text (_name);
192         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
193         {
194                 xmlpp::Node* cv = root->add_child ("ContentVersion");
195                 cv->add_child ("Id")->add_child_text ("urn:uri:" + _uuid + "_" + metadata.issue_date);
196                 cv->add_child ("LabelText")->add_child_text (_uuid + "_" + metadata.issue_date);
197         }
198         root->add_child("RatingList");
199
200         xmlpp::Node* reel_list = root->add_child ("ReelList");
201         
202         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
203                 (*i)->write_to_cpl (reel_list);
204         }
205
206         doc.write_to_file_formatted (p.string (), "UTF-8");
207
208         _digest = make_digest (p.string ());
209         _length = boost::filesystem::file_size (p.string ());
210 }
211
212 void
213 CPL::write_to_pkl (xmlpp::Node* node) const
214 {
215         xmlpp::Node* asset = node->add_child ("Asset");
216         asset->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
217         asset->add_child("Hash")->add_child_text (_digest);
218         asset->add_child("Size")->add_child_text (lexical_cast<string> (_length));
219         asset->add_child("Type")->add_child_text ("text/xml");
220 }
221
222 list<shared_ptr<const Asset> >
223 CPL::assets () const
224 {
225         list<shared_ptr<const Asset> > a;
226         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
227                 if ((*i)->main_picture ()) {
228                         a.push_back ((*i)->main_picture ());
229                 }
230                 if ((*i)->main_sound ()) {
231                         a.push_back ((*i)->main_sound ());
232                 }
233                 if ((*i)->main_subtitle ()) {
234                         a.push_back ((*i)->main_subtitle ());
235                 }
236         }
237
238         return a;
239 }
240
241 void
242 CPL::write_to_assetmap (xmlpp::Node* node) const
243 {
244         xmlpp::Node* asset = node->add_child ("Asset");
245         asset->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
246         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
247         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
248         chunk->add_child("Path")->add_child_text (_uuid + "_cpl.xml");
249         chunk->add_child("VolumeIndex")->add_child_text ("1");
250         chunk->add_child("Offset")->add_child_text("0");
251         chunk->add_child("Length")->add_child_text(lexical_cast<string> (_length));
252 }
253         
254         
255         
256 bool
257 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
258 {
259         if (_name != other._name && !opt.cpl_names_can_differ) {
260                 stringstream s;
261                 s << "names differ: " << _name << " vs " << other._name << "\n";
262                 note (ERROR, s.str ());
263                 return false;
264         }
265
266         if (_content_kind != other._content_kind) {
267                 note (ERROR, "content kinds differ");
268                 return false;
269         }
270
271         if (_fps != other._fps) {
272                 note (ERROR, "frames per second differ");
273                 return false;
274         }
275
276         if (_length != other._length) {
277                 note (ERROR, "lengths differ");
278                 return false;
279         }
280
281         if (_reels.size() != other._reels.size()) {
282                 note (ERROR, "reel counts differ");
283                 return false;
284         }
285         
286         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
287         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
288         
289         while (a != _reels.end ()) {
290                 if (!(*a)->equals (*b, opt, note)) {
291                         return false;
292                 }
293                 ++a;
294                 ++b;
295         }
296
297         return true;
298 }