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