Add very simple benchmark for rgb_to_xyz.
[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     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/reel_picture_asset.h
35  *  @brief ReelPictureAsset class.
36  */
37
38 #include "reel_picture_asset.h"
39 #include "picture_asset.h"
40 #include "dcp_assert.h"
41 #include "raw_convert.h"
42 #include "compose.hpp"
43 #include <libcxml/cxml.h>
44 #include <libxml++/libxml++.h>
45 #include <iomanip>
46 #include <cmath>
47
48 using std::bad_cast;
49 using std::string;
50 using boost::shared_ptr;
51 using boost::dynamic_pointer_cast;
52 using boost::optional;
53 using namespace dcp;
54
55 ReelPictureAsset::ReelPictureAsset (shared_ptr<PictureAsset> asset, int64_t entry_point)
56         : ReelAsset (asset->id(), asset->edit_rate(), asset->intrinsic_duration(), entry_point)
57         , ReelMXF (asset, asset->key_id())
58         , _frame_rate (asset->frame_rate ())
59         , _screen_aspect_ratio (asset->screen_aspect_ratio ())
60 {
61
62 }
63
64 ReelPictureAsset::ReelPictureAsset (shared_ptr<const cxml::Node> node)
65         : ReelAsset (node)
66         , ReelMXF (node)
67 {
68         _frame_rate = Fraction (node->string_child ("FrameRate"));
69         try {
70                 _screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
71         } catch (XMLError& e) {
72                 /* It's not a fraction */
73                 try {
74                         float f = node->number_child<float> ("ScreenAspectRatio");
75                         _screen_aspect_ratio = Fraction (f * 1000, 1000);
76                 } catch (bad_cast& e) {
77
78                 }
79         }
80 }
81
82 xmlpp::Node*
83 ReelPictureAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
84 {
85         xmlpp::Node* asset = write_to_cpl_base (node, standard, hash());
86
87         asset->add_child("FrameRate")->add_child_text(String::compose("%1 %2", _frame_rate.numerator, _frame_rate.denominator));
88         if (standard == INTEROP) {
89
90                 /* Allowed values for this tag from the standard */
91                 float allowed[] = { 1.33, 1.66, 1.77, 1.85, 2.00, 2.39 };
92                 int const num_allowed = sizeof(allowed) / sizeof(float);
93
94                 /* Actual ratio */
95                 float ratio = float (_screen_aspect_ratio.numerator) / _screen_aspect_ratio.denominator;
96
97                 /* Pick the closest and use that */
98                 optional<float> closest;
99                 optional<float> error;
100                 for (int i = 0; i < num_allowed; ++i) {
101                         float const e = fabsf (allowed[i] - ratio);
102                         if (!closest || e < error.get()) {
103                                 closest = allowed[i];
104                                 error = e;
105                         }
106                 }
107
108                 asset->add_child ("ScreenAspectRatio")->add_child_text (raw_convert<string> (closest.get(), 2, true));
109         } else {
110                 asset->add_child ("ScreenAspectRatio")->add_child_text (
111                         String::compose ("%1 %2", _screen_aspect_ratio.numerator, _screen_aspect_ratio.denominator)
112                         );
113         }
114
115         if (key_id ()) {
116                 /* Find <Hash> */
117                 xmlpp::Node* hash = find_child (asset, "Hash");
118                 asset->add_child_before(hash, "KeyId")->add_child_text("urn:uuid:" + key_id().get());
119         }
120
121         return asset;
122 }
123
124 string
125 ReelPictureAsset::key_type () const
126 {
127         return "MDIK";
128 }
129
130 bool
131 ReelPictureAsset::equals (shared_ptr<const ReelPictureAsset> other, EqualityOptions opt, NoteHandler note) const
132 {
133         if (!asset_equals (other, opt, note)) {
134                 return false;
135         }
136         if (!mxf_equals (other, opt, note)) {
137                 return false;
138         }
139
140         shared_ptr<const ReelPictureAsset> rpa = dynamic_pointer_cast<const ReelPictureAsset> (other);
141         if (!rpa) {
142                 return false;
143         }
144
145         if (_frame_rate != rpa->_frame_rate) {
146                 note (DCP_ERROR, "frame rates differ in reel");
147                 return false;
148         }
149
150         if (_screen_aspect_ratio != rpa->_screen_aspect_ratio) {
151                 note (DCP_ERROR, "screen aspect ratios differ in reel");
152                 return false;
153         }
154
155         return true;
156 }