Initial hacks.
[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 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
311         : _directory (directory)
312         , _content_kind (FEATURE)
313         , _length (0)
314         , _fps (0)
315 {
316         /* Read the XML */
317         shared_ptr<CPLFile> cpl;
318         try {
319                 cpl.reset (new CPLFile (file));
320         } catch (FileError& e) {
321                 throw FileError ("could not load CPL file", file);
322         }
323         
324         /* Now cherry-pick the required bits into our own data structure */
325         
326         _name = cpl->annotation_text;
327         _content_kind = cpl->content_kind;
328
329         for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
330
331                 shared_ptr<Picture> p;
332
333                 if ((*i)->asset_list->main_picture) {
334                         p = (*i)->asset_list->main_picture;
335                 } else {
336                         p = (*i)->asset_list->main_stereoscopic_picture;
337                 }
338                 
339                 _fps = p->edit_rate.numerator;
340                 _length += p->duration;
341
342                 shared_ptr<PictureAsset> picture;
343                 shared_ptr<SoundAsset> sound;
344                 shared_ptr<SubtitleAsset> subtitle;
345
346                 /* Some rather twisted logic to decide if we are 3D or not;
347                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
348                    just have a FrameRate twice the EditRate and apparently
349                    expect you to divine the fact that they are hence 3D.
350                 */
351
352                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
353
354                         try {
355                                 picture.reset (new MonoPictureAsset (
356                                                        _directory,
357                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
358                                                        _fps,
359                                                        (*i)->asset_list->main_picture->entry_point,
360                                                        (*i)->asset_list->main_picture->duration
361                                                        )
362                                         );
363                         } catch (MXFFileError) {
364                                 if (require_mxfs) {
365                                         throw;
366                                 }
367                         }
368                         
369                 } else {
370
371                         try {
372                                 picture.reset (new StereoPictureAsset (
373                                                        _directory,
374                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
375                                                        _fps,
376                                                        p->entry_point,
377                                                        p->duration
378                                                        )
379                                         );
380                         } catch (MXFFileError) {
381                                 if (require_mxfs) {
382                                         throw;
383                                 }
384                         }
385                         
386                 }
387                 
388                 if ((*i)->asset_list->main_sound) {
389                         
390                         try {
391                                 sound.reset (new SoundAsset (
392                                                      _directory,
393                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
394                                                      _fps,
395                                                      (*i)->asset_list->main_sound->entry_point,
396                                                      (*i)->asset_list->main_sound->duration
397                                                      )
398                                         );
399                         } catch (MXFFileError) {
400                                 if (require_mxfs) {
401                                         throw;
402                                 }
403                         }
404                 }
405
406                 if ((*i)->asset_list->main_subtitle) {
407                         
408                         subtitle.reset (new SubtitleAsset (
409                                                 _directory,
410                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
411                                                 )
412                                 );
413                 }
414                         
415                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
416         }
417 }
418
419 void
420 CPL::add_reel (shared_ptr<const Reel> reel)
421 {
422         _reels.push_back (reel);
423 }
424
425 void
426 CPL::write_xml () const
427 {
428         boost::filesystem::path p;
429         p /= _directory;
430         stringstream s;
431         s << _uuid << "_cpl.xml";
432         p /= s.str();
433         ofstream os (p.string().c_str());
434         
435         os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
436            << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
437            << "  <Id>urn:uuid:" << _uuid << "</Id>\n"
438            << "  <AnnotationText>" << _name << "</AnnotationText>\n"
439            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
440            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
441            << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
442            << "  <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
443            << "  <ContentVersion>\n"
444            << "    <Id>urn:uri:" << _uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
445            << "    <LabelText>" << _uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
446            << "  </ContentVersion>\n"
447            << "  <RatingList/>\n"
448            << "  <ReelList>\n";
449         
450         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
451                 (*i)->write_to_cpl (os);
452         }
453
454         os << "      </AssetList>\n"
455            << "    </Reel>\n"
456            << "  </ReelList>\n"
457            << "</CompositionPlaylist>\n";
458
459         os.close ();
460
461         _digest = make_digest (p.string (), 0);
462         _length = boost::filesystem::file_size (p.string ());
463 }
464
465 void
466 CPL::write_to_pkl (ostream& s) const
467 {
468         s << "    <Asset>\n"
469           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
470           << "      <Hash>" << _digest << "</Hash>\n"
471           << "      <Size>" << _length << "</Size>\n"
472           << "      <Type>text/xml</Type>\n"
473           << "    </Asset>\n";
474 }
475
476 list<shared_ptr<const Asset> >
477 CPL::assets () const
478 {
479         list<shared_ptr<const Asset> > a;
480         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
481                 if ((*i)->main_picture ()) {
482                         a.push_back ((*i)->main_picture ());
483                 }
484                 if ((*i)->main_sound ()) {
485                         a.push_back ((*i)->main_sound ());
486                 }
487                 if ((*i)->main_subtitle ()) {
488                         a.push_back ((*i)->main_subtitle ());
489                 }
490         }
491
492         return a;
493 }
494
495 void
496 CPL::write_to_assetmap (ostream& s) const
497 {
498         s << "    <Asset>\n"
499           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
500           << "      <ChunkList>\n"
501           << "        <Chunk>\n"
502           << "          <Path>" << _uuid << "_cpl.xml</Path>\n"
503           << "          <VolumeIndex>1</VolumeIndex>\n"
504           << "          <Offset>0</Offset>\n"
505           << "          <Length>" << _length << "</Length>\n"
506           << "        </Chunk>\n"
507           << "      </ChunkList>\n"
508           << "    </Asset>\n";
509 }
510         
511         
512         
513 bool
514 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
515 {
516         if (_name != other._name) {
517                 notes.push_back ("names differ");
518                 return false;
519         }
520
521         if (_content_kind != other._content_kind) {
522                 notes.push_back ("content kinds differ");
523                 return false;
524         }
525
526         if (_fps != other._fps) {
527                 notes.push_back ("frames per second differ");
528                 return false;
529         }
530
531         if (_length != other._length) {
532                 notes.push_back ("lengths differ");
533                 return false;
534         }
535
536         if (_reels.size() != other._reels.size()) {
537                 notes.push_back ("reel counts differ");
538                 return false;
539         }
540         
541         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
542         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
543         
544         while (a != _reels.end ()) {
545                 if (!(*a)->equals (*b, opt, notes)) {
546                         return false;
547                 }
548                 ++a;
549                 ++b;
550         }
551
552         return true;
553 }