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.
22 #include "parse/cpl.h"
24 #include "picture_asset.h"
25 #include "sound_asset.h"
26 #include "subtitle_asset.h"
27 #include "parse/asset_map.h"
32 using std::stringstream;
36 using boost::shared_ptr;
37 using boost::lexical_cast;
38 using namespace libdcp;
40 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
41 : _directory (directory)
43 , _content_kind (content_kind)
45 , _fps (frames_per_second)
50 /** Construct a CPL object from a XML file.
51 * @param directory The directory containing this CPL's DCP.
52 * @param file The CPL XML filename.
53 * @param asset_map The corresponding asset map.
54 * @param require_mxfs true to throw an exception if a required MXF file does not exist.
56 CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMap> asset_map, bool require_mxfs)
57 : _directory (directory)
58 , _content_kind (FEATURE)
63 shared_ptr<parse::CPL> cpl;
65 cpl.reset (new parse::CPL (file));
66 } catch (FileError& e) {
67 boost::throw_exception (FileError ("could not load CPL file", file));
70 /* Now cherry-pick the required bits into our own data structure */
72 _name = cpl->annotation_text;
73 _content_kind = cpl->content_kind;
75 for (list<shared_ptr<libdcp::parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
77 shared_ptr<parse::Picture> p;
79 if ((*i)->asset_list->main_picture) {
80 p = (*i)->asset_list->main_picture;
82 p = (*i)->asset_list->main_stereoscopic_picture;
85 _fps = p->edit_rate.numerator;
86 _length += p->duration;
88 shared_ptr<PictureAsset> picture;
89 shared_ptr<SoundAsset> sound;
90 shared_ptr<SubtitleAsset> subtitle;
92 /* Some rather twisted logic to decide if we are 3D or not;
93 some DCPs give a MainStereoscopicPicture to indicate 3D, others
94 just have a FrameRate twice the EditRate and apparently
95 expect you to divine the fact that they are hence 3D.
98 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
101 picture.reset (new MonoPictureAsset (
103 asset_map->asset_from_id (p->id)->chunks.front()->path
107 picture->set_entry_point (p->entry_point);
108 picture->set_duration (p->duration);
109 } catch (MXFFileError) {
117 picture.reset (new StereoPictureAsset (
119 asset_map->asset_from_id (p->id)->chunks.front()->path,
125 picture->set_entry_point (p->entry_point);
126 picture->set_duration (p->duration);
128 } catch (MXFFileError) {
136 if ((*i)->asset_list->main_sound) {
139 sound.reset (new SoundAsset (
141 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path
145 sound->set_entry_point ((*i)->asset_list->main_sound->entry_point);
146 sound->set_duration ((*i)->asset_list->main_sound->duration);
147 } catch (MXFFileError) {
154 if ((*i)->asset_list->main_subtitle) {
156 subtitle.reset (new SubtitleAsset (
158 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
162 subtitle->set_entry_point ((*i)->asset_list->main_subtitle->entry_point);
163 subtitle->set_duration ((*i)->asset_list->main_subtitle->duration);
166 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
171 CPL::add_reel (shared_ptr<const Reel> reel)
173 _reels.push_back (reel);
177 CPL::write_xml (XMLMetadata const & metadata) const
179 boost::filesystem::path p;
182 s << _uuid << "_cpl.xml";
186 xmlpp::Element* root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
187 root->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
188 root->add_child("AnnotationText")->add_child_text (_name);
189 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
190 root->add_child("Creator")->add_child_text (metadata.creator);
191 root->add_child("ContentTitleText")->add_child_text (_name);
192 root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
194 xmlpp::Node* cv = root->add_child ("ContentVersion");
195 cv->add_child ("Id")->add_child_text ("urn:uri:" + _uuid + "_" + metadata.issue_date);
196 cv->add_child ("LabelText")->add_child_text (_uuid + "_" + metadata.issue_date);
198 root->add_child("RatingList");
200 xmlpp::Node* reel_list = root->add_child ("ReelList");
202 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
203 (*i)->write_to_cpl (reel_list);
206 doc.write_to_file_formatted (p.string (), "UTF-8");
208 _digest = make_digest (p.string ());
209 _length = boost::filesystem::file_size (p.string ());
213 CPL::write_to_pkl (xmlpp::Node* node) const
215 xmlpp::Node* asset = node->add_child ("Asset");
216 asset->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
217 asset->add_child("Hash")->add_child_text (_digest);
218 asset->add_child("Size")->add_child_text (lexical_cast<string> (_length));
219 asset->add_child("Type")->add_child_text ("text/xml");
222 list<shared_ptr<const Asset> >
225 list<shared_ptr<const Asset> > a;
226 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
227 if ((*i)->main_picture ()) {
228 a.push_back ((*i)->main_picture ());
230 if ((*i)->main_sound ()) {
231 a.push_back ((*i)->main_sound ());
233 if ((*i)->main_subtitle ()) {
234 a.push_back ((*i)->main_subtitle ());
242 CPL::write_to_assetmap (xmlpp::Node* node) const
244 xmlpp::Node* asset = node->add_child ("Asset");
245 asset->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
246 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
247 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
248 chunk->add_child("Path")->add_child_text (_uuid + "_cpl.xml");
249 chunk->add_child("VolumeIndex")->add_child_text ("1");
250 chunk->add_child("Offset")->add_child_text("0");
251 chunk->add_child("Length")->add_child_text(lexical_cast<string> (_length));
257 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
259 if (_name != other._name && !opt.cpl_names_can_differ) {
261 s << "names differ: " << _name << " vs " << other._name << "\n";
262 note (ERROR, s.str ());
266 if (_content_kind != other._content_kind) {
267 note (ERROR, "content kinds differ");
271 if (_fps != other._fps) {
272 note (ERROR, "frames per second differ");
276 if (_length != other._length) {
277 note (ERROR, "lengths differ");
281 if (_reels.size() != other._reels.size()) {
282 note (ERROR, "reel counts differ");
286 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
287 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
289 while (a != _reels.end ()) {
290 if (!(*a)->equals (*b, opt, note)) {