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