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 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
311 : _directory (directory)
312 , _content_kind (FEATURE)
317 shared_ptr<CPLFile> cpl;
319 cpl.reset (new CPLFile (file));
320 } catch (FileError& e) {
321 throw FileError ("could not load CPL file", file);
324 /* Now cherry-pick the required bits into our own data structure */
326 _name = cpl->annotation_text;
327 _content_kind = cpl->content_kind;
329 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
331 shared_ptr<Picture> p;
333 if ((*i)->asset_list->main_picture) {
334 p = (*i)->asset_list->main_picture;
336 p = (*i)->asset_list->main_stereoscopic_picture;
339 _fps = p->edit_rate.numerator;
340 _length += p->duration;
342 shared_ptr<PictureAsset> picture;
343 shared_ptr<SoundAsset> sound;
344 shared_ptr<SubtitleAsset> subtitle;
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.
352 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
355 picture.reset (new MonoPictureAsset (
357 asset_map->asset_from_id (p->id)->chunks.front()->path,
359 (*i)->asset_list->main_picture->entry_point,
360 (*i)->asset_list->main_picture->duration
363 } catch (MXFFileError) {
372 picture.reset (new StereoPictureAsset (
374 asset_map->asset_from_id (p->id)->chunks.front()->path,
380 } catch (MXFFileError) {
388 if ((*i)->asset_list->main_sound) {
391 sound.reset (new SoundAsset (
393 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
395 (*i)->asset_list->main_sound->entry_point,
396 (*i)->asset_list->main_sound->duration
399 } catch (MXFFileError) {
406 if ((*i)->asset_list->main_subtitle) {
408 subtitle.reset (new SubtitleAsset (
410 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
415 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
420 CPL::add_reel (shared_ptr<const Reel> reel)
422 _reels.push_back (reel);
426 CPL::write_xml () const
428 boost::filesystem::path p;
431 s << _uuid << "_cpl.xml";
433 ofstream os (p.string().c_str());
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"
450 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
451 (*i)->write_to_cpl (os);
454 os << " </AssetList>\n"
457 << "</CompositionPlaylist>\n";
461 _digest = make_digest (p.string (), 0);
462 _length = boost::filesystem::file_size (p.string ());
466 CPL::write_to_pkl (ostream& s) const
469 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
470 << " <Hash>" << _digest << "</Hash>\n"
471 << " <Size>" << _length << "</Size>\n"
472 << " <Type>text/xml</Type>\n"
476 list<shared_ptr<const Asset> >
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 ());
484 if ((*i)->main_sound ()) {
485 a.push_back ((*i)->main_sound ());
487 if ((*i)->main_subtitle ()) {
488 a.push_back ((*i)->main_subtitle ());
496 CPL::write_to_assetmap (ostream& s) const
499 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
502 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
503 << " <VolumeIndex>1</VolumeIndex>\n"
504 << " <Offset>0</Offset>\n"
505 << " <Length>" << _length << "</Length>\n"
514 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
516 if (_name != other._name) {
517 notes.push_back ("names differ");
521 if (_content_kind != other._content_kind) {
522 notes.push_back ("content kinds differ");
526 if (_fps != other._fps) {
527 notes.push_back ("frames per second differ");
531 if (_length != other._length) {
532 notes.push_back ("lengths differ");
536 if (_reels.size() != other._reels.size()) {
537 notes.push_back ("reel counts differ");
541 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
542 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
544 while (a != _reels.end ()) {
545 if (!(*a)->equals (*b, opt, notes)) {