Add option to set project properties from a DCP loaded as content (#1688).
[dcpomatic.git] / src / lib / dcp_examiner.cc
1 /*
2     Copyright (C) 2014-2020 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 "util.h"
27 #include <dcp/dcp.h>
28 #include <dcp/decrypted_kdm.h>
29 #include <dcp/cpl.h>
30 #include <dcp/reel.h>
31 #include <dcp/reel_picture_asset.h>
32 #include <dcp/reel_sound_asset.h>
33 #include <dcp/mono_picture_asset.h>
34 #include <dcp/mono_picture_asset_reader.h>
35 #include <dcp/mono_picture_frame.h>
36 #include <dcp/stereo_picture_asset.h>
37 #include <dcp/stereo_picture_asset_reader.h>
38 #include <dcp/stereo_picture_frame.h>
39 #include <dcp/sound_asset.h>
40 #include <dcp/sound_asset_reader.h>
41 #include <dcp/subtitle_asset.h>
42 #include <dcp/reel_subtitle_asset.h>
43 #include <dcp/reel_closed_caption_asset.h>
44 #include <dcp/reel_markers_asset.h>
45 #include <dcp/sound_asset.h>
46 #include <boost/foreach.hpp>
47 #include <iostream>
48
49 #include "i18n.h"
50
51 using std::list;
52 using std::cout;
53 using std::runtime_error;
54 using std::map;
55 using boost::shared_ptr;
56 using boost::dynamic_pointer_cast;
57
58 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content, bool tolerant)
59         : DCP (content, tolerant)
60         , _video_length (0)
61         , _audio_length (0)
62         , _has_video (false)
63         , _has_audio (false)
64         , _encrypted (false)
65         , _needs_assets (false)
66         , _kdm_valid (false)
67         , _three_d (false)
68 {
69         shared_ptr<dcp::CPL> cpl;
70
71         for (int i = 0; i < TEXT_COUNT; ++i) {
72                 _text_count[i] = 0;
73         }
74
75         if (content->cpl ()) {
76                 /* Use the CPL that the content was using before */
77                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
78                         if (i->id() == content->cpl().get()) {
79                                 cpl = i;
80                         }
81                 }
82         } else {
83                 /* Choose the CPL with the fewest unsatisfied references */
84
85                 int least_unsatisfied = INT_MAX;
86
87                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
88                         int unsatisfied = 0;
89                         BOOST_FOREACH (shared_ptr<dcp::Reel> j, i->reels()) {
90                                 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
91                                         ++unsatisfied;
92                                 }
93                                 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
94                                         ++unsatisfied;
95                                 }
96                                 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
97                                         ++unsatisfied;
98                                 }
99                         }
100
101                         if (unsatisfied < least_unsatisfied) {
102                                 least_unsatisfied = unsatisfied;
103                                 cpl = i;
104                         }
105                 }
106         }
107
108         if (!cpl) {
109                 throw DCPError ("No CPLs found in DCP");
110         }
111
112         _cpl = cpl->id ();
113         _name = cpl->content_title_text ();
114         _content_kind = cpl->content_kind ();
115
116         BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
117
118                 if (i->main_picture ()) {
119                         if (!i->main_picture()->asset_ref().resolved()) {
120                                 /* We are missing this asset so we can't continue; examination will be repeated later */
121                                 _needs_assets = true;
122                                 return;
123                         }
124
125                         dcp::Fraction const frac = i->main_picture()->edit_rate ();
126                         float const fr = float(frac.numerator) / frac.denominator;
127                         if (!_video_frame_rate) {
128                                 _video_frame_rate = fr;
129                         } else if (_video_frame_rate.get() != fr) {
130                                 throw DCPError (_("Mismatched frame rates in DCP"));
131                         }
132
133                         _has_video = true;
134                         shared_ptr<dcp::PictureAsset> asset = i->main_picture()->asset ();
135                         if (!_video_size) {
136                                 _video_size = asset->size ();
137                         } else if (_video_size.get() != asset->size ()) {
138                                 throw DCPError (_("Mismatched video sizes in DCP"));
139                         }
140
141                         _video_length += i->main_picture()->actual_duration();
142                 }
143
144                 if (i->main_sound ()) {
145                         if (!i->main_sound()->asset_ref().resolved()) {
146                                 /* We are missing this asset so we can't continue; examination will be repeated later */
147                                 _needs_assets = true;
148                                 return;
149                         }
150
151                         _has_audio = true;
152                         shared_ptr<dcp::SoundAsset> asset = i->main_sound()->asset ();
153
154                         if (!_audio_channels) {
155                                 _audio_channels = asset->channels ();
156                         } else if (_audio_channels.get() != asset->channels ()) {
157                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
158                         }
159
160                         if (!_audio_frame_rate) {
161                                 _audio_frame_rate = asset->sampling_rate ();
162                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
163                                 throw DCPError (_("Mismatched audio sample rates in DCP"));
164                         }
165
166                         _audio_length += i->main_sound()->actual_duration();
167                 }
168
169                 if (i->main_subtitle ()) {
170                         if (!i->main_subtitle()->asset_ref().resolved()) {
171                                 /* We are missing this asset so we can't continue; examination will be repeated later */
172                                 _needs_assets = true;
173                                 return;
174                         }
175
176                         _text_count[TEXT_OPEN_SUBTITLE] = 1;
177                 }
178
179                 BOOST_FOREACH (shared_ptr<dcp::ReelClosedCaptionAsset> j, i->closed_captions()) {
180                         if (!j->asset_ref().resolved()) {
181                                 /* We are missing this asset so we can't continue; examination will be repeated later */
182                                 _needs_assets = true;
183                                 return;
184                         }
185
186                         _text_count[TEXT_CLOSED_CAPTION]++;
187                 }
188
189                 if (i->main_markers ()) {
190                         map<dcp::Marker, dcp::Time> rm = i->main_markers()->get();
191                         _markers.insert (rm.begin(), rm.end());
192                 }
193
194                 if (i->main_picture()) {
195                         _reel_lengths.push_back (i->main_picture()->actual_duration());
196                 } else if (i->main_sound()) {
197                         _reel_lengths.push_back (i->main_sound()->actual_duration());
198                 } else if (i->main_subtitle()) {
199                         _reel_lengths.push_back (i->main_subtitle()->actual_duration());
200                 } else if (!i->closed_captions().empty()) {
201                         _reel_lengths.push_back (i->closed_captions().front()->actual_duration());
202                 }
203         }
204
205         _encrypted = cpl->encrypted ();
206         _kdm_valid = true;
207
208         /* Check that we can read the first picture, sound and subtitle frames of each reel */
209         try {
210                 BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
211                         shared_ptr<dcp::PictureAsset> pic = i->main_picture()->asset ();
212                         shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (pic);
213                         shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (pic);
214
215                         if (mono) {
216                                 mono->start_read()->get_frame(0)->xyz_image ();
217                         } else {
218                                 stereo->start_read()->get_frame(0)->xyz_image (dcp::EYE_LEFT);
219                         }
220
221                         if (i->main_sound()) {
222                                 shared_ptr<dcp::SoundAsset> sound = i->main_sound()->asset ();
223                                 i->main_sound()->asset()->start_read()->get_frame(0);
224                         }
225
226                         if (i->main_subtitle()) {
227                                 i->main_subtitle()->asset()->subtitles ();
228                         }
229                 }
230         } catch (dcp::ReadError& e) {
231                 _kdm_valid = false;
232         } catch (dcp::MiscError& e) {
233                 _kdm_valid = false;
234         }
235
236         DCPOMATIC_ASSERT (cpl->standard ());
237         _standard = cpl->standard().get();
238         _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
239                 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
240         _ratings = list_to_vector (cpl->ratings());
241         _content_version = cpl->content_version_label_text ();
242
243         _cpl = cpl->id ();
244 }