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