Still more licence fixups.
[libdcp.git] / src / reel_picture_asset.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /** @file  src/reel_picture_asset.h
21  *  @brief ReelPictureAsset class.
22  */
23
24 #include "reel_picture_asset.h"
25 #include "picture_asset.h"
26 #include "dcp_assert.h"
27 #include "raw_convert.h"
28 #include "compose.hpp"
29 #include <libcxml/cxml.h>
30 #include <libxml++/libxml++.h>
31 #include <iomanip>
32 #include <cmath>
33
34 using std::bad_cast;
35 using std::string;
36 using std::stringstream;
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39 using boost::optional;
40 using namespace dcp;
41
42 ReelPictureAsset::ReelPictureAsset ()
43         : _frame_rate (Fraction (24, 1))
44         , _screen_aspect_ratio (Fraction (1998, 1080))
45 {
46
47 }
48
49 ReelPictureAsset::ReelPictureAsset (shared_ptr<PictureAsset> asset, int64_t entry_point)
50         : ReelAsset (asset, asset->edit_rate(), asset->intrinsic_duration(), entry_point)
51         , ReelMXF (asset->key_id())
52         , _frame_rate (asset->frame_rate ())
53         , _screen_aspect_ratio (asset->screen_aspect_ratio ())
54 {
55
56 }
57
58 ReelPictureAsset::ReelPictureAsset (shared_ptr<const cxml::Node> node)
59         : ReelAsset (node)
60         , ReelMXF (node)
61 {
62         _frame_rate = Fraction (node->string_child ("FrameRate"));
63         try {
64                 _screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
65         } catch (XMLError& e) {
66                 /* It's not a fraction */
67                 try {
68                         float f = node->number_child<float> ("ScreenAspectRatio");
69                         _screen_aspect_ratio = Fraction (f * 1000, 1000);
70                 } catch (bad_cast& e) {
71
72                 }
73         }
74 }
75
76 void
77 ReelPictureAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
78 {
79         ReelAsset::write_to_cpl (node, standard);
80
81         /* Find <MainPicture> */
82         xmlpp::Node* mp = find_child (node, cpl_node_name ());
83
84         mp->add_child ("FrameRate")->add_child_text (String::compose ("%1 %2", _frame_rate.numerator, _frame_rate.denominator));
85         if (standard == INTEROP) {
86
87                 /* Allowed values for this tag from the standard */
88                 float allowed[] = { 1.33, 1.66, 1.77, 1.85, 2.00, 2.39 };
89                 int const num_allowed = sizeof(allowed) / sizeof(float);
90
91                 /* Actual ratio */
92                 float ratio = float (_screen_aspect_ratio.numerator) / _screen_aspect_ratio.denominator;
93
94                 /* Pick the closest and use that */
95                 optional<float> closest;
96                 optional<float> error;
97                 for (int i = 0; i < num_allowed; ++i) {
98                         float const e = fabsf (allowed[i] - ratio);
99                         if (!closest || e < error.get()) {
100                                 closest = allowed[i];
101                                 error = e;
102                         }
103                 }
104
105                 mp->add_child ("ScreenAspectRatio")->add_child_text (raw_convert<string> (closest.get(), 2, true));
106         } else {
107                 mp->add_child ("ScreenAspectRatio")->add_child_text (
108                         String::compose ("%1 %2", _screen_aspect_ratio.numerator, _screen_aspect_ratio.denominator)
109                         );
110         }
111
112         if (key_id ()) {
113                 /* Find <Hash> */
114                 xmlpp::Node* hash = find_child (mp, "Hash");
115                 mp->add_child_before (hash, "KeyId")->add_child_text ("urn:uuid:" + key_id().get ());
116         }
117 }
118
119 string
120 ReelPictureAsset::key_type () const
121 {
122         return "MDIK";
123 }
124
125 bool
126 ReelPictureAsset::equals (shared_ptr<const ReelAsset> other, EqualityOptions opt, NoteHandler note) const
127 {
128         if (!ReelAsset::equals (other, opt, note)) {
129                 return false;
130         }
131
132         shared_ptr<const ReelPictureAsset> rpa = dynamic_pointer_cast<const ReelPictureAsset> (other);
133         if (!rpa) {
134                 return false;
135         }
136
137         if (_frame_rate != rpa->_frame_rate) {
138                 note (DCP_ERROR, "frame rates differ in reel");
139                 return false;
140         }
141
142         if (_screen_aspect_ratio != rpa->_screen_aspect_ratio) {
143                 note (DCP_ERROR, "screen aspect ratios differ in reel");
144                 return false;
145         }
146
147         return true;
148 }