Merge master.
[dcpomatic.git] / src / lib / dcp_examiner.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <dcp/dcp.h>
21 #include <dcp/cpl.h>
22 #include <dcp/reel.h>
23 #include <dcp/reel_picture_asset.h>
24 #include <dcp/reel_sound_asset.h>
25 #include <dcp/sound_mxf.h>
26 #include "dcp_examiner.h"
27 #include "dcp_content.h"
28 #include "exceptions.h"
29
30 #include "i18n.h"
31
32 using std::list;
33 using boost::shared_ptr;
34
35 DCPExaminer::DCPExaminer (shared_ptr<const DCPContent> content)
36 {
37         dcp::DCP dcp (content->directory ());
38         dcp.read ();
39
40         if (dcp.cpls().size() == 0) {
41                 throw DCPError ("No CPLs found in DCP");
42         } else if (dcp.cpls().size() > 1) {
43                 throw DCPError ("Multiple CPLs found in DCP");
44         }
45
46         _name = dcp.cpls().front()->content_title_text ();
47
48         list<shared_ptr<dcp::Reel> > reels = dcp.cpls().front()->reels ();
49         for (list<shared_ptr<dcp::Reel> >::const_iterator i = reels.begin(); i != reels.end(); ++i) {
50
51                 if ((*i)->main_picture ()) {
52                         dcp::Fraction const frac = (*i)->main_picture()->frame_rate ();
53                         float const fr = float(frac.numerator) / frac.denominator;
54                         if (!_video_frame_rate) {
55                                 _video_frame_rate = fr;
56                         } else if (_video_frame_rate.get() != fr) {
57                                 throw DCPError (_("Mismatched frame rates in DCP"));
58                         }
59
60                         shared_ptr<dcp::PictureMXF> mxf = (*i)->main_picture()->mxf ();
61                         if (!_video_size) {
62                                 _video_size = mxf->size ();
63                         } else if (_video_size.get() != mxf->size ()) {
64                                 throw DCPError (_("Mismatched video sizes in DCP"));
65                         }
66
67                         _video_length += ContentTime::from_frames ((*i)->main_picture()->duration(), _video_frame_rate.get ());
68                 }
69                         
70                 if ((*i)->main_sound ()) {
71                         shared_ptr<dcp::SoundMXF> mxf = (*i)->main_sound()->mxf ();
72
73                         if (!_audio_channels) {
74                                 _audio_channels = mxf->channels ();
75                         } else if (_audio_channels.get() != mxf->channels ()) {
76                                 throw DCPError (_("Mismatched audio channel counts in DCP"));
77                         }
78
79                         if (!_audio_frame_rate) {
80                                 _audio_frame_rate = mxf->sampling_rate ();
81                         } else if (_audio_frame_rate.get() != mxf->sampling_rate ()) {
82                                 throw DCPError (_("Mismatched audio frame rates in DCP"));
83                         }
84
85                         _audio_length += ContentTime::from_frames ((*i)->main_sound()->duration(), _video_frame_rate.get ());
86                 }
87
88                 if ((*i)->main_subtitle ()) {
89                         _has_subtitles = true;
90                 }
91         }
92 }