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"
45 using namespace boost;
46 using namespace libdcp;
48 DCP::DCP (string directory)
49 : _directory (directory)
51 filesystem::create_directories (directory);
55 DCP::write_xml () const
57 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
61 string pkl_uuid = make_uuid ();
62 string pkl_path = write_pkl (pkl_uuid);
65 write_assetmap (pkl_uuid, filesystem::file_size (pkl_path));
69 DCP::write_pkl (string pkl_uuid) const
71 assert (!_cpls.empty ());
76 s << pkl_uuid << "_pkl.xml";
78 ofstream pkl (p.string().c_str());
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"
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);
95 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
96 (*i)->write_to_pkl (pkl);
99 pkl << " </AssetList>\n"
100 << "</PackingList>\n";
106 DCP::write_volindex () const
111 ofstream vi (p.string().c_str());
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";
120 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
125 ofstream am (p.string().c_str());
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"
137 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
138 << " <PackingList>true</PackingList>\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"
149 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
150 (*i)->write_to_assetmap (am);
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);
158 am << " </AssetList>\n"
164 DCP::read (bool require_mxfs)
168 shared_ptr<AssetMap> asset_map;
170 filesystem::path p = _directory;
172 if (filesystem::exists (p)) {
173 asset_map.reset (new AssetMap (p.string ()));
177 if (filesystem::exists (p)) {
178 asset_map.reset (new AssetMap (p.string ()));
180 throw DCPReadError ("could not find AssetMap file");
184 } catch (FileError& e) {
185 throw FileError ("could not load AssetMap file", files.asset_map);
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");
193 filesystem::path t = _directory;
194 t /= (*i)->chunks.front()->path;
196 if (ends_with (t.string(), ".mxf") || ends_with (t.string(), ".ttf")) {
200 xmlpp::DomParser* p = new xmlpp::DomParser;
202 p->parse_file (t.string());
203 } catch (std::exception& e) {
208 string const root = p->get_document()->get_root_node()->get_name ();
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();
217 throw DCPReadError ("duplicate PKLs found");
219 } else if (root == "DCSubtitle") {
220 files.subtitles.push_back (t.string());
224 if (files.cpls.empty ()) {
225 throw FileError ("no CPL files found", "");
228 if (files.pkl.empty ()) {
229 throw FileError ("no PKL file found", "");
232 shared_ptr<PKLFile> pkl;
234 pkl.reset (new PKLFile (files.pkl));
235 } catch (FileError& e) {
236 throw FileError ("could not load PKL file", files.pkl);
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)));
249 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
251 if (_cpls.size() != other._cpls.size()) {
252 notes.push_back ("CPL counts differ");
256 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
257 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
259 while (a != _cpls.end ()) {
260 if (!(*a)->equals (*b->get(), opt, notes)) {
272 DCP::add_cpl (shared_ptr<CPL> cpl)
274 _cpls.push_back (cpl);
277 class AssetComparator
280 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
281 return a->uuid() < b->uuid();
285 list<shared_ptr<const Asset> >
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 ();
299 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
300 : _directory (directory)
302 , _content_kind (content_kind)
304 , _fps (frames_per_second)
306 _uuid = make_uuid ();
309 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
310 : _directory (directory)
311 , _content_kind (FEATURE)
316 shared_ptr<CPLFile> cpl;
318 cpl.reset (new CPLFile (file));
319 } catch (FileError& e) {
320 throw FileError ("could not load CPL file", file);
323 /* Now cherry-pick the required bits into our own data structure */
325 _name = cpl->annotation_text;
326 _content_kind = cpl->content_kind;
328 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
330 shared_ptr<Picture> p;
332 if ((*i)->asset_list->main_picture) {
333 p = (*i)->asset_list->main_picture;
335 p = (*i)->asset_list->main_stereoscopic_picture;
338 _fps = p->edit_rate.numerator;
339 _length += p->duration;
341 shared_ptr<PictureAsset> picture;
342 shared_ptr<SoundAsset> sound;
343 shared_ptr<SubtitleAsset> subtitle;
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.
351 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
354 picture.reset (new MonoPictureAsset (
356 asset_map->asset_from_id (p->id)->chunks.front()->path,
358 (*i)->asset_list->main_picture->entry_point,
359 (*i)->asset_list->main_picture->duration
362 } catch (MXFFileError) {
371 picture.reset (new StereoPictureAsset (
373 asset_map->asset_from_id (p->id)->chunks.front()->path,
379 } catch (MXFFileError) {
387 if ((*i)->asset_list->main_sound) {
390 sound.reset (new SoundAsset (
392 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
394 (*i)->asset_list->main_sound->entry_point,
395 (*i)->asset_list->main_sound->duration
398 } catch (MXFFileError) {
405 if ((*i)->asset_list->main_subtitle) {
407 subtitle.reset (new SubtitleAsset (
409 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
414 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
419 CPL::add_reel (shared_ptr<const Reel> reel)
421 _reels.push_back (reel);
425 CPL::write_xml () const
430 s << _uuid << "_cpl.xml";
432 ofstream os (p.string().c_str());
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"
449 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
450 (*i)->write_to_cpl (os);
453 os << " </AssetList>\n"
456 << "</CompositionPlaylist>\n";
460 _digest = make_digest (p.string (), 0);
461 _length = filesystem::file_size (p.string ());
465 CPL::write_to_pkl (ostream& s) const
468 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
469 << " <Hash>" << _digest << "</Hash>\n"
470 << " <Size>" << _length << "</Size>\n"
471 << " <Type>text/xml</Type>\n"
475 list<shared_ptr<const Asset> >
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 ());
483 if ((*i)->main_sound ()) {
484 a.push_back ((*i)->main_sound ());
486 if ((*i)->main_subtitle ()) {
487 a.push_back ((*i)->main_subtitle ());
495 CPL::write_to_assetmap (ostream& s) const
498 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
501 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
502 << " <VolumeIndex>1</VolumeIndex>\n"
503 << " <Offset>0</Offset>\n"
504 << " <Length>" << _length << "</Length>\n"
513 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
515 if (_name != other._name) {
516 notes.push_back ("names differ");
520 if (_content_kind != other._content_kind) {
521 notes.push_back ("content kinds differ");
525 if (_fps != other._fps) {
526 notes.push_back ("frames per second differ");
530 if (_length != other._length) {
531 notes.push_back ("lengths differ");
535 if (_reels.size() != other._reels.size()) {
536 notes.push_back ("reel counts differ");
540 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
541 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
543 while (a != _reels.end ()) {
544 if (!(*a)->equals (*b, opt, notes)) {