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/sound_asset.h>
43 #include <boost/foreach.hpp>
50 using std::runtime_error;
51 using boost::shared_ptr;
52 using boost::dynamic_pointer_cast;
54 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
58 , _has_subtitles (false)
60 , _needs_assets (false)
64 shared_ptr<dcp::CPL> cpl;
66 if (content->cpl ()) {
67 /* Use the CPL that the content was using before */
68 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
69 if (i->id() == content->cpl().get()) {
74 /* Choose the CPL with the fewest unsatisfied references */
76 int least_unsatisfied = INT_MAX;
78 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
80 BOOST_FOREACH (shared_ptr<dcp::Reel> j, i->reels()) {
81 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
84 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
87 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
92 if (unsatisfied < least_unsatisfied) {
93 least_unsatisfied = unsatisfied;
100 throw DCPError ("No CPLs found in DCP");
104 _name = cpl->content_title_text ();
106 BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
108 if (i->main_picture ()) {
109 if (!i->main_picture()->asset_ref().resolved()) {
110 /* We are missing this asset so we can't continue; examination will be repeated later */
111 _needs_assets = true;
115 dcp::Fraction const frac = i->main_picture()->edit_rate ();
116 float const fr = float(frac.numerator) / frac.denominator;
117 if (!_video_frame_rate) {
118 _video_frame_rate = fr;
119 } else if (_video_frame_rate.get() != fr) {
120 throw DCPError (_("Mismatched frame rates in DCP"));
123 shared_ptr<dcp::PictureAsset> asset = i->main_picture()->asset ();
125 _video_size = asset->size ();
126 } else if (_video_size.get() != asset->size ()) {
127 throw DCPError (_("Mismatched video sizes in DCP"));
130 _video_length += i->main_picture()->duration();
133 if (i->main_sound ()) {
134 if (!i->main_sound()->asset_ref().resolved()) {
135 /* We are missing this asset so we can't continue; examination will be repeated later */
136 _needs_assets = true;
140 shared_ptr<dcp::SoundAsset> asset = i->main_sound()->asset ();
142 if (!_audio_channels) {
143 _audio_channels = asset->channels ();
144 } else if (_audio_channels.get() != asset->channels ()) {
145 throw DCPError (_("Mismatched audio channel counts in DCP"));
148 if (!_audio_frame_rate) {
149 _audio_frame_rate = asset->sampling_rate ();
150 } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
151 throw DCPError (_("Mismatched audio sample rates in DCP"));
154 _audio_length += i->main_sound()->duration();
157 if (i->main_subtitle ()) {
158 if (!i->main_subtitle()->asset_ref().resolved()) {
159 /* We are missing this asset so we can't continue; examination will be repeated later */
160 _needs_assets = true;
164 _has_subtitles = true;
167 if (i->main_picture()) {
168 _reel_lengths.push_back (i->main_picture()->duration());
169 } else if (i->main_sound()) {
170 _reel_lengths.push_back (i->main_sound()->duration());
171 } else if (i->main_subtitle()) {
172 _reel_lengths.push_back (i->main_subtitle()->duration());
176 _encrypted = cpl->encrypted ();
179 /* Check that we can read the first picture, sound and subtitle frames of each reel */
181 BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
182 shared_ptr<dcp::PictureAsset> pic = i->main_picture()->asset ();
183 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (pic);
184 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (pic);
187 mono->start_read()->get_frame(0)->xyz_image ();
189 stereo->start_read()->get_frame(0)->xyz_image (dcp::EYE_LEFT);
192 if (i->main_sound()) {
193 shared_ptr<dcp::SoundAsset> sound = i->main_sound()->asset ();
194 i->main_sound()->asset()->start_read()->get_frame(0);
197 if (i->main_subtitle()) {
198 i->main_subtitle()->asset()->subtitles ();
201 } catch (dcp::DCPReadError& e) {
203 } catch (dcp::MiscError& e) {
207 DCPOMATIC_ASSERT (cpl->standard ());
208 _standard = cpl->standard().get();
209 _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
210 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());