2 Copyright (C) 2014 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.
20 #include "dcp_examiner.h"
21 #include "dcp_content.h"
22 #include "exceptions.h"
28 #include <dcp/reel_picture_asset.h>
29 #include <dcp/reel_sound_asset.h>
30 #include <dcp/mono_picture_mxf.h>
31 #include <dcp/mono_picture_frame.h>
32 #include <dcp/stereo_picture_mxf.h>
33 #include <dcp/stereo_picture_frame.h>
34 #include <dcp/sound_mxf.h>
40 using boost::shared_ptr;
41 using boost::dynamic_pointer_cast;
43 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
46 , _has_subtitles (false)
50 dcp::DCP dcp (content->directory ());
53 if (content->kdm ()) {
54 dcp.add (dcp::DecryptedKDM (content->kdm().get(), Config::instance()->decryption_private_key ()));
57 if (dcp.cpls().size() == 0) {
58 throw DCPError ("No CPLs found in DCP");
59 } else if (dcp.cpls().size() > 1) {
60 throw DCPError ("Multiple CPLs found in DCP");
63 _name = dcp.cpls().front()->content_title_text ();
65 list<shared_ptr<dcp::Reel> > reels = dcp.cpls().front()->reels ();
66 for (list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin(); i != reels.end(); ++i) {
68 if ((*i)->main_picture ()) {
69 dcp::Fraction const frac = (*i)->main_picture()->frame_rate ();
70 float const fr = float(frac.numerator) / frac.denominator;
71 if (!_video_frame_rate) {
72 _video_frame_rate = fr;
73 } else if (_video_frame_rate.get() != fr) {
74 throw DCPError (_("Mismatched frame rates in DCP"));
77 shared_ptr<dcp::PictureMXF> mxf = (*i)->main_picture()->mxf ();
79 _video_size = mxf->size ();
80 } else if (_video_size.get() != mxf->size ()) {
81 throw DCPError (_("Mismatched video sizes in DCP"));
84 _video_length += ContentTime::from_frames ((*i)->main_picture()->duration(), _video_frame_rate.get ());
87 if ((*i)->main_sound ()) {
88 shared_ptr<dcp::SoundMXF> mxf = (*i)->main_sound()->mxf ();
90 if (!_audio_channels) {
91 _audio_channels = mxf->channels ();
92 } else if (_audio_channels.get() != mxf->channels ()) {
93 throw DCPError (_("Mismatched audio channel counts in DCP"));
96 if (!_audio_frame_rate) {
97 _audio_frame_rate = mxf->sampling_rate ();
98 } else if (_audio_frame_rate.get() != mxf->sampling_rate ()) {
99 throw DCPError (_("Mismatched audio frame rates in DCP"));
102 _audio_length += ContentTime::from_frames ((*i)->main_sound()->duration(), _video_frame_rate.get ());
105 if ((*i)->main_subtitle ()) {
106 _has_subtitles = true;
110 _encrypted = dcp.encrypted ();
113 /* Check that we can read the first picture frame */
115 if (!dcp.cpls().empty () && !dcp.cpls().front()->reels().empty ()) {
116 shared_ptr<dcp::PictureMXF> mxf = dcp.cpls().front()->reels().front()->main_picture()->mxf ();
117 shared_ptr<dcp::MonoPictureMXF> mono = dynamic_pointer_cast<dcp::MonoPictureMXF> (mxf);
118 shared_ptr<dcp::StereoPictureMXF> stereo = dynamic_pointer_cast<dcp::StereoPictureMXF> (mxf);
121 mono->get_frame(0)->xyz_image ();
123 stereo->get_frame(0)->xyz_image (dcp::EYE_LEFT);
127 } catch (dcp::DCPReadError& e) {
129 if (_encrypted && content->kdm ()) {
130 /* XXX: maybe don't use an exception for this */
131 throw StringError (_("The KDM does not decrypt the DCP. Perhaps it is targeted at the wrong CPL"));