Clean up DCP comparison a bit.
[libdcp.git] / src / dcp.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 /** @file  src/dcp.cc
21  *  @brief A class to create a DCP.
22  */
23
24 #include <sstream>
25 #include <fstream>
26 #include <iomanip>
27 #include <cassert>
28 #include <iostream>
29 #include <boost/filesystem.hpp>
30 #include <libxml++/libxml++.h>
31 #include "dcp.h"
32 #include "asset.h"
33 #include "sound_asset.h"
34 #include "picture_asset.h"
35 #include "subtitle_asset.h"
36 #include "util.h"
37 #include "metadata.h"
38 #include "exceptions.h"
39 #include "cpl_file.h"
40 #include "pkl_file.h"
41 #include "asset_map.h"
42 #include "reel.h"
43
44 using namespace std;
45 using namespace boost;
46 using namespace libdcp;
47
48 DCP::DCP (string directory)
49         : _directory (directory)
50 {
51         filesystem::create_directories (directory);
52 }
53
54 void
55 DCP::write_xml () const
56 {
57         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
58                 (*i)->write_xml ();
59         }
60
61         string pkl_uuid = make_uuid ();
62         string pkl_path = write_pkl (pkl_uuid);
63         
64         write_volindex ();
65         write_assetmap (pkl_uuid, filesystem::file_size (pkl_path));
66 }
67
68 std::string
69 DCP::write_pkl (string pkl_uuid) const
70 {
71         assert (!_cpls.empty ());
72         
73         filesystem::path p;
74         p /= _directory;
75         stringstream s;
76         s << pkl_uuid << "_pkl.xml";
77         p /= s.str();
78         ofstream pkl (p.string().c_str());
79
80         pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
81             << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
82             << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
83                 /* XXX: this is a bit of a hack */
84             << "  <AnnotationText>" << _cpls.front()->name() << "</AnnotationText>\n"
85             << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
86             << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
87             << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
88             << "  <AssetList>\n";
89
90         list<shared_ptr<const Asset> > a = assets ();
91         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
92                 (*i)->write_to_pkl (pkl);
93         }
94
95         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
96                 (*i)->write_to_pkl (pkl);
97         }
98
99         pkl << "  </AssetList>\n"
100             << "</PackingList>\n";
101
102         return p.string ();
103 }
104
105 void
106 DCP::write_volindex () const
107 {
108         filesystem::path p;
109         p /= _directory;
110         p /= "VOLINDEX.xml";
111         ofstream vi (p.string().c_str());
112
113         vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
114            << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
115            << "  <Index>1</Index>\n"
116            << "</VolumeIndex>\n";
117 }
118
119 void
120 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
121 {
122         filesystem::path p;
123         p /= _directory;
124         p /= "ASSETMAP.xml";
125         ofstream am (p.string().c_str());
126
127         am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
128            << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
129            << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
130            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
131            << "  <VolumeCount>1</VolumeCount>\n"
132            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
133            << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
134            << "  <AssetList>\n";
135
136         am << "    <Asset>\n"
137            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
138            << "      <PackingList>true</PackingList>\n"
139            << "      <ChunkList>\n"
140            << "        <Chunk>\n"
141            << "          <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
142            << "          <VolumeIndex>1</VolumeIndex>\n"
143            << "          <Offset>0</Offset>\n"
144            << "          <Length>" << pkl_length << "</Length>\n"
145            << "        </Chunk>\n"
146            << "      </ChunkList>\n"
147            << "    </Asset>\n";
148         
149         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
150                 (*i)->write_to_assetmap (am);
151         }
152
153         list<shared_ptr<const Asset> > a = assets ();
154         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
155                 (*i)->write_to_assetmap (am);
156         }
157
158         am << "  </AssetList>\n"
159            << "</AssetMap>\n";
160 }
161
162
163 void
164 DCP::read (bool require_mxfs)
165 {
166         Files files;
167
168         shared_ptr<AssetMap> asset_map;
169         try {
170                 filesystem::path p = _directory;
171                 p /= "ASSETMAP";
172                 if (filesystem::exists (p)) {
173                         asset_map.reset (new AssetMap (p.string ()));
174                 } else {
175                         p = _directory;
176                         p /= "ASSETMAP.xml";
177                         if (filesystem::exists (p)) {
178                                 asset_map.reset (new AssetMap (p.string ()));
179                         } else {
180                                 throw DCPReadError ("could not find AssetMap file");
181                         }
182                 }
183                 
184         } catch (FileError& e) {
185                 throw FileError ("could not load AssetMap file", files.asset_map);
186         }
187
188         for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
189                 if ((*i)->chunks.size() != 1) {
190                         throw XMLError ("unsupported asset chunk count");
191                 }
192
193                 filesystem::path t = _directory;
194                 t /= (*i)->chunks.front()->path;
195                 
196                 if (ends_with (t.string(), ".mxf") || ends_with (t.string(), ".ttf")) {
197                         continue;
198                 }
199
200                 xmlpp::DomParser* p = new xmlpp::DomParser;
201                 try {
202                         p->parse_file (t.string());
203                 } catch (std::exception& e) {
204                         delete p;
205                         continue;
206                 }
207
208                 string const root = p->get_document()->get_root_node()->get_name ();
209                 delete p;
210
211                 if (root == "CompositionPlaylist") {
212                         files.cpls.push_back (t.string());
213                 } else if (root == "PackingList") {
214                         if (files.pkl.empty ()) {
215                                 files.pkl = t.string();
216                         } else {
217                                 throw DCPReadError ("duplicate PKLs found");
218                         }
219                 } else if (root == "DCSubtitle") {
220                         files.subtitles.push_back (t.string());
221                 }
222         }
223         
224         if (files.cpls.empty ()) {
225                 throw FileError ("no CPL files found", "");
226         }
227
228         if (files.pkl.empty ()) {
229                 throw FileError ("no PKL file found", "");
230         }
231
232         shared_ptr<PKLFile> pkl;
233         try {
234                 pkl.reset (new PKLFile (files.pkl));
235         } catch (FileError& e) {
236                 throw FileError ("could not load PKL file", files.pkl);
237         }
238
239         /* Cross-check */
240         /* XXX */
241
242         for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
243                 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
244         }
245
246 }
247
248 bool
249 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
250 {
251         if (_cpls.size() != other._cpls.size()) {
252                 notes.push_back ("CPL counts differ");
253                 return false;
254         }
255
256         list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
257         list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
258
259         while (a != _cpls.end ()) {
260                 if (!(*a)->equals (*b->get(), opt, notes)) {
261                         return false;
262                 }
263                 ++a;
264                 ++b;
265         }
266
267         return true;
268 }
269
270
271 void
272 DCP::add_cpl (shared_ptr<CPL> cpl)
273 {
274         _cpls.push_back (cpl);
275 }
276
277 class AssetComparator
278 {
279 public:
280         bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
281                 return a->uuid() < b->uuid();
282         }
283 };
284
285 list<shared_ptr<const Asset> >
286 DCP::assets () const
287 {
288         list<shared_ptr<const Asset> > a;
289         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
290                 list<shared_ptr<const Asset> > t = (*i)->assets ();
291                 a.merge (t);
292         }
293
294         a.sort ();
295         a.unique ();
296         return a;
297 }
298
299 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
300         : _directory (directory)
301         , _name (name)
302         , _content_kind (content_kind)
303         , _length (length)
304         , _fps (frames_per_second)
305 {
306         _uuid = make_uuid ();
307 }
308
309 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
310         : _directory (directory)
311         , _content_kind (FEATURE)
312         , _length (0)
313         , _fps (0)
314 {
315         /* Read the XML */
316         shared_ptr<CPLFile> cpl;
317         try {
318                 cpl.reset (new CPLFile (file));
319         } catch (FileError& e) {
320                 throw FileError ("could not load CPL file", file);
321         }
322         
323         /* Now cherry-pick the required bits into our own data structure */
324         
325         _name = cpl->annotation_text;
326         _content_kind = cpl->content_kind;
327
328         for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
329
330                 shared_ptr<Picture> p;
331
332                 if ((*i)->asset_list->main_picture) {
333                         p = (*i)->asset_list->main_picture;
334                 } else {
335                         p = (*i)->asset_list->main_stereoscopic_picture;
336                 }
337                 
338                 _fps = p->edit_rate.numerator;
339                 _length += p->duration;
340
341                 shared_ptr<PictureAsset> picture;
342                 shared_ptr<SoundAsset> sound;
343                 shared_ptr<SubtitleAsset> subtitle;
344
345                 /* Some rather twisted logic to decide if we are 3D or not;
346                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
347                    just have a FrameRate twice the EditRate and apparently
348                    expect you to divine the fact that they are hence 3D.
349                 */
350
351                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
352
353                         try {
354                                 picture.reset (new MonoPictureAsset (
355                                                        _directory,
356                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
357                                                        _fps,
358                                                        (*i)->asset_list->main_picture->entry_point,
359                                                        (*i)->asset_list->main_picture->duration
360                                                        )
361                                         );
362                         } catch (MXFFileError) {
363                                 if (require_mxfs) {
364                                         throw;
365                                 }
366                         }
367                         
368                 } else {
369
370                         try {
371                                 picture.reset (new StereoPictureAsset (
372                                                        _directory,
373                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
374                                                        _fps,
375                                                        p->entry_point,
376                                                        p->duration
377                                                        )
378                                         );
379                         } catch (MXFFileError) {
380                                 if (require_mxfs) {
381                                         throw;
382                                 }
383                         }
384                         
385                 }
386                 
387                 if ((*i)->asset_list->main_sound) {
388                         
389                         try {
390                                 sound.reset (new SoundAsset (
391                                                      _directory,
392                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
393                                                      _fps,
394                                                      (*i)->asset_list->main_sound->entry_point,
395                                                      (*i)->asset_list->main_sound->duration
396                                                      )
397                                         );
398                         } catch (MXFFileError) {
399                                 if (require_mxfs) {
400                                         throw;
401                                 }
402                         }
403                 }
404
405                 if ((*i)->asset_list->main_subtitle) {
406                         
407                         subtitle.reset (new SubtitleAsset (
408                                                 _directory,
409                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
410                                                 )
411                                 );
412                 }
413                         
414                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
415         }
416 }
417
418 void
419 CPL::add_reel (shared_ptr<const Reel> reel)
420 {
421         _reels.push_back (reel);
422 }
423
424 void
425 CPL::write_xml () const
426 {
427         filesystem::path p;
428         p /= _directory;
429         stringstream s;
430         s << _uuid << "_cpl.xml";
431         p /= s.str();
432         ofstream os (p.string().c_str());
433         
434         os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
435            << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
436            << "  <Id>urn:uuid:" << _uuid << "</Id>\n"
437            << "  <AnnotationText>" << _name << "</AnnotationText>\n"
438            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
439            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
440            << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
441            << "  <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
442            << "  <ContentVersion>\n"
443            << "    <Id>urn:uri:" << _uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
444            << "    <LabelText>" << _uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
445            << "  </ContentVersion>\n"
446            << "  <RatingList/>\n"
447            << "  <ReelList>\n";
448         
449         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
450                 (*i)->write_to_cpl (os);
451         }
452
453         os << "      </AssetList>\n"
454            << "    </Reel>\n"
455            << "  </ReelList>\n"
456            << "</CompositionPlaylist>\n";
457
458         os.close ();
459
460         _digest = make_digest (p.string (), 0);
461         _length = filesystem::file_size (p.string ());
462 }
463
464 void
465 CPL::write_to_pkl (ostream& s) const
466 {
467         s << "    <Asset>\n"
468           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
469           << "      <Hash>" << _digest << "</Hash>\n"
470           << "      <Size>" << _length << "</Size>\n"
471           << "      <Type>text/xml</Type>\n"
472           << "    </Asset>\n";
473 }
474
475 list<shared_ptr<const Asset> >
476 CPL::assets () const
477 {
478         list<shared_ptr<const Asset> > a;
479         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
480                 if ((*i)->main_picture ()) {
481                         a.push_back ((*i)->main_picture ());
482                 }
483                 if ((*i)->main_sound ()) {
484                         a.push_back ((*i)->main_sound ());
485                 }
486                 if ((*i)->main_subtitle ()) {
487                         a.push_back ((*i)->main_subtitle ());
488                 }
489         }
490
491         return a;
492 }
493
494 void
495 CPL::write_to_assetmap (ostream& s) const
496 {
497         s << "    <Asset>\n"
498           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
499           << "      <ChunkList>\n"
500           << "        <Chunk>\n"
501           << "          <Path>" << _uuid << "_cpl.xml</Path>\n"
502           << "          <VolumeIndex>1</VolumeIndex>\n"
503           << "          <Offset>0</Offset>\n"
504           << "          <Length>" << _length << "</Length>\n"
505           << "        </Chunk>\n"
506           << "      </ChunkList>\n"
507           << "    </Asset>\n";
508 }
509         
510         
511         
512 bool
513 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
514 {
515         if (_name != other._name) {
516                 notes.push_back ("names differ");
517                 return false;
518         }
519
520         if (_content_kind != other._content_kind) {
521                 notes.push_back ("content kinds differ");
522                 return false;
523         }
524
525         if (_fps != other._fps) {
526                 notes.push_back ("frames per second differ");
527                 return false;
528         }
529
530         if (_length != other._length) {
531                 notes.push_back ("lengths differ");
532                 return false;
533         }
534
535         if (_reels.size() != other._reels.size()) {
536                 notes.push_back ("reel counts differ");
537                 return false;
538         }
539         
540         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
541         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
542         
543         while (a != _reels.end ()) {
544                 if (!(*a)->equals (*b, opt, notes)) {
545                         return false;
546                 }
547                 ++a;
548                 ++b;
549         }
550
551         return true;
552 }