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) const
253 if (_cpls.size() != other._cpls.size()) {
254 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 list<string> n = (*a)->equals (*b->get(), opt);
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 if ((*i)->asset_list->main_picture) {
348 picture.reset (new MonoPictureAsset (
350 asset_map->asset_from_id ((*i)->asset_list->main_picture->id)->chunks.front()->path,
352 (*i)->asset_list->main_picture->entry_point,
353 (*i)->asset_list->main_picture->duration
356 } catch (MXFFileError) {
362 } else if ((*i)->asset_list->main_stereoscopic_picture) {
365 picture.reset (new StereoPictureAsset (
367 asset_map->asset_from_id ((*i)->asset_list->main_stereoscopic_picture->id)->chunks.front()->path,
369 (*i)->asset_list->main_stereoscopic_picture->entry_point,
370 (*i)->asset_list->main_stereoscopic_picture->duration
373 } catch (MXFFileError) {
381 if ((*i)->asset_list->main_sound) {
384 sound.reset (new SoundAsset (
386 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
388 (*i)->asset_list->main_sound->entry_point,
389 (*i)->asset_list->main_sound->duration
392 } catch (MXFFileError) {
399 if ((*i)->asset_list->main_subtitle) {
401 subtitle.reset (new SubtitleAsset (
403 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
408 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
413 CPL::add_reel (shared_ptr<const Reel> reel)
415 _reels.push_back (reel);
419 CPL::write_xml () const
424 s << _uuid << "_cpl.xml";
426 ofstream os (p.string().c_str());
428 os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
429 << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
430 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
431 << " <AnnotationText>" << _name << "</AnnotationText>\n"
432 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
433 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
434 << " <ContentTitleText>" << _name << "</ContentTitleText>\n"
435 << " <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
436 << " <ContentVersion>\n"
437 << " <Id>urn:uri:" << _uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
438 << " <LabelText>" << _uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
439 << " </ContentVersion>\n"
440 << " <RatingList/>\n"
443 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
444 (*i)->write_to_cpl (os);
447 os << " </AssetList>\n"
450 << "</CompositionPlaylist>\n";
454 _digest = make_digest (p.string (), 0);
455 _length = filesystem::file_size (p.string ());
459 CPL::write_to_pkl (ostream& s) const
462 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
463 << " <Hash>" << _digest << "</Hash>\n"
464 << " <Size>" << _length << "</Size>\n"
465 << " <Type>text/xml</Type>\n"
469 list<shared_ptr<const Asset> >
472 list<shared_ptr<const Asset> > a;
473 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
474 if ((*i)->main_picture ()) {
475 a.push_back ((*i)->main_picture ());
477 if ((*i)->main_sound ()) {
478 a.push_back ((*i)->main_sound ());
480 if ((*i)->main_subtitle ()) {
481 a.push_back ((*i)->main_subtitle ());
489 CPL::write_to_assetmap (ostream& s) const
492 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
495 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
496 << " <VolumeIndex>1</VolumeIndex>\n"
497 << " <Offset>0</Offset>\n"
498 << " <Length>" << _length << "</Length>\n"
507 CPL::equals (CPL const & other, EqualityOptions opt) const
511 if (opt.flags & LIBDCP_METADATA) {
512 if (_name != other._name) {
513 notes.push_back ("names differ");
515 if (_content_kind != other._content_kind) {
516 notes.push_back ("content kinds differ");
518 if (_fps != other._fps) {
519 notes.push_back ("frames per second differ");
521 if (_length != other._length) {
522 notes.push_back ("lengths differ");
526 if (_reels.size() != other._reels.size()) {
527 notes.push_back ("reel counts differ");
530 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
531 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
533 while (a != _reels.end ()) {
534 list<string> n = (*a)->equals (*b, opt);