Allow import of OV/VF DCPs (#906).
[dcpomatic.git] / src / lib / dcp_examiner.cc
1 /*
2     Copyright (C) 2014 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/reel_subtitle_asset.h>
39 #include <dcp/sound_asset.h>
40 #include <boost/foreach.hpp>
41 #include <iostream>
42
43 #include "i18n.h"
44
45 using std::list;
46 using std::cout;
47 using std::runtime_error;
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
50
51 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
52         : DCP (content)
53         , _video_length (0)
54         , _audio_length (0)
55         , _has_subtitles (false)
56         , _encrypted (false)
57         , _needs_assets (false)
58         , _kdm_valid (false)
59         , _three_d (false)
60 {
61         shared_ptr<dcp::CPL> cpl;
62
63         if (content->cpl ()) {
64                 /* Use the CPL that the content was using before */
65                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
66                         if (i->id() == content->cpl().get()) {
67                                 cpl = i;
68                         }
69                 }
70         } else {
71                 /* Choose the CPL with the fewest unsatisfied references */
72
73                 int least_unsatisfied = INT_MAX;
74
75                 BOOST_FOREACH (shared_ptr<dcp::CPL> i, cpls()) {
76                         int unsatisfied = 0;
77                         BOOST_FOREACH (shared_ptr<dcp::Reel> j, i->reels()) {
78                                 if (j->main_picture() && !j->main_picture()->asset_ref().resolved()) {
79                                         ++unsatisfied;
80                                 }
81                                 if (j->main_sound() && !j->main_sound()->asset_ref().resolved()) {
82                                         ++unsatisfied;
83                                 }
84                                 if (j->main_subtitle() && !j->main_subtitle()->asset_ref().resolved()) {
85                                         ++unsatisfied;
86                                 }
87                         }
88
89                         if (unsatisfied < least_unsatisfied) {
90                                 least_unsatisfied = unsatisfied;
91                                 cpl = i;
92                         }
93                 }
94         }
95
96         if (!cpl) {
97                 throw DCPError ("No CPLs found in DCP");
98         }
99
100         _cpl = cpl->id ();
101         _name = cpl->content_title_text ();
102
103         BOOST_FOREACH (shared_ptr<dcp::Reel> i, cpl->reels()) {
104
105                 if (i->main_picture ()) {
106                         if (!i->main_picture()->asset_ref().resolved()) {
107                                 /* We are missing this asset so we can't continue; examination will be repeated later */
108                                 _needs_assets = true;
109                                 return;
110                         }
111
112                         dcp::Fraction const frac = i->main_picture()->edit_rate ();
113                         float const fr = float(frac.numerator) / frac.denominator;
114                         if (!_video_frame_rate) {
115                                 _video_frame_rate = fr;
116                         } else if (_video_frame_rate.get() != fr) {
117                                 throw DCPError (_("Mismatched frame rates in DCP"));
118                         }
119
120                         shared_ptr<dcp::PictureAsset> asset = i->main_picture()->asset ();
121                         if (!_video_size) {
122                                 _video_size = asset->size ();
123                         } else if (_video_size.get() != asset->size ()) {
124                                 throw DCPError (_("Mismatched video sizes in DCP"));
125                         }
126
127                         _video_length += i->main_picture()->duration();
128                 }
129
130                 if (i->main_sound ()) {
131                         if (!i->main_sound()->asset_ref().resolved()) {
132                                 /* We are missing this asset so we can't continue; examination will be repeated later */
133                                 _needs_assets = true;
134                                 return;
135                         }
136
137                         shared_ptr<dcp::SoundAsset> asset = i->main_sound()->asset ();
138
139                         if (!_audio_channels) {
140                                 _audio_channels = asset->channels ();
141                         } else if (_audio_channels.get() != asset->channels ()) {
142                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
143                         }
144
145                         if (!_audio_frame_rate) {
146                                 _audio_frame_rate = asset->sampling_rate ();
147                         } else if (_audio_frame_rate.get() != asset->sampling_rate ()) {
148                                 throw DCPError (_("Mismatched audio sample rates in DCP"));
149                         }
150
151                         _audio_length += i->main_sound()->duration();
152                 }
153
154                 if (i->main_subtitle ()) {
155                         if (!i->main_subtitle()->asset_ref().resolved()) {
156                                 /* We are missing this asset so we can't continue; examination will be repeated later */
157                                 _needs_assets = true;
158                                 return;
159                         }
160
161                         _has_subtitles = true;
162                 }
163         }
164
165         _encrypted = cpl->encrypted ();
166         _kdm_valid = true;
167
168         /* Check that we can read the first picture frame */
169         try {
170                 if (!cpl->reels().empty ()) {
171                         shared_ptr<dcp::PictureAsset> asset = cpl->reels().front()->main_picture()->asset ();
172                         shared_ptr<dcp::MonoPictureAsset> mono = dynamic_pointer_cast<dcp::MonoPictureAsset> (asset);
173                         shared_ptr<dcp::StereoPictureAsset> stereo = dynamic_pointer_cast<dcp::StereoPictureAsset> (asset);
174
175                         if (mono) {
176                                 mono->start_read()->get_frame(0)->xyz_image ();
177                         } else {
178                                 stereo->start_read()->get_frame(0)->xyz_image (dcp::EYE_LEFT);
179                         }
180
181                 }
182         } catch (dcp::DCPReadError& e) {
183                 _kdm_valid = false;
184                 if (_encrypted && content->kdm ()) {
185                         /* XXX: maybe don't use an exception for this */
186                         throw runtime_error (_("The KDM does not decrypt the DCP.  Perhaps it is targeted at the wrong CPL."));
187                 }
188         }
189
190         DCPOMATIC_ASSERT (cpl->standard ());
191         _standard = cpl->standard().get();
192         _three_d = !cpl->reels().empty() && cpl->reels().front()->main_picture() &&
193                 dynamic_pointer_cast<dcp::StereoPictureAsset> (cpl->reels().front()->main_picture()->asset());
194
195         _cpl = cpl->id ();
196 }