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