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