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