Basic support for decryption of imported DCPs.
[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/dcp.h>
21 #include <dcp/cpl.h>
22 #include <dcp/reel.h>
23 #include <dcp/reel_picture_asset.h>
24 #include <dcp/reel_sound_asset.h>
25 #include <dcp/mono_picture_mxf.h>
26 #include <dcp/mono_picture_frame.h>
27 #include <dcp/stereo_picture_mxf.h>
28 #include <dcp/stereo_picture_frame.h>
29 #include <dcp/sound_mxf.h>
30 #include "dcp_examiner.h"
31 #include "dcp_content.h"
32 #include "exceptions.h"
33 #include "image.h"
34 #include "config.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_private_key ()));
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::PictureMXF> mxf = (*i)->main_picture()->mxf ();
78                         if (!_video_size) {
79                                 _video_size = mxf->size ();
80                         } else if (_video_size.get() != mxf->size ()) {
81                                 throw DCPError (_("Mismatched video sizes in DCP"));
82                         }
83
84                         _video_length += ContentTime::from_frames ((*i)->main_picture()->duration(), _video_frame_rate.get ());
85                 }
86                         
87                 if ((*i)->main_sound ()) {
88                         shared_ptr<dcp::SoundMXF> mxf = (*i)->main_sound()->mxf ();
89
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"));
94                         }
95
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"));
100                         }
101
102                         _audio_length += ContentTime::from_frames ((*i)->main_sound()->duration(), _video_frame_rate.get ());
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::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);
119                         
120                         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, _video_size.get(), false));
121                         
122                         if (mono) {
123                                 mono->get_frame(0)->rgb_frame (image->data()[0]);
124                         } else {
125                                 stereo->get_frame(0)->rgb_frame (dcp::EYE_LEFT, image->data()[0]);
126                         }
127                         
128                 }
129         } catch (dcp::DCPReadError& e) {
130                 _kdm_valid = false;
131                 if (_encrypted && content->kdm ()) {
132                         /* XXX: maybe don't use an exception for this */
133                         throw StringError (_("The KDM does not decrypt the DCP.  Perhaps it is targeted at the wrong CPL"));
134                 }
135         }
136 }