2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "dcp_examiner.h"
22 #include "dcp_content.h"
23 #include "exceptions.h"
27 #include <dcp/decrypted_kdm.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/sound_asset.h>
44 #include <boost/foreach.hpp>
51 using std::runtime_error;
52 using boost::shared_ptr;
53 using boost::dynamic_pointer_cast;
55 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
62 , _needs_assets (false)
66 shared_ptr<dcp::CPL> cpl;
68 for (int i = 0; i < TEXT_COUNT; ++i) {
72 if (content->cpl ()) {
73 /* Use the CPL that the content was using before */
74 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
75 if (i->id() == content->cpl().get()) {
80 /* Choose the CPL with the fewest unsatisfied references */
82 int least_unsatisfied = INT_MAX;
84 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
86 BOOST_FOREACH (shared_ptr<dcp::Reel> j, i->reels()) {
87 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
90 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
93 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
98 if (unsatisfied < least_unsatisfied) {
99 least_unsatisfied = unsatisfied;
106 throw DCPError ("No CPLs found in DCP");
110 _name = cpl->content_title_text ();
112 BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
114 if (i->main_picture ()) {
115 if (!i->main_picture()->asset_ref().resolved()) {
116 /* We are missing this asset so we can't continue; examination will be repeated later */
117 _needs_assets = true;
121 dcp::Fraction const frac = i->main_picture()->edit_rate ();
122 float const fr = float(frac.numerator) / frac.denominator;
123 if (!_video_frame_rate) {
124 _video_frame_rate = fr;
125 } else if (_video_frame_rate.get() != fr) {
126 throw DCPError (_("Mismatched frame rates in DCP"));
130 shared_ptr<dcp::PictureAsset> asset = i->main_picture()->asset ();
132 _video_size = asset->size ();
133 } else if (_video_size.get() != asset->size ()) {
134 throw DCPError (_("Mismatched video sizes in DCP"));
137 _video_length += i->main_picture()->duration();
140 if (i->main_sound ()) {
141 if (!i->main_sound()->asset_ref().resolved()) {
142 /* We are missing this asset so we can't continue; examination will be repeated later */
143 _needs_assets = true;
148 shared_ptr<dcp::SoundAsset> asset = i->main_sound()->asset ();
150 if (!_audio_channels) {
151 _audio_channels = asset->channels ();
152 } else if (_audio_channels.get() != asset->channels ()) {
153 throw DCPError (_("Mismatched audio channel counts in DCP"));
156 if (!_audio_frame_rate) {
157 _audio_frame_rate = asset->sampling_rate ();
158 } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
159 throw DCPError (_("Mismatched audio sample rates in DCP"));
162 _audio_length += i->main_sound()->duration();
165 if (i->main_subtitle ()) {
166 if (!i->main_subtitle()->asset_ref().resolved()) {
167 /* We are missing this asset so we can't continue; examination will be repeated later */
168 _needs_assets = true;
172 _has_text[TEXT_OPEN_SUBTITLE] = true;
175 BOOST_FOREACH (shared_ptr<dcp::ReelClosedCaptionAsset> j, i->closed_captions()) {
176 if (!j->asset_ref().resolved()) {
177 /* We are missing this asset so we can't continue; examination will be repeated later */
178 _needs_assets = true;
182 _has_text[TEXT_CLOSED_CAPTION] = true;
185 if (i->main_picture()) {
186 _reel_lengths.push_back (i->main_picture()->duration());
187 } else if (i->main_sound()) {
188 _reel_lengths.push_back (i->main_sound()->duration());
189 } else if (i->main_subtitle()) {
190 _reel_lengths.push_back (i->main_subtitle()->duration());
191 } else if (!i->closed_captions().empty()) {
192 _reel_lengths.push_back (i->closed_captions().front()->duration());
196 _encrypted = cpl->encrypted ();
199 /* Check that we can read the first picture, sound and subtitle frames of each reel */
201 BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
202 shared_ptr<dcp::PictureAsset> pic = i->main_picture()->asset ();
203 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (pic);
204 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (pic);
207 mono->start_read()->get_frame(0)->xyz_image ();
209 stereo->start_read()->get_frame(0)->xyz_image (dcp::EYE_LEFT);
212 if (i->main_sound()) {
213 shared_ptr<dcp::SoundAsset> sound = i->main_sound()->asset ();
214 i->main_sound()->asset()->start_read()->get_frame(0);
217 if (i->main_subtitle()) {
218 i->main_subtitle()->asset()->subtitles ();
221 } catch (dcp::DCPReadError& e) {
223 } catch (dcp::MiscError& e) {
227 DCPOMATIC_ASSERT (cpl->standard ());
228 _standard = cpl->standard().get();
229 _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
230 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());