2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "dcp_examiner.h"
22 #include "dcp_content.h"
23 #include "exceptions.h"
27 #include <dcp/decrypted_kdm.h>
30 #include <dcp/reel_picture_asset.h>
31 #include <dcp/reel_sound_asset.h>
32 #include <dcp/mono_picture_asset.h>
33 #include <dcp/mono_picture_asset_reader.h>
34 #include <dcp/mono_picture_frame.h>
35 #include <dcp/stereo_picture_asset.h>
36 #include <dcp/stereo_picture_asset_reader.h>
37 #include <dcp/stereo_picture_frame.h>
38 #include <dcp/sound_asset.h>
45 using std::runtime_error;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
49 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
52 , _has_subtitles (false)
57 dcp::DCP dcp (content->directory ());
58 dcp.read (false, 0, true);
60 if (content->kdm ()) {
61 dcp.add (dcp::DecryptedKDM (content->kdm().get(), Config::instance()->decryption_chain()->key().get ()));
64 if (dcp.cpls().size() == 0) {
65 throw DCPError ("No CPLs found in DCP");
66 } else if (dcp.cpls().size() > 1) {
67 throw DCPError ("Multiple CPLs found in DCP");
70 _name = dcp.cpls().front()->content_title_text ();
72 list<shared_ptr<dcp::Reel> > reels = dcp.cpls().front()->reels ();
73 for (list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin(); i != reels.end(); ++i) {
75 if ((*i)->main_picture ()) {
76 dcp::Fraction const frac = (*i)->main_picture()->edit_rate ();
77 float const fr = float(frac.numerator) / frac.denominator;
78 if (!_video_frame_rate) {
79 _video_frame_rate = fr;
80 } else if (_video_frame_rate.get() != fr) {
81 throw DCPError (_("Mismatched frame rates in DCP"));
84 shared_ptr<dcp::PictureAsset> asset = (*i)->main_picture()->asset ();
86 _video_size = asset->size ();
87 } else if (_video_size.get() != asset->size ()) {
88 throw DCPError (_("Mismatched video sizes in DCP"));
91 _video_length += (*i)->main_picture()->duration();
94 if ((*i)->main_sound ()) {
95 shared_ptr<dcp::SoundAsset> asset = (*i)->main_sound()->asset ();
97 if (!_audio_channels) {
98 _audio_channels = asset->channels ();
99 } else if (_audio_channels.get() != asset->channels ()) {
100 throw DCPError (_("Mismatched audio channel counts in DCP"));
103 if (!_audio_frame_rate) {
104 _audio_frame_rate = asset->sampling_rate ();
105 } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
106 throw DCPError (_("Mismatched audio sample rates in DCP"));
109 _audio_length += (*i)->main_sound()->duration();
112 if ((*i)->main_subtitle ()) {
113 _has_subtitles = true;
117 _encrypted = dcp.encrypted ();
120 /* Check that we can read the first picture frame */
122 if (!dcp.cpls().empty () && !dcp.cpls().front()->reels().empty ()) {
123 shared_ptr<dcp::PictureAsset> asset = dcp.cpls().front()->reels().front()->main_picture()->asset ();
124 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
125 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
128 mono->start_read()->get_frame(0)->xyz_image ();
130 stereo->start_read()->get_frame(0)->xyz_image (dcp::EYE_LEFT);
134 } catch (dcp::DCPReadError& e) {
136 if (_encrypted && content->kdm ()) {
137 /* XXX: maybe don't use an exception for this */
138 throw runtime_error (_("The KDM does not decrypt the DCP. Perhaps it is targeted at the wrong CPL."));
142 _standard = dcp.standard ();
143 _three_d = !reels.empty() && reels.front()->main_picture() &&
144 dynamic_pointer_cast<dcp::StereoPictureAsset> (reels.front()->main_picture()->asset());