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)
63 , _needs_assets (false)
67 shared_ptr<dcp::CPL> cpl;
69 if (content->cpl ()) {
70 /* Use the CPL that the content was using before */
71 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
72 if (i->id() == content->cpl().get()) {
77 /* Choose the CPL with the fewest unsatisfied references */
79 int least_unsatisfied = INT_MAX;
81 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
83 BOOST_FOREACH (shared_ptr<dcp::Reel> j, i->reels()) {
84 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
87 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
90 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
95 if (unsatisfied < least_unsatisfied) {
96 least_unsatisfied = unsatisfied;
103 throw DCPError ("No CPLs found in DCP");
107 _name = cpl->content_title_text ();
109 BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
111 if (i->main_picture ()) {
112 if (!i->main_picture()->asset_ref().resolved()) {
113 /* We are missing this asset so we can't continue; examination will be repeated later */
114 _needs_assets = true;
118 dcp::Fraction const frac = i->main_picture()->edit_rate ();
119 float const fr = float(frac.numerator) / frac.denominator;
120 if (!_video_frame_rate) {
121 _video_frame_rate = fr;
122 } else if (_video_frame_rate.get() != fr) {
123 throw DCPError (_("Mismatched frame rates in DCP"));
127 shared_ptr<dcp::PictureAsset> asset = i->main_picture()->asset ();
129 _video_size = asset->size ();
130 } else if (_video_size.get() != asset->size ()) {
131 throw DCPError (_("Mismatched video sizes in DCP"));
134 _video_length += i->main_picture()->duration();
137 if (i->main_sound ()) {
138 if (!i->main_sound()->asset_ref().resolved()) {
139 /* We are missing this asset so we can't continue; examination will be repeated later */
140 _needs_assets = true;
145 shared_ptr<dcp::SoundAsset> asset = i->main_sound()->asset ();
147 if (!_audio_channels) {
148 _audio_channels = asset->channels ();
149 } else if (_audio_channels.get() != asset->channels ()) {
150 throw DCPError (_("Mismatched audio channel counts in DCP"));
153 if (!_audio_frame_rate) {
154 _audio_frame_rate = asset->sampling_rate ();
155 } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
156 throw DCPError (_("Mismatched audio sample rates in DCP"));
159 _audio_length += i->main_sound()->duration();
162 if (i->main_subtitle ()) {
163 if (!i->main_subtitle()->asset_ref().resolved()) {
164 /* We are missing this asset so we can't continue; examination will be repeated later */
165 _needs_assets = true;
172 if (i->closed_caption ()) {
173 if (!i->closed_caption()->asset_ref().resolved()) {
174 /* We are missing this asset so we can't continue; examination will be repeated later */
175 _needs_assets = true;
182 if (i->main_picture()) {
183 _reel_lengths.push_back (i->main_picture()->duration());
184 } else if (i->main_sound()) {
185 _reel_lengths.push_back (i->main_sound()->duration());
186 } else if (i->main_subtitle()) {
187 _reel_lengths.push_back (i->main_subtitle()->duration());
188 } else if (i->closed_caption()) {
189 _reel_lengths.push_back (i->closed_caption()->duration());
193 _encrypted = cpl->encrypted ();
196 /* Check that we can read the first picture, sound and subtitle frames of each reel */
198 BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
199 shared_ptr<dcp::PictureAsset> pic = i->main_picture()->asset ();
200 shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (pic);
201 shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (pic);
204 mono->start_read()->get_frame(0)->xyz_image ();
206 stereo->start_read()->get_frame(0)->xyz_image (dcp::EYE_LEFT);
209 if (i->main_sound()) {
210 shared_ptr<dcp::SoundAsset> sound = i->main_sound()->asset ();
211 i->main_sound()->asset()->start_read()->get_frame(0);
214 if (i->main_subtitle()) {
215 i->main_subtitle()->asset()->subtitles ();
218 } catch (dcp::DCPReadError& e) {
220 } catch (dcp::MiscError& e) {
224 DCPOMATIC_ASSERT (cpl->standard ());
225 _standard = cpl->standard().get();
226 _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
227 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());