2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 * @brief A class to create a DCP.
29 #include <boost/filesystem.hpp>
30 #include <libxml++/libxml++.h>
33 #include "sound_asset.h"
34 #include "picture_asset.h"
35 #include "subtitle_asset.h"
38 #include "exceptions.h"
41 #include "asset_map.h"
46 using std::stringstream;
49 using boost::shared_ptr;
50 using namespace libdcp;
52 DCP::DCP (string directory)
53 : _directory (directory)
55 boost::filesystem::create_directories (directory);
59 DCP::write_xml () const
61 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
65 string pkl_uuid = make_uuid ();
66 string pkl_path = write_pkl (pkl_uuid);
69 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
73 DCP::write_pkl (string pkl_uuid) const
75 assert (!_cpls.empty ());
77 boost::filesystem::path p;
80 s << pkl_uuid << "_pkl.xml";
82 ofstream pkl (p.string().c_str());
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"
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);
99 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
100 (*i)->write_to_pkl (pkl);
103 pkl << " </AssetList>\n"
104 << "</PackingList>\n";
110 DCP::write_volindex () const
112 boost::filesystem::path p;
115 ofstream vi (p.string().c_str());
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";
124 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
126 boost::filesystem::path p;
129 ofstream am (p.string().c_str());
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"
141 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
142 << " <PackingList>true</PackingList>\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"
153 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
154 (*i)->write_to_assetmap (am);
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);
162 am << " </AssetList>\n"
168 DCP::read (bool require_mxfs)
172 shared_ptr<AssetMap> asset_map;
174 boost::filesystem::path p = _directory;
176 if (boost::filesystem::exists (p)) {
177 asset_map.reset (new AssetMap (p.string ()));
181 if (boost::filesystem::exists (p)) {
182 asset_map.reset (new AssetMap (p.string ()));
184 throw DCPReadError ("could not find AssetMap file");
188 } catch (FileError& e) {
189 throw FileError ("could not load AssetMap file", files.asset_map);
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");
197 boost::filesystem::path t = _directory;
198 t /= (*i)->chunks.front()->path;
200 if (ends_with (t.string(), ".mxf") || ends_with (t.string(), ".ttf")) {
204 xmlpp::DomParser* p = new xmlpp::DomParser;
206 p->parse_file (t.string());
207 } catch (std::exception& e) {
212 string const root = p->get_document()->get_root_node()->get_name ();
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();
221 throw DCPReadError ("duplicate PKLs found");
226 if (files.cpls.empty ()) {
227 throw FileError ("no CPL files found", "");
230 if (files.pkl.empty ()) {
231 throw FileError ("no PKL file found", "");
234 shared_ptr<PKLFile> pkl;
236 pkl.reset (new PKLFile (files.pkl));
237 } catch (FileError& e) {
238 throw FileError ("could not load PKL file", files.pkl);
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)));
250 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
252 if (_cpls.size() != other._cpls.size()) {
253 notes.push_back ("CPL counts differ");
257 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
258 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
260 while (a != _cpls.end ()) {
261 if (!(*a)->equals (*b->get(), opt, notes)) {
273 DCP::add_cpl (shared_ptr<CPL> cpl)
275 _cpls.push_back (cpl);
278 class AssetComparator
281 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
282 return a->uuid() < b->uuid();
286 list<shared_ptr<const Asset> >
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 ();
300 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
301 : _directory (directory)
303 , _content_kind (content_kind)
305 , _fps (frames_per_second)
307 _uuid = make_uuid ();
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.
316 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
317 : _directory (directory)
318 , _content_kind (FEATURE)
323 shared_ptr<CPLFile> cpl;
325 cpl.reset (new CPLFile (file));
326 } catch (FileError& e) {
327 throw FileError ("could not load CPL file", file);
330 /* Now cherry-pick the required bits into our own data structure */
332 _name = cpl->annotation_text;
333 _content_kind = cpl->content_kind;
335 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
337 shared_ptr<Picture> p;
339 if ((*i)->asset_list->main_picture) {
340 p = (*i)->asset_list->main_picture;
342 p = (*i)->asset_list->main_stereoscopic_picture;
345 _fps = p->edit_rate.numerator;
346 _length += p->duration;
348 shared_ptr<PictureAsset> picture;
349 shared_ptr<SoundAsset> sound;
350 shared_ptr<SubtitleAsset> subtitle;
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.
358 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
361 picture.reset (new MonoPictureAsset (
363 asset_map->asset_from_id (p->id)->chunks.front()->path,
365 (*i)->asset_list->main_picture->entry_point,
366 (*i)->asset_list->main_picture->duration
369 } catch (MXFFileError) {
378 picture.reset (new StereoPictureAsset (
380 asset_map->asset_from_id (p->id)->chunks.front()->path,
386 } catch (MXFFileError) {
394 if ((*i)->asset_list->main_sound) {
397 sound.reset (new SoundAsset (
399 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
401 (*i)->asset_list->main_sound->entry_point,
402 (*i)->asset_list->main_sound->duration
405 } catch (MXFFileError) {
412 if ((*i)->asset_list->main_subtitle) {
414 subtitle.reset (new SubtitleAsset (
416 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
421 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
426 CPL::add_reel (shared_ptr<const Reel> reel)
428 _reels.push_back (reel);
432 CPL::write_xml () const
434 boost::filesystem::path p;
437 s << _uuid << "_cpl.xml";
439 ofstream os (p.string().c_str());
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"
456 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
457 (*i)->write_to_cpl (os);
460 os << " </AssetList>\n"
463 << "</CompositionPlaylist>\n";
467 _digest = make_digest (p.string (), 0);
468 _length = boost::filesystem::file_size (p.string ());
472 CPL::write_to_pkl (ostream& s) const
475 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
476 << " <Hash>" << _digest << "</Hash>\n"
477 << " <Size>" << _length << "</Size>\n"
478 << " <Type>text/xml</Type>\n"
482 list<shared_ptr<const Asset> >
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 ());
490 if ((*i)->main_sound ()) {
491 a.push_back ((*i)->main_sound ());
493 if ((*i)->main_subtitle ()) {
494 a.push_back ((*i)->main_subtitle ());
502 CPL::write_to_assetmap (ostream& s) const
505 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
508 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
509 << " <VolumeIndex>1</VolumeIndex>\n"
510 << " <Offset>0</Offset>\n"
511 << " <Length>" << _length << "</Length>\n"
520 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
522 if (_name != other._name) {
523 notes.push_back ("names differ");
527 if (_content_kind != other._content_kind) {
528 notes.push_back ("content kinds differ");
532 if (_fps != other._fps) {
533 notes.push_back ("frames per second differ");
537 if (_length != other._length) {
538 notes.push_back ("lengths differ");
542 if (_reels.size() != other._reels.size()) {
543 notes.push_back ("reel counts differ");
547 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
548 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
550 while (a != _reels.end ()) {
551 if (!(*a)->equals (*b, opt, notes)) {