Allow config of the full KDM decryption chain.
[dcpomatic.git] / src / lib / dcp_examiner.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "dcp_examiner.h"
21 #include "dcp_content.h"
22 #include "exceptions.h"
23 #include "image.h"
24 #include "config.h"
25 #include <dcp/dcp.h>
26 #include <dcp/cpl.h>
27 #include <dcp/reel.h>
28 #include <dcp/reel_picture_asset.h>
29 #include <dcp/reel_sound_asset.h>
30 #include <dcp/mono_picture_asset.h>
31 #include <dcp/mono_picture_frame.h>
32 #include <dcp/stereo_picture_asset.h>
33 #include <dcp/stereo_picture_frame.h>
34 #include <dcp/sound_asset.h>
35
36 #include "i18n.h"
37
38 using std::list;
39 using std::cout;
40 using boost::shared_ptr;
41 using boost::dynamic_pointer_cast;
42
43 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
44         : _video_length (0)
45         , _audio_length (0)
46         , _has_subtitles (false)
47         , _encrypted (false)
48         , _kdm_valid (false)
49 {
50         dcp::DCP dcp (content->directory ());
51         dcp.read ();
52
53         if (content->kdm ()) {
54                 dcp.add (dcp::DecryptedKDM (content->kdm().get(), Config::instance()->decryption_chain()->key().get ()));
55         }
56
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");
61         }
62
63         _name = dcp.cpls().front()->content_title_text ();
64
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) {
67
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"));
75                         }
76
77                         shared_ptr<dcp::PictureAsset> asset = (*i)->main_picture()->asset ();
78                         if (!_video_size) {
79                                 _video_size = asset->size ();
80                         } else if (_video_size.get() != asset->size ()) {
81                                 throw DCPError (_("Mismatched video sizes in DCP"));
82                         }
83
84                         _video_length += (*i)->main_picture()->duration();
85                 }
86
87                 if ((*i)->main_sound ()) {
88                         shared_ptr<dcp::SoundAsset> asset = (*i)->main_sound()->asset ();
89
90                         if (!_audio_channels) {
91                                 _audio_channels = asset->channels ();
92                         } else if (_audio_channels.get() != asset->channels ()) {
93                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
94                         }
95
96                         if (!_audio_frame_rate) {
97                                 _audio_frame_rate = asset->sampling_rate ();
98                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
99                                 throw DCPError (_("Mismatched audio frame rates in DCP"));
100                         }
101
102                         _audio_length += (*i)->main_sound()->duration();
103                 }
104
105                 if ((*i)->main_subtitle ()) {
106                         _has_subtitles = true;
107                 }
108         }
109
110         _encrypted = dcp.encrypted ();
111         _kdm_valid = true;
112
113         /* Check that we can read the first picture frame */
114         try {
115                 if (!dcp.cpls().empty () && !dcp.cpls().front()->reels().empty ()) {
116                         shared_ptr<dcp::PictureAsset> asset = dcp.cpls().front()->reels().front()->main_picture()->asset ();
117                         shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
118                         shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
119
120                         if (mono) {
121                                 mono->get_frame(0)->xyz_image ();
122                         } else {
123                                 stereo->get_frame(0)->xyz_image (dcp::EYE_LEFT);
124                         }
125
126                 }
127         } catch (dcp::DCPReadError& e) {
128                 _kdm_valid = false;
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."));
132                 }
133         }
134 }