No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / src / lib / dcp_examiner.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "dcp_examiner.h"
22 #include "dcp_content.h"
23 #include "exceptions.h"
24 #include "image.h"
25 #include "config.h"
26 #include <dcp/dcp.h>
27 #include <dcp/decrypted_kdm.h>
28 #include <dcp/cpl.h>
29 #include <dcp/reel.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_frame.h>
34 #include <dcp/stereo_picture_asset.h>
35 #include <dcp/stereo_picture_frame.h>
36 #include <dcp/sound_asset.h>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 using std::list;
42 using std::cout;
43 using std::runtime_error;
44 using boost::shared_ptr;
45 using boost::dynamic_pointer_cast;
46
47 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
48         : _video_length (0)
49         , _audio_length (0)
50         , _has_subtitles (false)
51         , _encrypted (false)
52         , _kdm_valid (false)
53 {
54         dcp::DCP dcp (content->directory ());
55         dcp.read (false, 0, true);
56
57         if (content->kdm ()) {
58                 dcp.add (dcp::DecryptedKDM (content->kdm().get(), Config::instance()->decryption_chain()->key().get ()));
59         }
60
61         if (dcp.cpls().size() == 0) {
62                 throw DCPError ("No CPLs found in DCP");
63         } else if (dcp.cpls().size() > 1) {
64                 throw DCPError ("Multiple CPLs found in DCP");
65         }
66
67         _name = dcp.cpls().front()->content_title_text ();
68
69         list<shared_ptr<dcp::Reel> > reels = dcp.cpls().front()->reels ();
70         for (list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin(); i != reels.end(); ++i) {
71
72                 if ((*i)->main_picture ()) {
73                         dcp::Fraction const frac = (*i)->main_picture()->frame_rate ();
74                         float const fr = float(frac.numerator) / frac.denominator;
75                         if (!_video_frame_rate) {
76                                 _video_frame_rate = fr;
77                         } else if (_video_frame_rate.get() != fr) {
78                                 throw DCPError (_("Mismatched frame rates in DCP"));
79                         }
80
81                         shared_ptr<dcp::PictureAsset> asset = (*i)->main_picture()->asset ();
82                         if (!_video_size) {
83                                 _video_size = asset->size ();
84                         } else if (_video_size.get() != asset->size ()) {
85                                 throw DCPError (_("Mismatched video sizes in DCP"));
86                         }
87
88                         _video_length += (*i)->main_picture()->duration();
89                 }
90
91                 if ((*i)->main_sound ()) {
92                         shared_ptr<dcp::SoundAsset> asset = (*i)->main_sound()->asset ();
93
94                         if (!_audio_channels) {
95                                 _audio_channels = asset->channels ();
96                         } else if (_audio_channels.get() != asset->channels ()) {
97                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
98                         }
99
100                         if (!_audio_frame_rate) {
101                                 _audio_frame_rate = asset->sampling_rate ();
102                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
103                                 throw DCPError (_("Mismatched audio sample rates in DCP"));
104                         }
105
106                         _audio_length += (*i)->main_sound()->duration();
107                 }
108
109                 if ((*i)->main_subtitle ()) {
110                         _has_subtitles = true;
111                 }
112         }
113
114         _encrypted = dcp.encrypted ();
115         _kdm_valid = true;
116
117         /* Check that we can read the first picture frame */
118         try {
119                 if (!dcp.cpls().empty () && !dcp.cpls().front()->reels().empty ()) {
120                         shared_ptr<dcp::PictureAsset> asset = dcp.cpls().front()->reels().front()->main_picture()->asset ();
121                         shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
122                         shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
123
124                         if (mono) {
125                                 mono->get_frame(0)->xyz_image ();
126                         } else {
127                                 stereo->get_frame(0)->xyz_image (dcp::EYE_LEFT);
128                         }
129
130                 }
131         } catch (dcp::DCPReadError& e) {
132                 _kdm_valid = false;
133                 if (_encrypted && content->kdm ()) {
134                         /* XXX: maybe don't use an exception for this */
135                         throw runtime_error (_("The KDM does not decrypt the DCP.  Perhaps it is targeted at the wrong CPL."));
136                 }
137         }
138
139         _standard = dcp.standard ();
140 }