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 <boost/algorithm/string.hpp>
31 #include <libxml++/libxml++.h>
34 #include "sound_asset.h"
35 #include "picture_asset.h"
36 #include "subtitle_asset.h"
39 #include "exceptions.h"
42 #include "asset_map.h"
47 using std::stringstream;
50 using boost::shared_ptr;
51 using namespace libdcp;
53 DCP::DCP (string directory)
54 : _directory (directory)
56 boost::filesystem::create_directories (directory);
60 DCP::write_xml () const
62 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
66 string pkl_uuid = make_uuid ();
67 string pkl_path = write_pkl (pkl_uuid);
70 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
74 DCP::write_pkl (string pkl_uuid) const
76 assert (!_cpls.empty ());
78 boost::filesystem::path p;
81 s << pkl_uuid << "_pkl.xml";
83 ofstream pkl (p.string().c_str());
85 pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
86 << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
87 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
88 /* XXX: this is a bit of a hack */
89 << " <AnnotationText>" << _cpls.front()->name() << "</AnnotationText>\n"
90 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
91 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
92 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
95 list<shared_ptr<const Asset> > a = assets ();
96 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
97 (*i)->write_to_pkl (pkl);
100 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
101 (*i)->write_to_pkl (pkl);
104 pkl << " </AssetList>\n"
105 << "</PackingList>\n";
111 DCP::write_volindex () const
113 boost::filesystem::path p;
116 ofstream vi (p.string().c_str());
118 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
119 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
120 << " <Index>1</Index>\n"
121 << "</VolumeIndex>\n";
125 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
127 boost::filesystem::path p;
130 ofstream am (p.string().c_str());
132 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
133 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
134 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
135 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
136 << " <VolumeCount>1</VolumeCount>\n"
137 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
138 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
142 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
143 << " <PackingList>true</PackingList>\n"
146 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
147 << " <VolumeIndex>1</VolumeIndex>\n"
148 << " <Offset>0</Offset>\n"
149 << " <Length>" << pkl_length << "</Length>\n"
154 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
155 (*i)->write_to_assetmap (am);
158 list<shared_ptr<const Asset> > a = assets ();
159 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
160 (*i)->write_to_assetmap (am);
163 am << " </AssetList>\n"
169 DCP::read (bool require_mxfs)
173 shared_ptr<AssetMap> asset_map;
175 boost::filesystem::path p = _directory;
177 if (boost::filesystem::exists (p)) {
178 asset_map.reset (new AssetMap (p.string ()));
182 if (boost::filesystem::exists (p)) {
183 asset_map.reset (new AssetMap (p.string ()));
185 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
189 } catch (FileError& e) {
190 boost::throw_exception (FileError ("could not load AssetMap file", files.asset_map));
193 for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
194 if ((*i)->chunks.size() != 1) {
195 boost::throw_exception (XMLError ("unsupported asset chunk count"));
198 boost::filesystem::path t = _directory;
199 t /= (*i)->chunks.front()->path;
201 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
205 xmlpp::DomParser* p = new xmlpp::DomParser;
207 p->parse_file (t.string());
208 } catch (std::exception& e) {
213 string const root = p->get_document()->get_root_node()->get_name ();
216 if (root == "CompositionPlaylist") {
217 files.cpls.push_back (t.string());
218 } else if (root == "PackingList") {
219 if (files.pkl.empty ()) {
220 files.pkl = t.string();
222 boost::throw_exception (DCPReadError ("duplicate PKLs found"));
227 if (files.cpls.empty ()) {
228 boost::throw_exception (FileError ("no CPL files found", ""));
231 if (files.pkl.empty ()) {
232 boost::throw_exception (FileError ("no PKL file found", ""));
235 shared_ptr<PKLFile> pkl;
237 pkl.reset (new PKLFile (files.pkl));
238 } catch (FileError& e) {
239 boost::throw_exception (FileError ("could not load PKL file", files.pkl));
245 for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
246 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
251 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
253 if (_cpls.size() != other._cpls.size()) {
254 note (ERROR, "CPL counts differ");
258 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
259 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
261 while (a != _cpls.end ()) {
262 if (!(*a)->equals (*b->get(), opt, note)) {
274 DCP::add_cpl (shared_ptr<CPL> cpl)
276 _cpls.push_back (cpl);
279 class AssetComparator
282 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
283 return a->uuid() < b->uuid();
287 list<shared_ptr<const Asset> >
290 list<shared_ptr<const Asset> > a;
291 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
292 list<shared_ptr<const Asset> > t = (*i)->assets ();
301 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
302 : _directory (directory)
304 , _content_kind (content_kind)
306 , _fps (frames_per_second)
308 _uuid = make_uuid ();
311 /** Construct a CPL object from a XML file.
312 * @param directory The directory containing this CPL's DCP.
313 * @param file The CPL XML filename.
314 * @param asset_map The corresponding asset map.
315 * @param require_mxfs true to throw an exception if a required MXF file does not exist.
317 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
318 : _directory (directory)
319 , _content_kind (FEATURE)
324 shared_ptr<CPLFile> cpl;
326 cpl.reset (new CPLFile (file));
327 } catch (FileError& e) {
328 boost::throw_exception (FileError ("could not load CPL file", file));
331 /* Now cherry-pick the required bits into our own data structure */
333 _name = cpl->annotation_text;
334 _content_kind = cpl->content_kind;
336 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
338 shared_ptr<Picture> p;
340 if ((*i)->asset_list->main_picture) {
341 p = (*i)->asset_list->main_picture;
343 p = (*i)->asset_list->main_stereoscopic_picture;
346 _fps = p->edit_rate.numerator;
347 _length += p->duration;
349 shared_ptr<PictureAsset> picture;
350 shared_ptr<SoundAsset> sound;
351 shared_ptr<SubtitleAsset> subtitle;
353 /* Some rather twisted logic to decide if we are 3D or not;
354 some DCPs give a MainStereoscopicPicture to indicate 3D, others
355 just have a FrameRate twice the EditRate and apparently
356 expect you to divine the fact that they are hence 3D.
359 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
362 picture.reset (new MonoPictureAsset (
364 asset_map->asset_from_id (p->id)->chunks.front()->path
368 picture->set_entry_point ((*i)->asset_list->main_picture->entry_point);
369 } catch (MXFFileError) {
378 picture.reset (new StereoPictureAsset (
380 asset_map->asset_from_id (p->id)->chunks.front()->path,
386 picture->set_entry_point (p->entry_point);
388 } catch (MXFFileError) {
396 if ((*i)->asset_list->main_sound) {
399 sound.reset (new SoundAsset (
401 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path
405 sound->set_entry_point ((*i)->asset_list->main_sound->entry_point);
406 } catch (MXFFileError) {
413 if ((*i)->asset_list->main_subtitle) {
415 subtitle.reset (new SubtitleAsset (
417 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
422 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
427 CPL::add_reel (shared_ptr<const Reel> reel)
429 _reels.push_back (reel);
433 CPL::write_xml () const
435 boost::filesystem::path p;
438 s << _uuid << "_cpl.xml";
440 ofstream os (p.string().c_str());
442 os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
443 << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
444 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
445 << " <AnnotationText>" << _name << "</AnnotationText>\n"
446 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
447 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
448 << " <ContentTitleText>" << _name << "</ContentTitleText>\n"
449 << " <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
450 << " <ContentVersion>\n"
451 << " <Id>urn:uri:" << _uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
452 << " <LabelText>" << _uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
453 << " </ContentVersion>\n"
454 << " <RatingList/>\n"
457 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
458 (*i)->write_to_cpl (os);
461 os << " </ReelList>\n"
462 << "</CompositionPlaylist>\n";
466 _digest = make_digest (p.string ());
467 _length = boost::filesystem::file_size (p.string ());
471 CPL::write_to_pkl (ostream& s) const
474 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
475 << " <Hash>" << _digest << "</Hash>\n"
476 << " <Size>" << _length << "</Size>\n"
477 << " <Type>text/xml</Type>\n"
481 list<shared_ptr<const Asset> >
484 list<shared_ptr<const Asset> > a;
485 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
486 if ((*i)->main_picture ()) {
487 a.push_back ((*i)->main_picture ());
489 if ((*i)->main_sound ()) {
490 a.push_back ((*i)->main_sound ());
492 if ((*i)->main_subtitle ()) {
493 a.push_back ((*i)->main_subtitle ());
501 CPL::write_to_assetmap (ostream& s) const
504 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
507 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
508 << " <VolumeIndex>1</VolumeIndex>\n"
509 << " <Offset>0</Offset>\n"
510 << " <Length>" << _length << "</Length>\n"
519 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
521 if (_name != other._name) {
522 note (ERROR, "names differ");
526 if (_content_kind != other._content_kind) {
527 note (ERROR, "content kinds differ");
531 if (_fps != other._fps) {
532 note (ERROR, "frames per second differ");
536 if (_length != other._length) {
537 note (ERROR, "lengths differ");
541 if (_reels.size() != other._reels.size()) {
542 note (ERROR, "reel counts differ");
546 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
547 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
549 while (a != _reels.end ()) {
550 if (!(*a)->equals (*b, opt, note)) {